How do I add collision between two player controlled turtles. I asked the question before but misunderstood my own groundings on the plan. I cannot figure out how to make two player controlled turtles in the game to hit each other and end the game. This is what I have so far: import turtle import pygame import math import string

wn = turtle.Screen() wn.bgcolor("black")

Write Tron

text=turtle.Pen() text.pencolor("aqua") text.hideturtle() text.penup() text.setposition(150, 300) text.write("TRON", font=("system", 30))

Draw border

mypen = turtle.Turtle() mypen.penup() mypen.pencolor('white') mypen.setposition(-300,-300) mypen.pendown() mypen.pensize(3) for side in range (4): mypen.forward(600) mypen.left(90) mypen.hideturtle()


Create player 1

player = turtle.Turtle() player.setposition(240,240) player.setheading(180) player.color("red") player.shape("triangle")


Create player 2

player2 = turtle.Turtle() player2.setposition(-240,-240) player2.color("aqua") player2.shape("triangle") player2.position() player2.speed(0)

Set speed variable

speed = 3

Define functions

def turnleft(): player.left(30)

def turnright(): player.right(30)

def increasespeed(): global speed speed += 3

Set keyboard bindings for p1ayer 1

turtle.listen() turtle.onkey(turnleft,"Left") turtle.onkey(turnright,"Right")

Define player 2 functions

def turnleft(): player2.left(30)

def turnright(): player2.right(30)

def increasespeed(): global speed speed += 3

Set keyboard bindings for player 2

turtle.listen() turtle.onkey(turnleft,"a") turtle.onkey(turnright,"d") turtle.onkey(turnleft,"A") turtle.onkey(turnright,"D")


while True: player.forward(speed) player2.forward(speed)

if player.xcor() > 300 or player.xcor() < -300:
    print("GAME OVER")
if player.ycor() > 300 or player.ycor() < -300:
    print("GAME OVER")
if player2.xcor() > 300 or player2.xcor() < -300:
    print("GAME OVER")
if player2.ycor() > 300 or player2.ycor() < -300:
    print("GAME OVER")


while True: if player.setposition(player.ycor()+10, player.xcor()+10) and player2.setposition(player2.xcor()+10, player2.ycor()+10): quit


1 Answer 1


The code formatting for your answer has mostly broken down, it would be a lot easier to help you if you fixed it.

What does it mean for turtles to collide? It means that they occupy the same space, so the easiest thing to do is:

if player.position() == player2.position():
    print("GAME OVER")

however, this doesn't quite work because turtle keeps the position as a floating point, so the turtles could be separated by a distance of 0.00000001 and count as not colliding, when anyone would think they would do. One way to get around this is to use math.is_close. A minimal example of this is:

import turtle
import math
import time

player_1 = turtle.Turtle()
player_1.setposition((-100, 0))

player_2 = turtle.Turtle()
player_2.setposition((100, 0))

while True:

    if(math.isclose(player_1.xcor(), player_2.xcor(), abs_tol=1e-10) and
       math.isclose(player_1.ycor(), player_2.ycor(), abs_tol=1e-10)):
        print("GAME OVER")

    if player_1.xcor() > 300 or player_1.xcor() < -300:
        print("GAME OVER")

    if player_1.ycor() > 300 or player_1.ycor() < -300:
        print("GAME OVER")

    if player_2.xcor() > 300 or player_2.xcor() < -300:
        print("GAME OVER")

    if player_2.ycor() > 300 or player_2.ycor() < -300:
        print("GAME OVER")

print("Thanks for playing!")

Note that if you increase the argument in forward() then this doesn't work. That's because the turtles then "jump" from one position to the other and collisions are only checked in between jumps. It should not be complicated to modify the basic idea here to make that work however.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .