So I'm coding a tron game in turtle and the last part I need to do is to code the collisions between the turtles themselves, out of bound collisions, and collisions between the line made by the a turtle and the other turtle. I coded the out of bound collisions and they didn't work, and I also tried coding the player against player collisions which didn't work either. I've got no clue how to do the other one (where it outputs something when the turtle collides with the line generated by the other turtle). I thought of using an array in some way but I'm not sure how those work. Can anyone help me out?
Code for the out of bound collisions:
#Width and height of the screen
width = turtle.window_width()
height = turtle.window_height()
def collisions():
while True:
if(math.isclose(blue_player.xcor(), red_player.xcor(), abs_tol=1e-10) and
math.isclose(blue_player.ycor(), red_player.ycor(), abs_tol=1e-10)):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write("Red and Blue Crashed!\nGame over!", font=style, align='center')
break
if blue_player.xcor() > width or blue_player.xcor() < -1*width:
BlueTurtleOut = turtle.Turtle(visible=False)
BlueTurtleOut.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if blue_player.ycor() > height or blue_player.ycor() < -1*height:
BlueTurtleOut2 = turtle.Turtle(visible=False)
BlueTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.xcor() > width or red_player.xcor() < -1*width:
RedTurtleOut = turtle.Turtle(visible=False)
RedTurtleOut.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.ycor() > height or red_player.ycor() < -1*height:
RedTurtleOut2 = turtle.Turtle(visible=False)
RedTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
gameScreen()
gameScreen is a function called earlier in my code by the way, it's not the issue, although it could be in the wrong place in the function. I'm not sure.
Code for (attempted) collision between the turtles where nobody wins.
collision = turtle.Turtle()
collision.hideturtle()
if blue_player.pos() == red_player.pos():
collision.goto(0,0)
collision.showturtle()
collision.write("You collided with each other!\nIt's a tie!")
Here's the full code:
#Width and height of the screen
width = turtle.window_width()
height = turtle.window_height()
#Variables for tron game coded later on
red_direction = None
blue_direction = None
def TronGame():
#Border
#box = Turtle()
#box.ht()
#box.color('purple')
#box.speed('fastest')
#box.pensize(10)
# box.pu()
# box.goto(-1*height, -1*width)
# box.pd()
# for i in range(4):
# box.forward(height)
# box.left(90)
# box.forward(width)
# box.left(90)
#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)
def gameloop():
move_player(red_player, red_direction)
move_player(blue_player, blue_direction)
#Repeat after 10ms (0.01s) (1000ms/10ms = 100 FPS)
screen.ontimer(gameloop, 10)
def collisions():
while True:
if(math.isclose(blue_player.xcor(), red_player.xcor(), abs_tol=1e-10) and
math.isclose(blue_player.ycor(), red_player.ycor(), abs_tol=1e-10)):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write("Red and Blue Crashed!\nGame over!", font=style, align='center')
break
if blue_player.xcor() > width or blue_player.xcor() < -1*width:
BlueTurtleOut = turtle.Turtle(visible=False)
BlueTurtleOut.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if blue_player.ycor() > height or blue_player.ycor() < -1*height:
BlueTurtleOut2 = turtle.Turtle(visible=False)
BlueTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.xcor() > width or red_player.xcor() < -1*width:
RedTurtleOut = turtle.Turtle(visible=False)
RedTurtleOut.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.ycor() > height or red_player.ycor() < -1*height:
RedTurtleOut2 = turtle.Turtle(visible=False)
RedTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
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(10):
my_turtle = turtle.Turtle(visible=False)
my_turtle.color("white")
style = ('Arial', 25, 'italic')
my_turtle.write(10-x, font=style, align='center')
time.sleep(1)
my_turtle.undo()
screen.listen()
#collisions()
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")
screen.ontimer(gameloop, 250)
#collision = turtle.Turtle()
#collision.hideturtle()
#if blue_player.pos() == red_player.pos():
# collision.goto(0,0)
# collision.showturtle()
# collision.write("You collided with each other!\nIt's a tie!")
screen.mainloop()
MainTron()