为什么说我的mainloop无法访问?

时间:2019-10-25 21:48:12

标签: python loops pycharm turtle-graphics

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

1 个答案:

答案 0 :(得分:0)

这里真正的错误是在事件驱动的环境(如turtle)中使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="dirInstruction">101st</div> <div class="dirInstruction">1000001st</div> <div class="dirInstruction">1st</div> <div class="dirInstruction">addasd1stbbb</div> <div class="dirInstruction">1st1st1st</div>while True:。我们可以用乌龟sleep()方法替换无限循环和time.sleep()

ontimer()