0

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()
CR9GAMING
  • 13
  • 3
  • Welcome to Stackoverflow. First before you post anything here you should read this [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) –  Apr 04 '19 at 14:34
  • Read [How do you run your own code alongside Tkinter's event loop?](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) – stovfl Apr 04 '19 at 15:00
  • Again welcome to Stackoverflow. Although you provide a lot of interesting information in your question much of it does not pertain to what is being asked. It is ok to be new to programming but knowing that you are new probably does not help us write a better answer. We also don't always need to know why you are asking. See if you can edit the question and simply describe what you are asking and then show us what you have tried and describe what is going wrong. A good question much like good code often takes several edits to get it right. Don't give up. – Charlie Wallace Apr 04 '19 at 15:10

1 Answers1

0

Below is a highly simplified rework of your example code that allows the alien and bullet to move at the same time. The solution is not to write a loop to do the entire movement but rather a function that does a small bit of the move and then calls itself via after():

from tkinter import *

WIDTH, HEIGHT = 900, 600

def enemy_move():
    s.move(enemy, 3, 0)
    SpaInv.after(10, enemy_move)

def shoot(event=None):
    s.itemconfig(bullet, state=NORMAL)
    s.move(bullet, 0, -3)
    SpaInv.after(10, shoot)

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')

s.bind_all('<space>', shoot)

enemy_move()

SpaInv.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81