# My code makes a tron-like game in turtle graphics but I'm missing a key collision [closed]

So I'm doing the last part of my tron game which is to code the collision that happens when the 2nd players' tron bike hits the line of the 1st players tron bike. From there, whoever ran into the line loses, and the other one wins. So my issue is that I'm not 100% sure on how to code this, but I've got an idea and I'm wondering if anyone can help.

What I was thinking was to make myself 4 definitions:

• 1 for the x coordinate of bike 1
• 1 for the y coordinate of bike 1
• 1 for the x coordinate of bike 2
• 1 for the y coordinate of bike 2

In each of these definitions there would be a while loop constantly updating a list with all of the coordinates, kind of like this:

def RedX():
RedXCordList = [red_player.xcord()]
while True:
RedXCordList.append(red_player.xcord())


I'm not sure if that's how that works in the first place, but the idea is to get the coordinates into a list/array somehow.

After getting the coordinates into there I'd have to check if one of the bikes positions was equal to any of the coordinates in the list/array, and if it was then there would be a collision. If not the it would continue running until something collided.

I've got barely any clue on how to code that help would be greatly appreciated.

Here's my full code if you want to look at it:

def TronGame():
#Blue Player movements
def blue_up():
global blue_direction
blue_direction = 'up'

def blue_down():
global blue_direction
blue_direction = 'down'

def blue_left():
global blue_direction
blue_direction = 'left'

def blue_right():
global blue_direction
blue_direction = 'right'

#Red player Movemnts
def red_up():
global red_direction
red_direction = 'up'

def red_down():
global red_direction
red_direction = 'down'

def red_left():
global red_direction
red_direction = 'left'

def red_right():
global red_direction
red_direction = 'right'

#Player movements
def move_player(player, direction):
if direction == 'up':
player.forward(5)
elif direction == 'down':
player.forward(5)
elif direction == 'left':
player.forward(5)
elif direction == 'right':
player.forward(5)

# Helper function to print end of game message
def game_over(message):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write(f"{message}\nGame over!", font=style, align='center')

def collisions():
x_blue, y_blue = blue_player.xcor(), blue_player.ycor()
x_red, y_red  = red_player.xcor(), red_player.ycor()
message_turtle =  turtle.Turtle(visible=False)
message_turtle.color("white")
collision_text = ""
if( math.isclose(x_blue, x_red, abs_tol=10.0) and
math.isclose(y_blue, y_red, abs_tol=10.0)):
collision_text = "Red and Blue Crashed!\nGame over!"
if x_blue > width/2 or x_blue < -1*width/2:
collision_text = "Blue went out of bounds./nRed wins!"
if y_blue > height/2 or y_blue < -1*height/2:
collision_text = "Blue went out of bounds./nRed wins!"
if x_red > width/2 or x_red < -1*width/2:
collision_text = "Red went out of bounds./nBlue wins!"
if y_red > height/2 or y_red < -1*height/2:
collision_text = "Red went out of bounds./nBlue wins!"
if collision_text:
message_turtle.write(collision_text,font=('Arial', 25, 'italic'), align='center')

return False

def MainTron():
global screen
global blue_player
global red_player

screen = turtle.Screen()
screen.setup(width, height)
screen.bgpic('TronBg.png')
screen.bgcolor('black')

blue_player = turtle.Turtle()
blue_player.shape('BlueBike.gif')
blue_player.pencolor("blue")
blue_player.pensize(3)
blue_player.pu()
blue_player.goto(-1*(width)/3, height/8)
blue_player.pd()

red_player = turtle.Turtle()
red_player.shape('RedBike.gif')
red_player.pencolor("red")
red_player.pensize(3)
red_player.pu()
red_player.goto(width/3, height/8)
red_player.pd()

for x in range(5):
my_turtle = turtle.Turtle(visible=False)
my_turtle.color("white")
style = ('Arial', 25, 'italic')
my_turtle.write(5-x, font=style, align='center')
time.sleep(1)
my_turtle.undo()

screen.listen()

screen.onkey(red_up, "w")
screen.onkey(red_down, "s")
screen.onkey(red_left, "a")
screen.onkey(red_right, "d")

screen.onkey(blue_up, "Up")
screen.onkey(blue_down, "Down")
screen.onkey(blue_left, "Left")
screen.onkey(blue_right, "Right")

def gameloop():
move_player(red_player, red_direction)
move_player(blue_player, blue_direction)
# Check for a collision. End game if there is one
if collisions():
return
#Repeat after 16ms (0.016s) (1000ms/16ms = 62.5 FPS)
screen.ontimer(gameloop, 16)

screen.ontimer(gameloop, 250)

gameloop()
screen.mainloop()

MainTron()

TronGame()

• Code Review aims to help improve working code. If you are trying to figure out why your program crashes or produces a wrong result, ask on Stack Overflow instead. Code Review is also not the place to ask for implementing new features. Jun 21 at 13:14
• That being said, however, I'd suggest adding the current co-ordinates to the list you think of in your 'move_player' function. The basic strategy is sound, while inefficient in terms of storage space recording a bunch of redundant coordinates takes. If you want to go down that route, it's easier to have a 2D list (a list of lists) shadow your game board, all positions set to 0 initially, and move_player sets each position passed through by red to 1 and by blue to 2 - returning False if the initial value at that position was 0 or True if it was already 1 or 2. [...] Jun 21 at 13:20
• [...] Then you just pass the return value from move_player into collisions, and deal with it there. collisions would accept 2 boolean parameters of the form red_move_failed, blue_move_failed. Jun 21 at 13:23