【发布时间】:2020-02-22 06:09:38
【问题描述】:
import turtle
import time
delay = 0.1
ms = turtle.Screen()
ms.title("hungry snake Game by @Rafa")
ms.bgcolor("green")
ms.setup(width=600, height=600)
ms.tracer(0)
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x + 20)
if head.direction == "right":
x = head.xcor()
head.setx(x - 20)
# main game loop
while True:
ms.update()
move()
time.sleep(delay)
//Here is where am having trouble with the code?
ms.mainloop()
【问题讨论】:
-
你有一个永无止境的while循环。之后的任何事情都无法访问
-
@khelwood 他发布的代码格式不正确,我可能在修复时弄错了缩进,所以我修复了它。
-
应该是
turtle.mainloop(),而不是ms.mainloop()。 -
@Barmar 问题的标题是询问为什么代码无法访问,而不是为什么它不起作用。在您重新编辑之前,最后一行无法访问,这大概就是问题所在。
-
哎呀,你是对的。但他也说错了,链接的问题显示了如何解决这个问题。
标签: python loops pycharm turtle-graphics