Apr-17-2023, 02:22 PM
Hi! I'm a new member here on the forum. I was trying to make a turtle tag game and I was trying to find if the first turtle, p1, is touching the second turtle, p2. I tried to use the distance() function but it doesn't seem to work. Please help!
#variables
p1 = turtle.Turtle()
p2 = turtle.Turtle()
game = turtle.Turtle()
s = turtle.Screen()
turn = 0
won = 0
gameconsole = True
#turtle setup
p1.penup()
p2.penup()
game.penup()
game.hideturtle()
p1.color("blue")
p2.color("red")
#game setup not finished yet
game.goto(0, 200)
if turn == 0 and won == 0:
print("Blue is the tagger, Red is the runner.")
elif turn == 1 and won == 0:
print("Red is the tagger, Blue is the runner.")
#p1 keyboard events
def p1_forward():
p1.forward(20)
def p1_backward():
p1.backward(20)
def p1_left():
p1.left(90)
def p1_right():
p1.right(90)
#p2 keyboard events
def p2_forward():
p2.forward(20)
def p2_backward():
p2.backward(20)
def p2_left():
p2.left(90)
def p2_right():
p2.right(90)
#p1 keyboard inputs
s.onkey(p1_forward, "w")
s.onkey(p1_backward, "s")
s.onkey(p1_left, "a")
s.onkey(p1_right, "d")
#p2 keyboard inputs
s.onkey(p2_forward, "Up")
s.onkey(p2_backward, "Down")
s.onkey(p2_left, "Left")
s.onkey(p2_right, "Right")
#keyboard execution
s.listen()
s.mainloop()
#p1 touch detection
while gameconsole == True:
if p1.distance(p2) < 15:
game.clear()
print("Blue won!")
