当我运行Python Turtle游戏时,“图形窗口”停止响应

时间:2019-05-01 22:33:38

标签: python turtle-graphics

我一般对乌龟和编程都不熟悉,所以想制作一个标签游戏。有两个用户,一个是奔跑者,一个是追逐者(鲍勃和菲尔)。当追赶者靠近跑步者(使用距离公式检测到)时,程序会将其重新设置为原始开始位置。但是,每当我运行该程序时,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()

3 个答案:

答案 0 :(得分:0)

经过审查,这应该可行:

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

答案 1 :(得分:0)

使您的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()

答案 2 :(得分:0)

您的原始代码和我在下面尝试解决的建议答案存在问题。具体来说:您无需计算乌龟之间的距离,因为它们已经知道该怎么做;您和其他人使用if (message.author.bot) return;没有意义,因为您没有tracer()的电话;鲍勃和菲尔之间的碰撞应该是对称的,但答案之一是鲍勃与菲尔相撞,而不是菲尔与鲍勃相撞。答案之一似乎并不了解update()的本质。

turtle.done()