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

Re: [pygame] timing question



Yes, the Pygame event is exactly that and using the K_ codes for checks. I
also added this other function as well which uses the windows version and is
just a little more in the check using a dictionary list for inserting
commands. This time inside I use a GG method variable to store the
dictionary. That dictionary is the key-code first like the K_ command codes
for the keyboard. It first checks to see if it is on the list with the IN
command then places the equivalent command from the dictionary into the
resulting return code you want.

    The 2 control codes for all keyboard command is either a 0 which is a
null or a 224 code for most extended keyboard commands. In the other example
I gave you are also printed the keycodes the keyboard has and can be
commented out.
    But when pressing the key and getting the event triggered, you will get
either a keycode, control code of 0 or 224 and just check for that.

    For in the extended keyboard keys there are 2 characters sent out from
the keyboard. Where the first key is the 0 or 224 and a check must be done
for that. If neither is present then it will send only one key back. I had a
routine that checked for that then passed them into the routine below as 2
keys as present inside the keyboard buffer. But you would have to build your
own or just do a test. So the routine below is passing in either a string of
one char or a string of 2 chars based on the key pressed on the keyboard.

    Once that is observed then place your dictionary substitute into the
result as I have done below.
    The dictionary has the format: {"O", "MRS"} and the "O" is the code for
the End Of Line or EOL key on the extended keyboard. You can build your own
list using all the key codes of the K_ list.

    So make any list you want and the rest is as he mentions below. The
keyboard keydown event and call the method I suggested.

        Bruce


def key2Com(GG, kbs):
    "CONVERT THE 0 AND 224 KEY CODES TO COMMANDS!"
    if len(kbs) < 2: return(kbs)
    if kbs[0] == chr(0):
        if kbs[1] == "O": kbs = "CMRS"
        elif kbs[1] == "Q": kbs = "CLRS"
    if kbs[0] == chr(224) and kbs[1] in GG.MKS224: kbs = GG.MKS224[ kbs[1]]
    return (kbs)


----- Original Message -----
From: "kschnee" <kschnee@xxxxxxxxxx>
To: <pygame-users@xxxxxxxx>
Sent: Tuesday, January 29, 2008 11:54 AM
Subject: 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