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.
Alternatively, Someone can tell me how to detect color and whenever turtle detects the color red or blue it will detect a collision and one will win and one will lose.
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.setheading(90)
player.forward(5)
elif direction == 'down':
player.setheading(270)
player.forward(5)
elif direction == 'left':
player.setheading(180)
player.forward(5)
elif direction == 'right':
player.setheading(0)
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')
screen.addshape('BlueBike.gif')
screen.addshape('RedBike.gif')
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()