【问题标题】:Single Images will show but multiple images will not using Python Turtle library单个图像将显示,但多个图像不会使用 Python Turtle 库
【发布时间】:2020-04-22 21:14:04
【问题描述】:

我想出了一个问题,然后我尝试将多个图像放在屏幕上,只有最新的图像才会显示。

AntiAirMissleLauncherRed(200,-200)
InfantryRed(-200,200)
JeepRed(-200,-200)

我在每个图像中放置了不同的坐标,因此它们应该出现在屏幕上的不同位置。然而海龟只是移动到下一个位置而不是打印图像。 The last Image

这是我用来使图像出现在屏幕上的代码:

def InfantryRed(x,y):

    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()

    RedInfantry = "C:\\Users\\User\\Desktop\\a level computer science\\Coursework\\Week\\Red team\\InfantryRedV20.gif"

    screen.addshape(RedInfantry)
    turtle.shape(RedInfantry)

当单独打印这些图像时,它们中的每一个都会显示,但是当尝试在同一屏幕上打印多个图像时它不起作用。

【问题讨论】:

    标签: python image turtle-graphics


    【解决方案1】:

    您没有正确使用海龟命令。根据您的图像是永久静止还是需要移动,您需要采取不同的方法。

    首先,永久静止:

    from turtle import Screen, Turtle
    
    RedInfantry = r"C:\Users\User\Desktop\a level computer science\Coursework\Week\Red team\InfantryRedV20.gif"
    
    def InfantryRed(x, y):
        turtle.shape(RedInfantry)
        turtle.goto(x, y)
        return turtle.stamp()  # leave a lasting impression
    
    screen = Screen()
    screen.addshape(RedInfantry)
    
    turtle = Turtle()
    turtle.penup()
    
    infantry_1 = InfantryRed(200, -200)
    infantry_2 = InfantryRed(-200, 200)
    infantry_3 = InfantryRed(-200, -200)
    
    turtle.clearstamp(infantry_1)
    
    screen.exitonclick()
    

    用匹配的screen.addshape() 调用填写您的其他图像路径。您将无法移动上述图像,但如果您想最终删除它们,请跟踪 stamp() 返回的内容。

    如果您希望能够移动(和隐藏)您的图像,那么您需要为每个图像分配一个海龟:

    from turtle import Screen, Turtle
    
    RedInfantry = r"C:\Users\User\Desktop\a level computer science\Coursework\Week\Red team\InfantryRedV20.gif"
    
    def InfantryRed(x, y):
        turtle = Turtle()
        turtle.shape(RedInfantry)
        turtle.penup()
        turtle.goto(x, y)
    
        return turtle
    
    screen = Screen()
    screen.addshape(RedInfantry)
    
    infantry_1 = InfantryRed(200, -200)
    infantry_2 = InfantryRed(-200, 200)
    infantry_3 = InfantryRed(-200, -200)
    
    infantry_1.goto(200, 200)
    
    screen.exitonclick()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 2020-11-13
      相关资源
      最近更新 更多