使用“if”语句检测两个海龟的碰撞

2024-03-29 15:06:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创造一个游戏,一只海龟被关在一个盒子里。一个球在盒子周围反弹,如果它与乌龟相撞,或者乌龟碰到盒子的边缘,游戏就结束了。一个红点在盒子里的一个随机位置产卵,当海龟跑过这个红点时,这个红点应该会消失并在其他地方重生。然而,这只是有时起作用。有时,这个点会随机消失,并在其他地方产卵,即使海龟甚至不接近它。我怎样才能解决这个问题?你知道吗

from turtle import Screen, Turtle, mainloop

wn = Screen()
wn.bgcolor("light blue")



#CONTROLLABLE TURTLE
t = Turtle()
t.color('black')
t.pensize(10)
t.shape("turtle")
t.speed("fastest")
t.penup()
t.goto(-130,-200)
t.pendown()

global hit
global score
hit = False
score = 0

#MAKE SQUARE
for i in range(4):
    t.forward(400)
    t.left(90)


t.penup()
t.goto(50,0)
speed = 2
t.color("black")

#BALL
ball1 = turtle.Turtle()
ball1.color("blue")
ball1.speed(0)
ball1.penup()
ball1.shape("circle")




A = random.randint(30,60)
B = random.randint(120,150)
C = random.randint(210,240)
D = random.randint(300,330)
Directions = [A, B, C, D]
direct = random.choice(Directions)
def tDirection(direct):
  ball1.right(direct)
tDirection(direct)
angle = 90

#DOT TURTLE
dot = turtle.Turtle()
dot.color("black")
dot.speed(0)
dot.hideturtle()

def createDot():
    dotx = random.randint(-10,230)
    doty = random.randint(-160,200)
    dot.penup()
    dot.goto(dotx,doty)
    dot.pendown()
    dot.pensize(3)
    dot.fillcolor("Red")
    dot.begin_fill()
    dot.circle(7)
    dot.end_fill()

createDot()


#make score function

while True:
  if hit == False:

    #moving ball
    ty = ball1.ycor()
    tx = ball1.xcor()
    if ty < -183:
      angleCurr = ball1.heading()
      if(270>angleCurr>180):
        ball1.right(angle)
      else:
        ball1.left(angle)

      ball1.forward(2)
    elif ty > 185:
      angleCurr = ball1.heading()
      if(0<angleCurr<90):
        ball1.right(angle)
      else:
        ball1.left(angle)

      ball1.forward(2)
    elif tx < -115:
      angleCurr = ball1.heading()
      if(180<angleCurr<270):
        ball1.left(angle)
      else:
        ball1.right(angle)

      ball1.forward(2)
    elif tx > 251:
      angleCurr = ball1.heading()
      if(0<angleCurr<90):
        ball1.left(angle)
      else:
        ball1.right(angle)

    ball1.forward(9)  

    wn.onkey(lambda: t.setheading(180), 'Left')
    wn.onkey(lambda: t.setheading(0), 'Right')
    wn.onkey(lambda: t.setheading(90), 'Up')
    wn.onkey(lambda: t.setheading(270), 'Down')



    w = turtle.Turtle()
    w.hideturtle()
    w.penup()
    w.speed("fastest")

    def end():
      w.goto(-95,-20)
      w.pendown()
      w.write("GAME OVER", font=("Arial", 40, "normal")) 
      t.hideturtle()
      ball1.hideturtle() 
      dot.hideturtle()
      dot.clear()

    speed = 2

    def turtleMove():
        t.forward(speed)
        wn.ontimer(turtleMove, 10)
    dodx = dot.xcor()
    dody = dot.ycor()
    if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
      hit = True
      dot.clear()
    elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
      end()

    if t.xcor() > 253 or t.xcor() < -115 or t.ycor() > 185 or t.ycor() < -183:
      end()

    turtleMove()
    wn.mainloop()
    wn.listen()


  if hit == True:
    createDot()
    score+=1
    print(score)
    hit = False

Tags: ifrandomdotforwardspeedrandintturtlehit
2条回答

我想可能是打字错误:

if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:

碰撞检测是与y坐标进行比较,而不是像对x坐标那样进行相减。你知道吗

所以这应该可以解决:

if abs(t.xcor() - dodx) < 5 and abs(t.ycor() - dody) < 5:

我会避免这样的代码:

