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
#2
The mainloop() function is blocking. It doesn't finish until you close the window. Your "tag" loop does not run until after you close the window.

The only time a collision can happen is when the turtle moves, and the turtle only moves forward or backward. Turning left or right does not change the position of the turtle, only the heading. So the solution is to modify the forward and backward moves so they check for a collision.
import turtle


def move_forward(player):
    """Move player turtle and check for collision."""
    player.forward(20)
    if p1.distance(p2) < 15:
        game.clear()
        print(f"{player.name} won!")


def move_backward(player):
    """Move player turtle and check for collision."""
    player.backward(20)
    if p1.distance(p2) < 15:
        game.clear()
        print(f"{player.name} won!")


def player(color, f, b, l, r):
    """Make a player turtle."""
    player = turtle.Turtle()
    player.color(color)
    player.name = color.title()
    s.onkey(lambda: move_forward(player), f)
    s.onkey(lambda: move_backward(player), b)
    s.onkey(lambda: player.left(90), l)
    s.onkey(lambda: player.right(90), r)
    player.penup()
    return player


game = turtle.Turtle()
s = turtle.Screen()
p1 = player("blue", "w", "s", "a", "d")
p2 = player("red", "Up", "Down", "Left", "Right")
game.penup()
game.hideturtle()

s.listen()
s.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 11,400 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,191 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,651 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