In an event-driven game, you need to give control back to the event
loop in order for the framework to pass events to your program.  Your
main function, however, doesn't do so: you've got a while loop that
monopolizes control flow:

def game():
    # ... game initialization logic
    while playing:
        # ... loop logic
        time.sleep(speed)

This gives no opportunity to process keyboard events, since the
program is stuck within the while loop.


To resolve this, you want to set up a timer which emits a "tick" event
every so often.  Essentially, if you're using a Tkinter-based
framework, you're looking for the "after" method:

    http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method

where your program will look something like this:

#################################
def game():
    # ... game initialization logic
    gameLoop();

def gameLoop():
    if playing:
        # ... loop logic
        win.after(speed, gameLoop)
#################################


The gameLoop tells the framework to restart the game loop "after" some
time.  gameLoop() then returns, and gives control back to the event
loop that was started with "win.mainloop()".
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to