dodx = dot.xcor()
dody = dot.ycor()
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
    hit = True
    dot.clear()
elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
    end()

而是使用turtle的distance()方法:

if t.distance(dot) < 5:
    hit = True
    dot.clear()
elif t.distance(ball1) < 5:
    end()

However, this only works sometimes. Sometimes the dot will randomly disappear and spawn somewhere else even though the turtle isn't even close to it.

你在假装这个代码运行吗?由于两个不同的import问题,它甚至无法启动。修好这些,海龟控制根本不起作用。而且球不会在箱子周围弹跳!你知道吗

这似乎是一个随机收集的位代码借用其他程序补丁在一起一厢情愿。它根本不起作用。你知道吗

下面我把你的代码拆开,重新组合起来,使它基本上可以运行。蓝色的球会在盒子周围弹跳;你可以用箭头键移动海龟;当海龟碰到红点时,红点会消失并重新出现在其他地方:

from turtle import Screen, Turtle
from random import randint, choice

CURSOR_SIZE = 20

def tDirection(direct):
    ball.right(direct)

def turtleMove():
    turtle.forward(speed)
    screen.ontimer(turtleMove, 10)

def createDot():
    x, y = randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE), randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE)
    dot.goto(x, y)

def end():
    marker.write("GAME OVER", align='center', font=("Arial", 40, "normal"))

    turtle.hideturtle()
    ball.hideturtle()
    dot.hideturtle()

screen = Screen()
screen.bgcolor("light blue")

score = 0
speed = 2

# CONTROLLABLE TURTLE
turtle = Turtle("turtle")
turtle.color('black')
turtle.pensize(10)
turtle.speed("fastest")
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()

# MAKE SQUARE
for i in range(4):
    turtle.forward(400)
    turtle.left(90)

turtle.penup()
turtle.goto(50, 0)

# BALL
ball = Turtle("circle")
ball.color("blue")
ball.speed('fastest')
ball.penup()

A = randint(30, 60)
B = randint(120, 150)
C = randint(210, 240)
D = randint(300, 330)

directions = [A, B, C, D]
direct = choice(directions)

tDirection(direct)
angle = 90

# DOT TURTLE
dot = Turtle('circle')
dot.color("red")
dot.speed('fastest')
dot.pensize(3)
dot.penup()

createDot()

marker = Turtle(visible=False)
marker.penup()
marker.sety(-20)

screen.onkey(lambda: turtle.setheading(0), 'Right')
screen.onkey(lambda: turtle.setheading(90), 'Up')
screen.onkey(lambda: turtle.setheading(180), 'Left')
screen.onkey(lambda: turtle.setheading(270), 'Down')
screen.listen()

turtleMove()

while True:
    # moving ball
    tx, ty = ball.position()

    if ty < CURSOR_SIZE - 200:
        angleCurr = ball.heading()

        if 270 > angleCurr > 180:
            ball.right(angle)
        else:
            ball.left(angle)

        ball.forward(2)
    elif ty > 200 - CURSOR_SIZE:
        angleCurr = ball.heading()

        if 0 < angleCurr < 90:
            ball.right(angle)
        else:
            ball.left(angle)

        ball.forward(2)
    elif tx < CURSOR_SIZE - 200:
        angleCurr = ball.heading()

        if 180 < angleCurr < 270:
            ball.left(angle)
        else:
            ball.right(angle)

        ball.forward(2)
    elif tx > 200 - CURSOR_SIZE:
        angleCurr = ball.heading()

        if 0 < angleCurr < 90:
            ball.left(angle)
        else:
            ball.right(angle)

        ball.forward(2)

    ball.forward(9)

    if turtle.distance(dot) < CURSOR_SIZE/2:
        score += 1
        dot.hideturtle()
        createDot()
        dot.showturtle()
    elif turtle.distance(ball) < CURSOR_SIZE/2:
        end()

    if not CURSOR_SIZE - 200 < turtle.xcor() < 200 - CURSOR_SIZE or not CURSOR_SIZE - 200 < turtle.ycor() < 200 - CURSOR_SIZE:
        end()

screen.mainloop()

当你请求帮助时,请诚实的说出你的代码状态。你知道吗

上面的代码还不完整,还有工作要做。例如,while True:循环应转换为函数和ontimer()事件;分数需要显示;游戏应可重新启动。你知道吗

相关问题 更多 >