I'm working Turtle module game based on Asteroids and I have most of the code done. I just need to figure out how to make turtle objects reappear on the opposite side on the border when it collides with it. I made them turn 180 degrees when then collide until I figure out a way. I know I'm supposed to check for change in Xcor and Ycor, but other then that I'm a but lost honestly.
Edit: To clarify, I'm looking to have the objects like the player and asteroids continue to wrap around the main window when they cross the border
import math
import random
import turtle
import winsound
# screen setup
scr = turtle.Screen()
scr.setup(600, 600, 0, 0)
scr.bgpic('space_bg.gif')
scr.tracer(2)
speed = 1 # global movement speed
# Drawing a border around the canvas
border = turtle.Turtle()
border.penup()
border.setpos(-250, -250)
border.pendown()
border.pencolor('yellow')
border.pensize(3)
for side in range(4):
border.forward(500)
border.lt(90)
border.hideturtle()
# create points on upper left screen
points = 0
# creating player object
player = turtle.Turtle()
player.color('white')
player.penup()
player.speed(1)
turtle.register_shape('cursor', ((5, -15), (0, 5), (-5, -15)))
player.shape('cursor')
# create asteroids(random amount between 5-6)
max_asteroids = random.randint(5, 6)
asteroids = []
for i in range(max_asteroids):
asteroids.append(turtle.Turtle())
asteroids[i].color('red')
asteroids[i].shape('circle')
asteroids[i].penup()
asteroids[i].speed(1)
asteroids[i].setpos(random.randint(-240, 240), random.randint(-240, 240))
# creating black holes
max_blackholes = random.randint(3, 4)
blackhole = []
for i in range(max_blackholes):
blackhole.append(turtle.Turtle())
blackhole[i].color('green')
blackhole[i].shape('circle')
blackhole[i].fillcolor('black')
blackhole[i].penup()
blackhole[i].setpos(random.randint(-240, 240), random.randint(-240, 240))
# movement functions when player turns left
def left_turn():
player.lt(30)
# movement functions when player turns right
def right_turn():
player.rt(30)
# Increasing movement speed
def speed_up():
global speed
speed += 1
# Decreasing movement speed
def slow_down():
global speed
speed -= 1
# Collision with asteroid
def aster_collide(obj1, obj2):
distance = math.sqrt(math.pow(obj1.xcor() - obj2.xcor(), 2) + math.pow(obj1.ycor() - obj2.ycor(), 2))
if distance < 20:
return True
else:
return False
def hyperspace(obj1, obj2): # collision with black hole
distance = math.sqrt(math.pow(obj1.xcor() - obj2.xcor(), 2) + math.pow(obj1.ycor() - obj2.ycor(), 2))
if distance < 20:
return True
else:
return False
# set keyboard bindings
turtle.listen()
turtle.onkey(left_turn, "Left")
turtle.onkey(right_turn, "Right")
turtle.onkey(speed_up, "Up")
turtle.onkey(slow_down, "Down")
while True:
player.fd(speed)
# boundry collision check for player
if player.xcor() > 248 or player.xcor() < -248:
player.right(180)
if player.ycor() > 248 or player.ycor() < -248:
player.right(180)
# boundary checking for asteroids
for i in range(max_asteroids):
asteroids[i].fd(1) # move the asteroid
if asteroids[i].xcor() > 240 or asteroids[i].xcor() < -240:
asteroids[i].right(180)
if asteroids[i].ycor() > 240 or asteroids[i].ycor() < -240:
asteroids[i].right(180)
# if player collides with asteroid
if aster_collide(player, asteroids[i]):
asteroids[i].setpos(random.randint(-240, 240), random.randint(-240, 240))
# winsound.PlaySound('collision_sound.wav', winsound.SND_NOWAIT)
points += 1
# Adding score to top of the screen
border.undo()
border.penup()
border.hideturtle()
border.setpos(-230, 260)
scores = "Score: %s" % points
border.write(scores, False, align="left", font=("Arial", 14, "normal"))
winsound.PlaySound('point_up.wav', winsound.SND_ASYNC)
for count in range(max_blackholes):
if hyperspace(player, blackhole[count]):
player.setpos(random.randint(-240, 240), random.randint(-240, 240))
# boundry collision check for player
if player.xcor() > 248 or player.xcor() < -248:
player.right(180)
if player.ycor() > 248 or player.ycor() < -248:
player.right(180)