How to efficiently hold a key in Pygame? - python

I've found two related questions:
Pygame hold key down causes an infinite loop
pygame - on hold button down
But I want to be specific. How to?
while not done:
for e in event.get():
if e.type == KEYDOWN:
keys = key.get_pressed()
if e.type == QUIT or keys[K_ESCAPE]:
done = True
if keys[K_DOWN]:
print "DOWN"
When I press the down arrow, it prints, but it prints just once. If I want to print it another time, I need to press it again.
If I use the while keyword instead,
while keys[K_DOWN]:
print "DOWN"
I get an infinite loop for some obscure reason.
This logical alternative is also useless:
if ((e.type == KEYDOWN) and keys[K_DOWN]):
print "DOWN"
And there is this other one that somehow cleans the events and you can use while:
while not done:
for e in event.get():
if e.type == KEYDOWN:
keys = key.get_pressed()
if e.type == QUIT or keys[K_ESCAPE]:
done = True
while keys[K_DOWN]:
print "DOWN"
event.get()
keys = key.get_pressed()
But you press the down key for less than one second and it prints thousands of times. (Moving a player would be impossible, and adjusting clock for this does not seem to be the right way to deal with it (And I've tried and I've failed miserably.)).
To press and execute the block thousands of times is useless. What I want, is to press the key and keep going with the action while I don't release it, within the defined game clock speed.

Don't mix up event.get() and key.get_pressed().
If you press or release a key, and event is put into the event queue, which you query with event.get(). Do this if you're actually interested if a key was pressed down physically or released (these are the actual keyboard events. Note that KEYDOWN get's added multiple time to the queue depending on the key-repeat settings).
Also, there's no need to query the state of all keys while handling a KEYDOWN event, since you already know which key is pressed down by checking event.key
If you're interested in if a key is hold down (and ignoring the key-repeat, which you probably want), then you should simply use key.get_pressed(). Using a bunch of flags is just unnecessary and will clutter up your code.
So your code could simplified to:
while not done:
keys = key.get_pressed()
if keys[K_DOWN]:
print "DOWN"
for e in event.get():
pass # proceed other events.
# always call event.get() or event.poll() in the main loop

I am not familiar with Pygame, but, as I see, the program in it should have an event-based architecture. Unless you get the incoming events and process them, nothing happens. That's why your simple loop becomes infinite: it just does not process events.
while keys[K_DOWN]: # Nobody updates the keys, no events are processed
print "DOWN"
Then concerning the get_pressed() call. What it returns is a list of keys. So, you are trying to just loop until the key is released. That's a problem. According to this, pygame.event.get() returns immediately even if there are no events in the queue. The call to get() means: my code still has what to do, but I don't want to block the events, so please process the pending events before I continue. If your code is just waiting for an event, that means it has nothing to do.
The function to WAIT (without blocking the inner loop of Pygame) for an event is pygame.event.wait() (the logic is: I have nothing to do in my code until something happens). However, if you use it, you will have to get information about keys pressed or released from the event itself, not from get_pressed().
Here is an example from the comments to the doc page:
for event in pygame.event.get() :
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_SPACE :
print "Space bar pressed down." #Here you should put you program in the mode associated with the pressed SPACE key
elif event.key == pygame.K_ESCAPE :
print "Escape key pressed down."
elif event.type == pygame.KEYUP :
if event.key == pygame.K_SPACE :
print "Space bar released."
elif event.key == pygame.K_ESCAPE :
print "Escape key released." #Time to change the mode again

I like to take a slightly different approach to this problem. Instead of checking if the key is pressed and taking some action when it is, set a flag on key down and unset it on key up. Then in the function to update the player's position, check the flag and update accordingly. The following pseudo-Python explains what I'm getting at:
if down_key_pressed:
down_flag = True
elif down_key_released:
down_flag = False
elif right_key_pressed:
etc...
This should be done in a separate loop that takes the player's input. Then in update_player_position() you can do:
if down_flag:
move_player_down()
elif right_flag:
move_player_right()
This example assumes four-directional movement, but you could extend it to eight-directional fairly easily by just using if down_flag and right_flag instead.

