Is it me or is pygame.key.get_pressed() not working?

2024/5/20 20:22:31

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?

Answer

You might be confused by what get_pressed() is actually doing. From the docs:

Returns asequence of boolean values representing the state of every key on thekeyboard. Use the key constant values to index the array. A True valuemeans the that button is pressed.

Getting the list of pushed buttons with this function is not theproper way to handle text entry from the user. You have no way to knowthe order of keys pressed, and rapidly pushed keys can be completelyunnoticed between two calls to pygame.key.get_pressed(). There is alsono way to translate these pushed keys into a fully translatedcharacter value. See the pygame.KEYDOWN events on the event queue forthis 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 Stuffstate = 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 = Falsewhile True:for event in pygame.event.get():if event.type == pygame.KEYDOWN and event.key == pygame.K_d:is_moving = Trueelif event.type == pygame.KEYUP and event.key == pygame.K_d:is_moving = False
https://en.xdnf.cn/q/73195.html

Related Q&A

Find out if a python script is running in IDLE or terminal/command prompt

Is there a way to find out if the python script is running in the IDLE interpreter or the terminal?Works cross-platform if possible, or if needed a different way for each platform.Work with Python 2 a…

How to map coordinates in AxesImage to coordinates in saved image file?

I use matplotlib to display a matrix of numbers as an image, attach labels along the axes, and save the plot to a PNG file. For the purpose of creating an HTML image map, I need to know the pixel coor…

Why am I getting IOError: [Errno 13] Permission denied?

I am creating Log file for the code but I am getting the following error :[Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] import mainLCF [Tue Jun 11 17:22:59 2013] [error] [client 127.0.0.1] …

Difference between bytearray and list

What is the difference between bytearray and for example, a list or tuple?As the name suggests, bytearray must be an array that carries byte objects. In python, it seems that bytes and str are treate…

python NameError: name anything is not defined (but it is!)

Note: Solved. It turned out that I was importing a previous version of the same module.It is easy to find similar topics on StackOverflow, where someone ran into a NameError. But most of the questions …

Python File Creation Date Rename - Request for Critique

Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied by the number of objects I "shoot", I can generate a large number of images. Problem: Camera gen…

Secure authentication system in python?

I am making a web application in python and I would like to have a secure login system.I have done login systems many times before by having the user login and then a random string is saved in a cookie…

Finding All Positions Of A Character In A String

Im trying to find all the index numbers of a character in a python string using a very basic skill set. For example if I have the string "Apples are totally awesome" and I want to find the pl…

DataError: (1406, Data too long for column name at row 1)

Ive read nearly all other posts with the same error and cant seem to find a proper solution. In my models.py file I have this:class LetsSayCups(models.Model):name = models.CharField(max_length=65535)de…

Continued Fractions Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 7…