首页 > 解决方案 > 当我尝试创建蛇游戏时出现 self.screen._incrementudc() 错误和 turtle.Terminator 错误

问题描述

我试图通过查看 youtube 中的视频来创建一个 Snake 游戏。但不幸的是,发布视频的人成功地用下面的代码创建了这个游戏。但是我遇到了错误。

我得到了蛇的方头。但我的蛇一动不动。

请帮助我调试它,以便在我单击 a、s、w、d 时我的蛇会移动,正如您在代码中看到的那样。

我的代码如下:

    import turtle
    import time

    delay = 0.1
# set the screen
    wn = turtle.Screen()
    wn.title("Snake Peek-a-Boo Game ")
    wn.bgcolor("pink")
    wn.setup(width=600, height=600)
    wn.tracer(0)  # screen updates

# Snake head
    head = turtle.Turtle()
    head.speed(0)
    head.shape("square")
    head.color("Black")
    head.penup()
    head.goto(0, 0)
    head.direction = "stop"


# functions
def go_up():
    head.direction = "up"


def go_down():
    head.direction = "down"


def go_left():
    head.direction = "left"


def go_right():
    head.direction = "right"


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)

    # keyboard
    wn.listen()

    wn.onkeypress(go_up, "w")

    wn.onkeypress(go_down, "s")

    wn.onkeypress(go_left, "a")

    wn.onkeypress(go_right, "d")


while True:
    wn.update()

    move()

    time.sleep(delay)

    wn.mainloop()

---------------------------------END OF CODE--------------------
My error below:

 File "<input>", line 1, in <module>
  line 197, in runfile
  pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  PyCharm Community Edition 2020.1.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
   line 69, in <module>
    wn.update()
  line 1303, in update
    t._update_data()
  line 2646, in _update_data
    self.screen._incrementudc()
  line 1292, in _incrementudc
    raise Terminator
turtle.Terminator

标签: python

解决方案


Python 中的缩进在语法中起着重要的作用(不像 C++ 这样的语言,空格通常无关紧要),所以如果遵循别人的代码,也要始终注意空格,因为它们会影响程序的逻辑.

在您发布的代码中,有两个问题:

  1. 不应缩进包含事件侦听器(从wn.listen()到)的块。wn.onkeypress(go_right, "d")由于您不小心缩进了它,Python 认为它属于该move()函数。这仍然有效,因为它将在 while 循环的每次迭代中执行,但我会将它移到move()函数范围之外。
  2. 更大的问题:wn.mainloop()也是缩进的,这导致它成为while循环的一部分。您应该删除缩进以wn.mainloop()自行运行。

尝试以下操作:

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)

# keyboard
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")

while True:
    wn.update()
    move()
    time.sleep(delay)
    
wn.mainloop()

此外,该函数似乎move还包含一些与运动相关的错误,因为您正在执行相同的上下移动代码,以及相同的左右移动代码。我建议你看一下head.sety(y + 20)indownhead.setx(x + 20)inleft()来调试你的代码。


推荐阅读