每当我尝试运行脚本时,它都会停止工作

发布于 2025-01-27 07:58:08 字数 435 浏览 3 评论 0原文

我目前正在制作PY游戏,而Python每次都停止工作,但我不确定为什么。

from turtle import Turtle, Screen

screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')

screen.title('Snake Game')
turtles = []
y = 0
x = 0
for _ in range(3):
    turtle = Turtle('square')
    turtle.color('white')
    turtle.penup()
    turtle.goto(x, y)
    x -= 20


game_is_on = True
while game_is_on:
    for seg in turtles:
        seg.forward(20)

I am currently making a py game and python stops working every time, but I'm not sure why.

from turtle import Turtle, Screen

screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')

screen.title('Snake Game')
turtles = []
y = 0
x = 0
for _ in range(3):
    turtle = Turtle('square')
    turtle.color('white')
    turtle.penup()
    turtle.goto(x, y)
    x -= 20


game_is_on = True
while game_is_on:
    for seg in turtles:
        seg.forward(20)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2025-02-03 07:58:08

您已经忘记将每只乌龟添加到Turtles列表中。您需要做的就是将乌龟添加到for循环中。

这是代码:

from turtle import Turtle, Screen

screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')

screen.title('Snake Game')
turtles = []
y = 0
x = 0
for _ in range(3):
    turtle = Turtle('square')
    turtle.color('white')
    turtle.penup()
    turtle.goto(x, y)
    turtles.append(turtle)
    x -= 20

game_is_on = True
while game_is_on:
    for seg in turtles:
        seg.forward(20)

You have forgotten to add each turtle to the turtles list. All you need to do is append the turtle in the for loop.

Here is the code:

from turtle import Turtle, Screen

screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')

screen.title('Snake Game')
turtles = []
y = 0
x = 0
for _ in range(3):
    turtle = Turtle('square')
    turtle.color('white')
    turtle.penup()
    turtle.goto(x, y)
    turtles.append(turtle)
    x -= 20

game_is_on = True
while game_is_on:
    for seg in turtles:
        seg.forward(20)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文