单张图片会显示,但多张图片不会使用 Python Turtle 库

Single Images will show but multiple images will not using Python Turtle library

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

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)

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

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

首先,永久静止:

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() returns.

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

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()