I made a PvP game in Pygame, but I have a problem. I need to press on two of the keys on the keyboard at the same time (and each key does something different), but I can't. What can I do? Should I use threads? How? By the way, I'm using Mac OS X Catalina, Python 3 and Pygame 2.0.0.
Asked
Active
Viewed 81 times
1 Answers
0
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 button.
For example, if you want to test whether a and b are pressed simultaneously:
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and keys[pygame.K_b]:
# do somthing
# [...]
However, you don't need to do things at the same time. It is sufficient to do the things in the same frame. The display is just updated once per frame. All what happens in the same frame appears to be simultaneously. Read How to run multiple while loops at a time in Python to see how you can do more than one thing at a time.

Rabbid76
- 202,892
- 27
- 131
- 174
-
Thank you, but I need each key to do something different at the same time. – Itay Jan 02 '21 at 17:52
-
@Itay And what? You can evaluate all keys at the same time. Most likely, you have completely misunderstood the concept of an application loop. This seems to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You don't need to do things at the same time. It is sufficient to do the things in the same frame. The display is just updated once per frame. All what happens in the same frame appears to be simultaneously. – Rabbid76 Jan 02 '21 at 17:55