I am trying to make a space invaders game using turtle, and in order to move the bullet up the screen I am trying to update the y coordinate of the bullet every 0.5 seconds. I tried to do this with a while loop and the time.sleep() function, but everytime I try to fire a bullet, the program crashes. Is there any way I can stop it from doing this? Or why is it not working? Is there an equally effective method that will not crash my program?
import turtle
import time
userx = 0
x = 0
y = -300
enemies = [(300,50, "left"), (400,50, "left"), (350, -50, "right")]
bullets = []
gameover = False
def frame():
pass
ship = turtle.Turtle()
ship.pu()
ship.ht()
ship.setpos(userx, -300)
ship.pd()
ship.circle(5)
bullet = turtle.Turtle()
bullet.pu()
bullet.ht()
bullet.setpos(x, y)
bullet.pd()
bullet.circle(2)
def key_left():
global userx
pass
userx += -10
print(userx)
def key_right():
global userx
pass
userx += 10
print(userx)
def key_space():
pass # your code here
global x
global y
global bullets
x = userx
while y <= 300:
time.sleep(0.5)
y += 4
else: y = 0
bullets += (x,y)
def physics():
global bullets
global enemies
pass
def ai():
global enemies
global gameover
pass
def reset():
global enemies
global bullets
global userx
global gameover
pass
def main():
turtle.tracer(0,0)
turtle.hideturtle()
turtle.onkey(key_left, "Left")
turtle.onkey(key_right, "Right")
turtle.onkey(key_space, "space")
turtle.listen()
reset()
while not gameover:
turtle.clear()
physics()
ai()
frame()
turtle.update()
time.sleep(0.05)
main()