okay, so I am making a basic space-ship game.
I can't get rotation to work because it scrambles the bitmap, but that's for another question.Should I even use a gif? any other filetype suggestions?
back to the actual point here, so:
k = pygame.key.get_pressed()
yeah, self explanatory. this doesn't work, as it returns each key as pressed.
so, somewhere else:
d = k[pygame.K_d]
and another line:
print d
and another:
if d:
So, k returns as each key on the keyboard pressed.
d returns 0 indefinitely, whether or not d is pressed.
d is always 0.
the statement about d therefore is never true.
Why is this happening?
To know which key was pressed, we have to check the event. key variable corresponds to which pygame keys. For example, the pygame key for the letter “A” is “K_a” then we will compare event. Key with K a and if it comes to be same that means the key “A” was pressed.
key. get_pressed() which returns a list of the state of all keys. The list contains 0 for all the keys which are not pressed and 1 for all keys that are pressed. Its index in the list is defined by constants in the pygame module, all prefixed with K_ and the key name.
Pygame. error video system not initialized can be fixed by stopping the main loop of implementation with the exit() function. The pygame. init() error is caused by its function not being called and can be solved with the import and initialization of pygame modules.
You might be confused by what get_pressed() is actually doing. From the docs:
Returns a sequence of boolean values representing the state of every key on the keyboard. Use the key constant values to index the array. A True value means the that button is pressed.
Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed(). There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality.
In other words, when you call get_pressed(), you are getting a representation of the state of the keyboard at the time of get_pressed() being called.
For example, let's say one second into your game you call get_pressed(). You'll get back a structure that lists all of the keys on the keyboard and if they are pressed (they will all be false).
At two seconds into your game, you press a key. If you look at the same structure that you were looking at earlier, it will STILL say that everything is not pressed, because you're still looking at the state of the keyboard as it was a second ago. However, if you called get_pressed() again, you'd get back a new, updated structure, and this new structure should show that the key is pressed.
One way to solve this would be to do the following:
while True:
# Update Stuff
# Draw Stuff
state = pygame.key.get_pressed()
# Now check the keys
Now, you're getting up-to-date information on the keyboard.
One thing that should be noted is that using the functionality above, you could STILL potentially miss a keyboard press. If the update functionality took a long time, a key could potentially be pressed then unpressed in a small enough time that you wouldn't have called get_pressed() when the key was down.
If this might be a problem, you will probably want to use the event loop instead. Something like...
is_moving = False
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
is_moving = True
elif event.type == pygame.KEYUP and event.key == pygame.K_d:
is_moving = False
For mixing (1) event and (2) keystate inputs, it looks like
import pygame
from pygame.locals import *
done = False
while not done:
for event in pygame.event.get():
# any other key event input
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
if event.key == K_ESC:
done = True
elif event.key == K_F1:
print "hi world mode"
# get key current state
keys = pygame.key.get_pressed()
if keys[K_SPACE]:
#repeating fire while held
fire()
I like the same to be true for KEYDOWN and KEYUP,
You have to poll events. One way you can do it is
while not done:
for event in pygame.event.get():
# any other key event input
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
if event.key == K_ESC:
done = True
player.handle_event(event)
then in Player()
def handle_event(self, event):
if event.type == KEYDOWN:
if event.key == K_f: print 'Player.f pressed'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With