[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] timing question



On Tue, 29 Jan 2008 11:06:06 +0100, "Miguel Sicart" <mundial82@xxxxxxxxx>
wrote:
> Hi all,
> I'm writing a little text-based game and I have stumbled upon a strange
> newbie problem.
> In this game, the player has to input a number of commands in order to
> interact with the game. I am using raw_input to get input from the
player.
> My problem is that I want to make it turn based, and I want to limit the
> amount of time players have per turn. And I don't know how to do that. I
> have tried to create an event and push it to the event list, but it
> doesn't
> act until the player provides input, so it is not working.


Others are offering actual code, so I'll chime in with the theory:

The "raw_input" function is a "blocking" function, meaning that it waits
for input and prevents anything else from happening. Because of that,
putting events onto the Pygame stack won't help because those events won't
get checked until after the user hits Enter. What the others are saying is
basically that instead of that function you can use a continuous loop that
checks whether a key has been hit and if so, reacts to it, otherwise just
going back into the loop. That's a "non-blocking" solution that lets you
check the Pygame event queue to do things like looping music and checking a
timer.

In Pygame there's a slightly different way to do that, loosely like so:
text_entered = ""
for event in pygame.event.get():
  if event.type == KEYDOWN:
    key = event.key
    ## Check for pressing of keys like Escape and Backspace.
    if key == K_ENTER:
      ## We're done.
    ## If the key is in the range of keys representing
letters/numbers/space:
      text_entered += character ## where character is the appropriate
letter etc.


Kris