【问题标题】:When I run my Python Turtle game, the Graphics Window stops responding当我运行 Python Turtle 游戏时,图形窗口停止响应
【发布时间】:2023-03-30 02:58:01
【问题描述】:

我是海龟和一般编程的新手,想制作一个标签游戏。有两个用户,一个是跑步者,一个是追逐者(Bob 和 Phil)。当追逐者靠近跑步者时(使用距离公式检测),程序会将它们设置回原来的起始位置。然而,每当我运行程序时,Turtle Graphics 窗口就会冻结并停止响应,最终导致我关闭程序。谁能帮我确定问题?

import turtle
import math
bob = turtle.Turtle()

wn = turtle.Screen()
wn.bgcolor("black")
wn.setup(width = 700, height = 700)
wn.tracer()

border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
border_pen.hideturtle()

bob.shape("triangle")
bob.speed(0)
bob.color("blue")
bob.penup()
bob.setpos(-50, 0)
def draw():
    bob.penup()
def nodraw():
    bob.pendown()

def fd():
    bob.fd(20)
    if bob.xcor() > 280:
        bob.setx(280)
    if bob.ycor() > 280:
        bob.sety(280)

    if bob.xcor() < -280:
        bob.setx(-280)
    if bob.ycor() <- 280:
        bob.sety(-280)
def right():
    bob.right(90)

def left():
    bob.left(90)

turtle.listen()
turtle.onkey(left, "Left")
turtle.onkey(fd, "Up")
turtle.onkey(right, "Right")

phil = turtle.Turtle()
phil.speed(0)
phil.penup()
phil.shape("triangle")
phil.color("red")
phil.setpos(50, 0)

def fdp():
    phil.fd(20)
    if phil.xcor() > 280:
        phil.setx(280)
    if phil.ycor() > 280:
        phil.sety(280)

    if phil.xcor() < -280:
        phil.setx(-280)
    if phil.ycor() <- 280:
        phil.sety(-280)
def rightp():
    phil.right(90)

def leftp():
    phil.left(90)

turtle.listen()
turtle.onkey(leftp, "a")
turtle.onkey(fdp, "w")
turtle.onkey(rightp, "d")


def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
    if distance < 20:
        return True
    else:
        return False
while True:
    if isCollision(bob, phil):
        bob.setposition(-50, 0)
        phil.setposition(50, 0)



turtle.done()

【问题讨论】:

  • 问题是你的'while True'函数基本上一直在运行。我们需要为您寻找一种方法,让您只有在鲍勃的位置发生变化后才能控制距离。

标签: python turtle-graphics


【解决方案1】:

您的原始代码和我在下面尝试解决的建议答案存在问题。具体来说:你不需要计算海龟之间的距离,它们已经知道该怎么做;您和其他人使用tracer() 毫无意义,因为您没有update() 电话; Bob 和 Phil 之间的碰撞应该是对称的,但其中一个答案认为 Bob 与 Phil 碰撞,但不是 Phil 与 Bob 碰撞;其中一个答案似乎不理解turtle.done() 的性质。

from turtle import Screen, Turtle, mainloop

def bob_left():
    bob.left(90)

def bob_forward():
    bob.forward(20)

    if bob.xcor() > 280:
        bob.setx(280)
    elif bob.xcor() < -280:
        bob.setx(-280)

    if bob.ycor() > 280:
        bob.sety(280)
    elif bob.ycor() < -280:
        bob.sety(-280)

    checkCollision()

def bob_right():
    bob.right(90)

def phil_left():
    phil.left(90)

def phil_forward():
    phil.forward(20)

    if phil.xcor() > 280:
        phil.setx(280)
    elif phil.xcor() < -280:
        phil.setx(-280)

    if phil.ycor() > 280:
        phil.sety(280)
    elif phil.ycor() < -280:
        phil.sety(-280)

    checkCollision()

def phil_right():
    phil.right(90)

def isCollision(t1, t2):
    return t1.distance(t2) < 20

def checkCollision():
    if isCollision(bob, phil):
        bob.setposition(-50, 0)
        phil.setposition(50, 0)

screen = Screen()
screen.bgcolor('black')
screen.setup(width=700, height=700)

border_pen = Turtle(visible=False)
border_pen.speed('fastest')
border_pen.color('white')
border_pen.pensize(3)

border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()

