Most of this code works, but when I press 'space bar' the bullet should fire and move along with the alien. Instead the bullet moves and the alien just stops in its tracks as if the enemy_move()
function had been stopped when the shoot()
function started. If you have any idea how to make them work at the same time that would be great.
bulletState = TRUE
bulletHealth = 0
enemyHealth = 100
def SpaceInvaders():
window.destroy()
SpaInv=Tk()
s = Canvas(SpaInv, height=HEIGHT, width=WIDTH, bg='black')
s.pack()
ship = s.create_polygon(590, 485, 630, 485, 610, 430, fill='red')
bullet = s.create_oval(600, 425, 620, 400, fill='yellow', state=HIDDEN)
enemy = s.create_rectangle(4, 4, 34, 34, fill = 'green')
def enemy_move():
global enemyHealth
while enemyHealth > 0:
pos = s.coords(enemy)
s.move(enemy, 3, 0)
time.sleep(0.01)
SpaInv.update()
def shoot(event):
global bulletHealth
global WIDTH
global bulletState
s.itemconfig(bullet, state=NORMAL)
bulletState = FALSE
while bulletHealth == 0:
s.move(bullet, 0, -3)
time.sleep(0.01)
SpaInv.update()
s.bind_all('<Key>', move_ship)
s.bind_all('<space>', shoot)
enemy_move()