You can get keydown event repeatedly if you use pygame.key.set_repeat(# millisecond) to set the time limitation for each key event. Quote: when the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled. When pygame is initialized the key repeat is disabled. please see following link for detail http://www.pygame.org/docs/ref/key.html#pygame.key.set_repeat

I am using a different approach on holding down a key that I am using in the specific task of moving an object left or right.
I do not care about the computer knowing that a key is actually held down.
When I press a key, a variable that I define for that key (EG: left arrow) is set to True.
Until that key is unpressed (with the event pygame.KEYUP) the "movement to left" is performed.

Dominik's solution is the perfect one (separating event.get() from the keyboard ones). It works perfectly! Finally, no more problems with pygame's input.
Flags:
flag = False # The flag is essential.
while not done:
for e in event.get(): # At each frame it loops through all possible events.
keys = key.get_pressed() # It gets the states of all keyboard keys.
if e.type == QUIT:
done = True
if e.type == KEYDOWN: # If the type is KEYDOWN (DIFFERENT FROM "HELD").
if keys[K_DOWN]: # And if the key is K_DOWN:
flag = True # The flag is set to true.
elif e.type == KEYUP: # The very opposite.
if keys[K_DOWN]:
flag = False
if flag == True: # DON'T USE "while flag == true", it'll crash everything. At every frame, it'll check if the flag is true up there. It's important to remember that the KEYDOWN worked ONLY ONCE, and it was enough to set that flag. So, at every frame, THE FLAG is checked, NOT THE KEYDOWN STATE. Every "player movement" while a key is being held MUST be done through flags.
print "DOWN"

This is basically How I did it, well Go to www.cswonders.com, and it might help you . Go to level 3, then 3.6, and go through the tutorial. I learned it from there. Here is my code.
def update(self):
self.speedx = 0
self.speedy = 0
# If left or right key is pressed, move left or right
pressed_key = pygame.key.get_pressed()
if pressed_key[pygame.K_LEFT]:
self.speedx = -10
if pressed_key[pygame.K_RIGHT]:
self.speedx = 10
if pressed_key[pygame.K_UP]:
self.speedy = -10
if pressed_key[pygame.K_DOWN]:
self.speedy = 10
self.rect.x += self.speedx
self.rect.y += self.speedy
# No further move if off screen
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.left < 0:
self.rect.left = 0

Related

How can I make it so that something happens in pygame when I type a number [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
Closed 1 year ago.
My code is:
keys = pygame.key.get_pressed()
if keys[pygame.K_1]:
if money >= worker_cost:
money -= worker_cost
workers += 1
worker_cost = round(worker_cost*1.1, 2)
So I am trying to make a game in pygame, but when I put this in to detect when the user wants to buy something, it won't detect that I pressed anything. I have tried putting print commands inside the if loop to see if the loop starts, but it doesn't. Any tips would be appreciated
Use the keyboard events instead of pygame.key.get_pressed().
pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a key and get continuous movement.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_1:
# do action
if money >= worker_cost:
money -= worker_cost
workers += 1
worker_cost = round(worker_cost*1.1, 2)
# [...]

What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN

I have been trying to code a small 2D game using python.
checkp_list[0]=head_pos
pressed_key= pygame.key.get_pressed()
if pressed_key[pygame.K_ESCAPE]:
running=False
if pressed_key[pygame.K_UP]:
if dir_ not in ["up", "down"]:
dir_= "up"
checkp_no= checkp_no+1
#head_pos_next=head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
if pressed_key[pygame.K_DOWN]:
if dir_ not in ["up", "down"]:
dir_= "down"
checkp_no= checkp_no+1
#head_pos_next = head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
if pressed_key[pygame.K_RIGHT]:
if dir_ not in ["left", "right"]:
dir_= "right"
checkp_no= checkp_no+1
#head_pos_next = head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
if pressed_key[pygame.K_LEFT]:
if dir_ not in ["left", "right"]:
dir_= "left"
checkp_no= checkp_no+1
#head_pos_next = head_move(head_pos, dir_)
checkp_list.insert(checkp_no,head_pos)
log_file_obj.write("chekp_list {}, checkp_no {} after append dir {}\n".format(checkp_list,checkp_no,dir_))
All this is running inside a while True: loop. Basically, whenever an arrow key is pressed, direction indicator dir_ changes and it'll add 1 to checkpoint number checkp_no and insert current head position(head_pos of the head object in checkp_no index position.
But unfortunately, all in the checkp_list points turns out to be the latest head_pos. The list checkp_list is an important factor to implement my logic.
chekp_list while start [[325, 791], [325, 791], [325, 791]],
this is the checkp_list when checkp_no is 2 and when while loop is starting another iteration (taken from log file created).
All the points where checkp_list is getting appended are above.
Please help me to identify the issue.
pygame.key.get_pressed() returns a list with the current states of a key. When a key is hold down, the state for the key is True, else it is False. Use pygame.key.get_pressed() to evaluate if a key is continuously pressed.
while True:
pressed_key= pygame.key.get_pressed()
if pressed_key[pygame.K_UP]:
# the code in this condition is executed as long UP is hold down
# [...]
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released.
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
# The following code is executed once, every time when ESC is pressed.
# [...]
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
# The following code is executed once, every time when ESC is released.
# [...]

How can I detect keyboard input to play a sound with python?

I am making a program that will play a sound when you press a key (or just type) on the keyboard.
I just started to work on it, and I am trying to use pygame.key.get_pressed() to check for keyboard input. I got to here with the code:
from pygame import init, key
init()
spam = []
while True:
spam = key.get_pressed()
if True in spam:
print('KEY PRESSED')
elif False in spam:
print('NONE')
It works by checking if a True value is in spam (spam is the name of the list pygame returns). key.get_pressed returns a list of True/False values for every key, True if pressed, False if not.
The problem with this code is that when I run it, it only outputs None. This means that I am not detecting the keyboard input.
If anyone knows how to fix this, or a better way to do it, I would greatly appreciate it!
Thanks!
pygame.key.get_pressed() gets the states of all keybord buttons. It returns a list.
But, note the values which are returned by pygame.key.get_pressed() are only updated when the key event is get events from the queue by pygame.event.get(). In simple words, pygame.key.get_pressed() only works, if there is an event loop, too.
You can evaluate a specific key (e.g. space key):
allKeys = pygame.key.get_pressed()
spam = allKeys[pygame.K_SPACE]
If you want to evaluate if any key is pressed, then you've to evaluate if any() value in allKeys is True:
allKeys = pygame.key.get_pressed()
anyKey = any([k for k in allKeys if k == True])
Another solution would be to check if the KEYDOWN event occurred. The next pending event can be fetched by pygame.event.get():
keyPressed = False
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
keyPressed = True

How to get an input from user in Pygame and save it as a variable? [duplicate]

This question already has an answer here:
How to get text input from user in Pygame? [duplicate]
(1 answer)
Closed 5 years ago.
I want to take input from the user in the game (e.g. their name), then put it on the screen.
I tried modules (including InputBox), but none of them are working. They just displayed my text on screen.
I want to save that input to a variable. Is there any way to do this?
Example:
font1 = pygame.font.SysFont("None", 30)
score = 0
text = font1.render("{}".format(score), True,(255,255,255))
...
...
if sneakeattheapple:
score += 1
text = font1.render("{}".format(score), True,(255,255,255))
...
...
screen.blit(text,(275,6))
This is going to put the score variable on the screen. But score is already defined, I want to do this with a variable given by user.
EDIT: Let me be more clear. In Python we can do this:
x = input("Do you want to do this? (y/n): ")
if x == "y":
#do something
if x == "n":
#do something
This is what I want to do in Pygame.
There's nothing baked into Pygame for this. You will either need to use a 3rd-party GUI library, or build it yourself. Example: if the textbox has focus, take all keydown events, and append to a string. Each frame, draw a box, then draw the string on top.
Building a simple one shouldn't be that hard, but if you want a more full-featured one, it will likely be easier to use a library.
I am currently assuming this function worked with your program is successful and raises no errors. Here is a function in the link you gave us:
def ask(screen, question):
"ask(screen, question) -> answer"
pygame.font.init()
current_string = []
display_box(screen, question + ": " + string.join(current_string,""))
while 1:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
break
elif inkey == K_MINUS:
current_string.append("_")
elif inkey <= 127:
current_string.append(chr(inkey))
display_box(screen, question + ": " + string.join(current_string,""))
return string.join(current_string,"")
It looks like this is how you get input from a user with a pygame screen right? Let's look at line 4 of this function:
current_string = []
The stuff the user types in is stored in this list. Assuming you know how to take a string and put it on the screen, you could save the string like this:
string_input = ''.join(current_string)
If you can make a similar function (if this one doesn't work), you can do the same thing! Just save the first item in the list holding the string in a variable as shown above. If you have any problems, please comment so I can edit my answer. To the next part now. You can simply activate this function at any time. You might want to activate when something happens. An example is when the snake eats the apple. You probaly have a function for that I believe. You can make a variable like this :
Eat = 0
And put in that function. When that variable is equal to 0. Nothing really activate the other function. When the snake eats the apple, reset the variable to 1 and then activate the function like this:
if Eat = 0:
someclassname.ask()
You do this with many other occasions. I hope this is clearer and more helpful!
Have you tried getting key event's and then putting them together to form something? You could put something like this inside of your main game loop.
input = ''
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
playing = False
if event.key == pygame.K_w:
input = input + "w"
if event.key == pygame.K_s:
input = input + "s"
if event.key == pygame.K_a:
input = input + "a"
Then you can check for when the user is done with input(button/enter key), and finalize the variable.
With this you might run into a problem where the key is held longer so you get 3 w's for only pressing the button once. If this is the case you can either 1. not allow another input until a certain time has passed(.25 s maybe?) or 2. use pygame.KEYUP instead of pygame.KEYDOWN and exclusively check for strokes.
Here is a possible solution, it may be really useful for future visitors that are also suffering with this problem like me. Which is;
First making a text-render function
def text1(word,x,y):
font = pygame.font.SysFont(None, 25)
text = font.render("{}".format(word), True, RED)
return screen.blit(text,(x,y))
Then a function works like input;
def inpt():
word=""
text1("Please enter your name: ",300,400) #example asking name
pygame.display.flip()
done = True
while done:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
word+=str(chr(event.key))
if event.key == pygame.K_b:
word+=chr(event.key)
if event.key == pygame.K_c:
word+=chr(event.key)
if event.key == pygame.K_d:
word+=chr(event.key)
if event.key == pygame.K_RETURN:
done=False
#events...
return text1(word,700,30)
As you see, this function catching keyboard events, it has its own while loop also, it's important. I break the loop when pressing Enter button which is if event.key == pygame.K_RETURN: done=False . Then returning our word with text1 function, displaying. Other events can be set of course opinion-based, like space button for a gap etc.
Then, actually we have to make an intro function and we will ask the user's name in there for example, when intro function is done, name goes to set on the screen until game is over.
def game_intro():
intro=True
while intro:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
intro=False
inpt() #Here we are calling our function
screen.fill(white)
pygame.display.update()
clock.tick(15)
See that we break this loop with the Enter button again. So we will ask the user our question in intro, then we will set it on the screen, top-right corner for example.

python xlib xgrabkey keyrelease events not firing

i want to catch keydown and keyup events with python xlib, but keyup events disappear when some keys are pressed simultaneously.
if 2 or more keyes are released simultaneously then there will be 2 or more keypress events, but only 1 keyrelease event.
for this to happen the keys don't even have to be released simultaneously, for example if you enter this sequence fast:
press A
press B
release A
release B
will yield only 1 keyrelease for A
press A
press B
release B
release A
will yield 2 keyreleases
from Xlib import X,XK
from Xlib.display import Display
import signal,sys
root = None
display = None
def grab_keyname(n):
global root
keysym = XK.string_to_keysym(n)
keycode = display.keysym_to_keycode(keysym)
root.grab_key(keycode, X.AnyModifier, False,X.GrabModeSync, X.GrabModeAsync)
def main():
# current display
global display,root
display = Display()
root = display.screen().root
root.change_attributes(event_mask = X.KeyPressMask|X.KeyReleaseMask)
grab_keyname("j")
grab_keyname("k")
grab_keyname("l")
signal.signal(signal.SIGALRM, lambda a,b:sys.exit(1))
signal.alarm(4)
while True:
event = display.next_event()
print event.type
main()
Even though this question is 7 years old, a solution for anyone stumbling upon this:
This is a 'bug' in Xorg (which is apparently intentional) which causes keyboard grab to stop upon a key release, and only to start again on another button press. Therefore, any events in between (-> the second key release event) are lost. See https://bugs.freedesktop.org/show_bug.cgi?id=99280
The solution proposed there is to have a counter which indicates how many of the keys are still pressed, and manually grab the keyboard if there are still pending presses. In addition, the keyboard needs to be ungrabbed after the last release event.
The implementation might look like the following:
keys_pressed = 0
# ...
event = display.next_event()
if event.type == X.KeyPress:
keys_pressed += 1
elif event.type == X.KeyRelease:
if keys_pressed != 0:
keys_pressed -= 1
if keys_pressed == 0:
display.flush()
display.ungrab_keyboard(X.CurrentTime)
else:
root.grab_keyboard(False, X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)
Note the check for keys_pressed != 0 in the release branch: this is because after ungrabbing the keyboard, one additional release event is caught (there is probably a way to prevent this, but it does not matter for my use case...)

Categories

Resources