Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Detection
#1
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!")
Reply


Messages In This Thread
Turtle Detection - by ChinaPlaneFlyer - Apr-17-2023, 02:22 PM
RE: Turtle Detection - by deanhystad - Apr-17-2023, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 11,510 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,213 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,682 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020