for _ in range(4):
    border_pen.fd(600)
    border_pen.lt(90)

bob = Turtle('triangle')
bob.speed('fastest')
bob.color('blue')
bob.penup()
bob.setpos(-50, 0)

phil = Turtle('triangle')
phil.speed('fastest')
phil.color('red')
phil.penup()
phil.setposition(50, 0)

screen.onkey(bob_left, 'Left')
screen.onkey(bob_forward, 'Up')
screen.onkey(bob_right, 'Right')

screen.onkey(phil_left, 'a')
screen.onkey(phil_forward, 'w')
screen.onkey(phil_right, 'd')
screen.listen()

mainloop()

【讨论】:

    【解决方案2】:

    经过审查,这应该可以工作:

    import turtle
    import math
    
    bob = turtle.Turtle()
    
    wn = turtle.Screen()
    wn.bgcolor("black")
    wn.setup(width = 700, height = 700)
    wn.tracer()
    
    border_pen = turtle.Turtle()
    border_pen.speed(0)
    border_pen.color("white")
    border_pen.penup()
    border_pen.setposition(-300,-300)
    border_pen.pendown()
    border_pen.pensize(3)
    
    for side in range(4):
        border_pen.fd(600)
        border_pen.lt(90)
    border_pen.hideturtle()
    
    bob.shape("triangle")
    bob.speed(0)
    bob.color("blue")
    bob.penup()
    bob.setpos(-50, 0)
    def draw():
        bob.penup()
    def nodraw():
        bob.pendown()
    
    def fd():
        bob.fd(20)
        if bob.xcor() > 280:
            bob.setx(280)
        if bob.ycor() > 280:
            bob.sety(280)
    
        if bob.xcor() < -280:
            bob.setx(-280)
        if bob.ycor() <- 280:
            bob.sety(-280)
        distance = math.sqrt(math.pow(bob.xcor()-phil.xcor(),2)+math.pow(bob.ycor()-phil.ycor(),2))
        print(distance)
        if distance < 20:
            bob.setposition(-50, 0)
            phil.setposition(50, 0)
    def right():
        bob.right(90)
    
    def left():
        bob.left(90)
    
    turtle.listen()
    turtle.onkey(left, "Left")
    turtle.onkey(fd, "Up")
    turtle.onkey(right, "Right")
    
    phil = turtle.Turtle()
    phil.speed(0)
    phil.penup()
    phil.shape("triangle")
    phil.color("red")
    phil.setpos(50, 0)
    
    def fdp():
        phil.fd(20)
        if phil.xcor() > 280:
            phil.setx(280)
        if phil.ycor() > 280:
            phil.sety(280)
    
        if phil.xcor() < -280:
            phil.setx(-280)
        if phil.ycor() <- 280:
            phil.sety(-280)
    def rightp():
        phil.right(90)
    
    def leftp():
        phil.left(90)
    
    turtle.listen()
    turtle.onkey(leftp, "a")
    turtle.onkey(fdp, "w")
    turtle.onkey(rightp, "d")
    
    turtle.done()
    

    【讨论】:

    • 这非常有效!我唯一要做的另一件事是将碰撞检查器放在 phil 和 bob 函数下。非常感谢!
    • 是的,stackoverflow 的新手,我的错。
    【解决方案3】:

    将turtle.done() 缩进到while 循环内。这将解决您的冻结问题。像这样:

    while True:
        if isCollision(bob, phil):
            bob.setposition(-50, 0)
            phil.setposition(50, 0)
        turtle.done()
    

    您也可以删除while 循环,因为您的if 语句并不真正需要它来工作。像这样:

    if isCollision(bob, phil):
        bob.setposition(-50, 0)
        phil.setposition(50, 0)
    turtle.done()
    

    【讨论】:

    • 第一部分停止了我的冻结问题是碰撞检测器似乎不起作用。而且,如果我错了,请纠正我,但如果我摆脱循环,那不是只允许玩一次吗?
    • 没有检查您的碰撞代码 tbh 但摆脱 while 循环不会影响游戏并会继续运行。我尝试了上面的两个代码,它们的工作方式相同。
    • 将turtle.done() 放在while 循环中毫无意义,因为turtle.done() 将控制权交给了tkinter 事件处理程序并且不会返回,从而结束循环。摆脱while 循环,无需其他修改来补偿,有效地阻止了这个程序做它打算做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多