I'm making an Asteroidz clone in pygame and have two for event in pygame.event.get() loops, one for checking an exit request and wether the game should have started by pressing spacebar, then further in the game as to try and limit the player from holding spacebar down and continuously shooting. The relevent code for my check_input function, which is run once every loop, is below;
def check_input(self):
for event in pygame.event.get(): #NOT CHECKING THIS FAST ENOUGH, WHOLE PROCESS IS TOO SLOW
if (event.type == pygame.KEYUP) and (event.key == pygame.K_SPACE):
print ('boop')
self.shootThrottle = 0
if self.shootThrottle == 0:
self.shootThrottle += 1
bullets.add(Bullet(self.shape[0][0],self.shape[0][1], self.angle))
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.angle -= 7
self.rotate(-7)
elif key[pygame.K_RIGHT]:
self.angle += 7
self.rotate(7)
if self.angle > 360:
self.angle -= 360
elif self.angle < 0:
self.angle += 360
if key[pygame.K_UP]:
self.accelerate()
elif key[pygame.K_DOWN]:
self.decelerate()
I am using shootThrottle as a means to try stop bullets from being shot until spacebar has been let go. This system works, but due to the for event in pygame.event.get() being too slow, it doesn't function properly.
Any help is massively appreciated!
[...] and have two for event in pygame.event.get() loops [..]"
That's the issue. pygame.event.get() get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
Get the events once per frame and use them in multiple loops or pass the list or events to functions and methods where they are handled:
def handle_events(events):
for event in events:
# [...]
while run:
event_list = pygame.event.get()
# [...]
# 1st event loop
for event in event_list:
# [...]
# [...]
# 2nd event loop
for event in event_list:
# [...]
# [...]
# function which handles events
handle_events(event_list)
I'm making an Asteroidz clone in pygame and have two for event in pygame.event.get() loops, one for checking an exit request and wether the game should have started by pressing spacebar, then further in the game as to try and limit the player from holding spacebar down and continuously shooting. The relevent code for my check_input function, which is run once every loop, is below;
def check_input(self):
for event in pygame.event.get(): #NOT CHECKING THIS FAST ENOUGH, WHOLE PROCESS IS TOO SLOW
if (event.type == pygame.KEYUP) and (event.key == pygame.K_SPACE):
print ('boop')
self.shootThrottle = 0
if self.shootThrottle == 0:
self.shootThrottle += 1
bullets.add(Bullet(self.shape[0][0],self.shape[0][1], self.angle))
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.angle -= 7
self.rotate(-7)
elif key[pygame.K_RIGHT]:
self.angle += 7
self.rotate(7)
if self.angle > 360:
self.angle -= 360
elif self.angle < 0:
self.angle += 360
if key[pygame.K_UP]:
self.accelerate()
elif key[pygame.K_DOWN]:
self.decelerate()
I am using shootThrottle as a means to try stop bullets from being shot until spacebar has been let go. This system works, but due to the for event in pygame.event.get() being too slow, it doesn't function properly.
Any help is massively appreciated!
[...] and have two for event in pygame.event.get() loops [..]"
That's the issue. pygame.event.get() get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
Get the events once per frame and use them in multiple loops or pass the list or events to functions and methods where they are handled:
def handle_events(events):
for event in events:
# [...]
while run:
event_list = pygame.event.get()
# [...]
# 1st event loop
for event in event_list:
# [...]
# [...]
# 2nd event loop
for event in event_list:
# [...]
# [...]
# function which handles events
handle_events(event_list)
I'm making an Asteroidz clone in pygame and have two for event in pygame.event.get() loops, one for checking an exit request and wether the game should have started by pressing spacebar, then further in the game as to try and limit the player from holding spacebar down and continuously shooting. The relevent code for my check_input function, which is run once every loop, is below;
def check_input(self):
for event in pygame.event.get(): #NOT CHECKING THIS FAST ENOUGH, WHOLE PROCESS IS TOO SLOW
if (event.type == pygame.KEYUP) and (event.key == pygame.K_SPACE):
print ('boop')
self.shootThrottle = 0
if self.shootThrottle == 0:
self.shootThrottle += 1
bullets.add(Bullet(self.shape[0][0],self.shape[0][1], self.angle))
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.angle -= 7
self.rotate(-7)
elif key[pygame.K_RIGHT]:
self.angle += 7
self.rotate(7)
if self.angle > 360:
self.angle -= 360
elif self.angle < 0:
self.angle += 360
if key[pygame.K_UP]:
self.accelerate()
elif key[pygame.K_DOWN]:
self.decelerate()
I am using shootThrottle as a means to try stop bullets from being shot until spacebar has been let go. This system works, but due to the for event in pygame.event.get() being too slow, it doesn't function properly.
Any help is massively appreciated!
[...] and have two for event in pygame.event.get() loops [..]"
That's the issue. pygame.event.get() get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
Get the events once per frame and use them in multiple loops or pass the list or events to functions and methods where they are handled:
def handle_events(events):
for event in events:
# [...]
while run:
event_list = pygame.event.get()
# [...]
# 1st event loop
for event in event_list:
# [...]
# [...]
# 2nd event loop
for event in event_list:
# [...]
# [...]
# function which handles events
handle_events(event_list)
This question already has an answer here:
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
(1 answer)
Closed 2 years ago.
So, I've been working on a project in PyGame. To test the currency system out, I've decided to increment the money you have when a key is pressed. I've tried moving the if statement out of the game loop. Here's my code:
gameRun = True
while gameRun:
for event in pygame.event.get():
if event.key == pygame.K_p:
print("This Should Work")
currency.balance.amount += 1
pygame.display.update()
I have other several other lines of code but I believe that there is something wrong about these lines. Thank you so much!
Is there a reason why event.key doesn't work halfway through my code?
I guess you have more than 1 event loop, respectively call to pygame.event.get() are there in your code.
Note pygame.event.get() removes the events from the queue. If you have more than 1 event loop, then just one random loop will get the events, all the other loops go empty-handed. That causes that you'll miss events.
Get the list of events once in the main application loop and use same list of events in multiple event loops. e.g:
gameRun = True
while gameRun:
events = pygame.event.get()
for event in events:
if event.key == pygame.K_p:
print("This Should Work")
currency.balance.amount += 1
pygame.display.update()
# [...]
foo(event)
# [...]
for event in events:
# [...]
def foo(events):
for event in events:
# [...]
I'm attempting to work out simple controls for an application using pygame in Python. I have got the basics working, but I'm hitting a weird wall: I am using the arrow keys to control my character. If I hold down one arrow key, then hold down another arrow key (to move diagonally), the character moves as expected. However, if I release the second key that I pressed (while still holding down the first key), the character stops moving, even though I am still holding down that first key. Here is my simple movement code:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if pygame.key.get_pressed()[K_LEFT]:
player.pos = (player.pos[0] - 2, player.pos[1])
if pygame.key.get_pressed()[K_RIGHT]:
player.pos = (player.pos[0] + 2, player.pos[1])
if pygame.key.get_pressed()[K_UP]:
player.pos = (player.pos[0], player.pos[1] - 2)
if pygame.key.get_pressed()[K_DOWN]:
player.pos = (player.pos[0], player.pos[1] + 2)
Now, I was naturally very confused by this. So I tried to print some lines to debug. In the top of the main control loop, I wrote:
print (pygame.key.get_pressed()[K_DOWN], pygame.key.get_pressed()[K_RIGHT])
print pygame.event.get()
...to output a tuple displaying the state of the down and right arrow keys, and then display the pygame event queue. My results baffled me even more. If I move the character diagonally down and right, pressing the down key first and then the right key, then release the right key to make it move simply downward, the character stops moving as before... but this is printed to the shell:
(1, 0)
[]
That is, when I release the right arrow key and still hold down the down arrow key, pygame.key.get_pressed() understands that the down arrow key is still being held down, but there is nothing in the event queue.
Also, earlier in the code (before the control loop) I am invoking
pygame.key.set_repeat(1, 2)
to make the character continue to move while the key is held down.
Any help will be appreciated! Thanks :)
For things like movement, you should not check for events (like KEYDOWN or KEYUP), but check every iteration of your mainloop if your movement keys are pressed (using get_pressed).
In your code, you check the pressed keys only if there's also a KEYDOWN event.
There are also some other things to consider:
You should seperate the key-mapping and the speed of your player, so it will be easier later on to change either of this.
You should determine a movement vector and normalize it first, since otherwise, if your vertical and horizontal movement speed is 10, your diagonal movement speed would be ~14.
Working example:
import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
run = True
pos = pygame.Vector2(100, 100)
clock = pygame.time.Clock()
# speed of your player
speed = 2
# key bindings
move_map = {pygame.K_LEFT: pygame.Vector2(-1, 0),
pygame.K_RIGHT: pygame.Vector2(1, 0),
pygame.K_UP: pygame.Vector2(0, -1),
pygame.K_DOWN: pygame.Vector2(0, 1)}
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT: run = False
screen.fill((30, 30, 30))
# draw player, but convert position to integers first
pygame.draw.circle(screen, pygame.Color('dodgerblue'), [int(x) for x in pos], 10)
pygame.display.flip()
# determine movement vector
pressed = pygame.key.get_pressed()
move_vector = pygame.Vector2(0, 0)
for m in (move_map[key] for key in move_map if pressed[key]):
move_vector += m
# normalize movement vector if necessary
if move_vector.length() > 0:
move_vector.normalize_ip()
# apply speed to movement vector
move_vector *= speed
# update position of player
pos += move_vector
clock.tick(60)
just use the events return data, instead of trying to poll, you're already checking if its a keydown event TYPE, now just interrogate the KEY index, like so:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_LEFT:
player.pos = (player.pos[0] - 2, player.pos[1])
rest of code.....
also consider using a separate data structure to store the state of your controls, then just use the events to update that data structure. That will help make the controls a bit more flexible as you wont be relying on the event queue to cause your character to move, which in my experience causes problems like: not being able to push more than two buttons at a time, and odd delay or timing issues with character movements. so something like:
keystates={'up':False, 'down':False, 'left':False, 'right':False}
running=True
#start main pygame event processing loop here
while running:
for event in pygame.event.get():
if event.type == QUIT:
running=False
#check for key down events
if event.type == KEYDOWN:
if event.key == K_UP:
keystates['up']=True
if event.key == K_DOWN:
keystates['down']=True
if event.key == K_LEFT:
keystates['left']=True
if event.key == K_RIGHT:
keystates['right']=True
#check for key up events
if event.type == KEYUP:
if event.key == K_UP:
keystates['up']=False
if event.key == K_DOWN:
keystates['down']=False
if event.key == K_LEFT:
keystates['left']=False
if event.key == K_RIGHT:
keystates['right']=False
#do something about the key states here, now that the event queue has been processed
if keystates['up']:
character.moveUp() #or whatever your call for these are...
if keystates['down']:
character.moveDown()
if keystates['left']:
character.moveLeft()
if keystates['right']:
character.moveRight()
#gracefully exit pygame here
pygame.quit()
You are using event-based input , but in this case you want polling-based input. Then you don't mess with key-repeats.
import pygame
from pygame.locals import *
done = False
player.pos = Rect(0,0,10,10)
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_LEFT]:
player.pos.left -= 10
if keys[K_RIGHT]:
player.pos.left += 10
if keys[K_UP]:
player.pos.top -= 10
if keys[K_DOWN]:
player.pos.left += 10
if keys[K_SPACE]:
print 'firing repeated gun'
My guess is that set repeat doesn't work the way that you think it will. Basically, after your second key goes up, the repeat doesn't happen. This would seem to make sense to me: open up a text editor and hold down the "A" key. "A"s will spill out across the screen. Then, press the "J" key with the "A" key still held down. The "A"s stop. That is a typical key repeat system.
I'm not sure using this "set_repeat" method is going to work out in the end anyway. Basically, any key that the player presses will now "repeat", even if they click "fire" or "jump".
As an alternative, try saving the state when the user presses or releases. Don't use the set_repeat, but do something like the following:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if pygame.key.get_pressed()[K_LEFT]:
player.moving_left = True
if pygame.key.get_pressed()[K_RIGHT]:
player.moving_right = True
if pygame.key.get_pressed()[K_UP]:
player.moving_up = True
if pygame.key.get_pressed()[K_DOWN]:
player.moving_down = True
elif event.type == KEYUP:
if pygame.key.get_pressed()[K_LEFT]:
player.moving_left = False
if pygame.key.get_pressed()[K_RIGHT]:
player.moving_right = False
if pygame.key.get_pressed()[K_UP]:
player.moving_up = False
if pygame.key.get_pressed()[K_DOWN]:
player.moving_down = False
# Somewhere else in your game loop...
if player.moving_left:
player.pos[0] -= 2
if player.moving_right:
player.pos[0] += 2
if player.moving_up:
player.pos[1] -= 2
if player.moving_right:
player.pos[1] += 2