【问题标题】:Turtle graphics problems海龟图形问题
【发布时间】:2021-05-06 14:35:30
【问题描述】:

我试图用海龟图形制作一个类似于蛇的游戏。 我添加了 wn.update 行,但每次它只是说 en 错误,如果我删除它,它就会崩溃。 有人知道为什么吗?

我在 python 中很菜鸟,所以我可能会犯一个非常愚蠢的错误

import turtle
import random

score = 0

def go_up():
    if cat.direction != "down":
        cat.direction = "up"

def go_down():
    if cat.direction != "up":
        cat.direction = "down"

def go_left():
    if cat.direction != "right":
        cat.direction = "left"

def go_right():
    if cat.direction != "left":
        cat.direction = "right"

def move():
    if cat.direction == "up":
        y = cat.ycor()
        cat.sety(y + 20)

    if cat.direction == "down":
        y = cat.ycor()
        cat.sety(y - 20)

    if cat.direction == "left":
        x = cat.xcor()
        cat.setx(x - 20)

    if cat.direction == "right":
        x = cat.xcor()
        cat.setx(x + 20)

wn = turtle.Screen()
wn.setup(width =600,height= 600)
wn.tracer(0)

mouse=turtle.Turtle()
mouse.penup()
mouse.goto(0,100)

cat = turtle.Turtle()
cat.penup()

while True:
    wn.update()
    if cat.distance(mouse) < 20:
        x = random.randint(0, 500)
        y = random.randint(0, 500)
        mouse.goto(x, y)

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

wn.mainloop()

【问题讨论】:

  • 它是崩溃了还是只是出错了?
  • 如果它给出的错误比哪个错误?
  • @JonathanDrukker 它给出了一个错误,如果我删除该行,它就会崩溃。错误是:File "C:/Users/avner/PycharmProjects/CatAndMouse/main.py", line 52, in &lt;module&gt; wn.update() File "C:\Users\avner\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 1304, in update t._update_data() File "C:\Users\avner\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2647, in _update_data self.screen._incrementudc() File "C:\Users\avner\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 1293, in _incrementudc raise Terminator turtle.Terminator

标签: python python-3.x turtle-graphics python-turtle


【解决方案1】:

您的程序结构不正确。你不应该在事件驱动的龟中拥有while True:。 (尤其是在您需要执行的附加语句之前!)在您的程序基本工作之前,您不应该实现tracer()update()。这是“可播放”的代码的返工:

from turtle import Screen, Turtle
from random import randint

CURSOR_SIZE = 20

def go_up():
    if cat.direction != 'down':
        cat.direction = 'up'
        cat.setheading(90)
        screen.update()

def go_down():
    if cat.direction != 'up':
        cat.direction = 'down'
        cat.setheading(270)
        screen.update()

def go_left():
    if cat.direction != 'right':
        cat.direction = 'left'
        cat.setheading(180)
        screen.update()

def go_right():
    if cat.direction != 'left':
        cat.direction = 'right'
        cat.setheading(0)
        screen.update()

def move():
    cat.forward(20)

    if cat.distance(mouse) < CURSOR_SIZE:
        x = randint(CURSOR_SIZE - 300, 300 - CURSOR_SIZE)
        y = randint(CURSOR_SIZE - 300, 300 - CURSOR_SIZE)
        mouse.goto(x, y)

    screen.update()

    screen.ontimer(move, 200)  # 1/5 second delay in milliseconds

screen = Screen()
screen.setup(width=600, height=600)
screen.tracer(False)

mouse = Turtle('turtle')
mouse.penup()
mouse.sety(100)

cat = Turtle('arrow')
cat.penup()
cat.direction = 'right'  # user defined property

screen.onkeypress(go_up, 'w')
screen.onkeypress(go_down, 's')
screen.onkeypress(go_left, 'a')
screen.onkeypress(go_right, 'd')
screen.listen()

move()

screen.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多