I am doing a pygame project and it is supposed to have only 4 fps which makes it a frame every 0.25 seconds. My problem is that when you click briefly on a key, the event might not get detected because the program does not check for events over the whole 0.25 seconds but rather once every 0.25 seconds, which makes it easy for it to miss out on an event. Is there a way to solve this problem in pygame?
(I am using clock.tick() to set the fps)
I cannot provide a solution for your particular situation as you do not show us your code. However
My problem is that when you click briefly on a key, the event might not get detected
No that is not true. All events are detected. pygame.event.get() returns all the event that have been occurred since the last call of this function. See How to get keyboard input in pygame?.
Your game logic may be wrong and you are setting a status when the KEYDOWN event occurs, but rest it when the KEYUP event occurs. This will cause the behavior.
It is even possible to run the event loop more often than the application loop.
Run the application loop and event loop at a higher frame rate, but run the game at only 4 FPS. For example, run the application loop at 40 FPS. Use a frame counter and run the game when the counter reaches 10:
clock = pygame.time.Clock()
frame_count = 0
run = True
while run:
clock.tick(40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
frame_count = (frame_count +1) % 10
if frame_count == 0:
# run game and do drawing
# [...]
pygame.display.flip()
Related
I'm trying to do something like this.
pygame.event.set_allowed([pygame.KEYDOWN])
for event in pygame.event.get():
. . .
pygame.display.flip()
Since the game I'm making is static when there's no input anyway, I'm trying to avoid re-rendering identical frames between inputs in every single while True iteration. This code seems to be almost working, except there's the slightest (disorienting) delay between input and result. If someone can help me mitigate this I want to go further and put much of the rest of the code within the for event in ... block to optimise a bunch more by skipping iterating over the game state 2D list every loop since it's static until there's input. Thank you.
The usual way is to update the game in every frame and to limit the frames per second using pygame.time.Clock.tick.
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
# handle events
# [...]
# clear background
# [...]
# draw scene
# [...]
# update the display
pygame.display.flip()
# limit frames per second
clock.tick(100)
If there is more than 1 event at once, your approach can update the game several times per frame. Sets a status variable when there was an event and updates the game once when the status is set:
run = True
while run:
redraw_scene = False
for event in pygame.event.get():
redraw_scene = True
# handle events
# [...]
if redraw_scene:
# clear background
# [...]
# draw scene
# [...]
# update the display
pygame.display.flip()
When running I am blit'ing some text onto my surface (WIN) and I want this to be shown in the game so I call pygame.display.update to update the display, this works perfectly fine until the loop has iterated around 75 times and after this the display stops updating. Code below
def dialogue(x,y,text):
global clock
global run
typing=True
tempstring=""
counter=0
switcher=0
for z in range(0,(len(text))):
clock.tick(15)
tempstring=text[0:z+1]
counter+=1
print(counter)
print("temp is "+tempstring)
writing=TITLE_FONT.render(tempstring, 1, (255,255,255),(0,0,0))
if x==-1 and y>-1:
pygame.display.update(WIN.blit(writing, (640-(writing.get_width()//2),y-(writing.get_height()//2))))
elif x>-1 and y==-1:
pygame.display.update(WIN.blit(writing, (x-(writing.get_width()//2),360-(writing.get_height()//2))))
elif x==-1 and y==-1:
pygame.display.update(WIN.blit(writing, (640-(writing.get_width()//2),360-(writing.get_height()//2))))
else:
pygame.display.update(WIN.blit(writing, (x-(writing.get_width()//2),y-(writing.get_height()//2))))
print(typing)
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
run=False
pygame.quit()
break
if event.type == pygame.KEYDOWN:
break
I have tried just using pygame.display.update() at the end of the line of if statements too but this also fails.
On some systems PyGame doesn't work correctly if you don't get pygame.event from system - system may think that program hangs and it may even close it.
You may need to use pygame.even.get() (or similar functions) inside your loop.
In doc pygame.event you can find (but it is hidden in long description)
To prevent lost events, especially input events which signal a quit command, your program must handle events every frame (with pygame.event.get(), pygame.event.pump(), pygame.event.wait(), pygame.event.peek() or pygame.event.clear()) and process them.
Not handling events may cause your system to decide your program has locked up.
To keep pygame in sync with the system, you will need to call pygame.event.pump() internally process pygame event handlers to keep everything current.
I am currently writing a game that requires a timer. I have written some code that appears to work, as in it counts upward in seconds, but when I move the mouse, the timer resets back to zero and starts counting up again. Obviously, this is not what I want to happen, and I've tried figuring out why this happens instead of counting up continuously, but I could not solve it.
My code is:
passed_time = 0
timer_started = False
(^outside the while loop)
(/ inside while loop)
for event in ev:
if event.type == pygame.QUIT:
is_alive = False
if start_clicked:
timer_started = True
if timer_started:
start_time = pygame.time.get_ticks()
if won:
timer_started = False
if timer_started:
passed_time = pygame.time.get_ticks() - start_time
text = font.render(str(passed_time / 1000), True, font_color)
screen.blit(text, (20, 450))
as well as mouse events that set [start_clicked] or [won] to True when their respective colors are clicked.
From my basic knowledge of timers in PyGame, this should keep counting up once start_clicked is set to true, however it just resets every time the mouse is moved, which completely defeats the point of a timer on a mouse-movement based avoider game. What can I do to fix this and make it run continuously, until the won variable is true?
And if for whatever reason I didn't include enough to figure out the issue, the full code is here:
By looking at your full code, your event loop is wrong, because your identation is off.
Currently you have if event.type == pygame.QUIT: in the event loop, and all the other if event.type == whatever outside it because they are not indented enough.
All the ifs checking the events must be inside the event loop, while ifs unrelated to the event loop (like if start_clicked:) must be outside, typically after, the event loop.
I cannot fix all your code, but fixing the event loop will surely help.
Here's my check_for_pause() function:
#Check if the user is trying to pause the game
def check_for_pause():
keys=pygame.key.get_pressed() #Get status of all keys
if keys[K_SPACE]: #The space bar is held down
global paused #Make global so it can be edited
if paused==True: #It was paused, so unpause it
paused=False
elif paused==False: #It was playing, so pause it
paused=True
#Don't let the main loop continue until the space bar has been released again, otherwise the variable will flicker between True and False where the loop runs so fast!
space_bar_pressed=keys[K_SPACE]
while space_bar_pressed: #Repeat this loop until space_bar_pressed is False
keys=pygame.key.get_pressed()
if not keys[K_SPACE]: #Space bar has been released so set space_bar_pressed to False
space_bar_pressed=False
however this keeps making my program become unresponsive whenever I try to pause it! Basically, I want the variable "paused" to be either True or False. When the space bar is pressed, it should change to whichever one it isn't currently. Because I'm using check_for_pause() in another never-ending loop, I need to make it so that the function only stops executing when the space bar is released, otherwise if the user holds the space bar for more than a split second it will keep switching between True and False.
Any ideas why my program becomes unresponsive when I run this? I know it's to do with the bit that waits until the space bar's been released because when I remove that bit of the code then my program runs fine (but obviously the pause feature then doesn't work).
You have a main loop that probably looks something like this...
while True:
check_for_pause()
# Update the game
# Draw the game
When you check for pause, and the space key is pressed down, paused gets set to True. Then, you have this loop...
space_bar_pressed=keys[K_SPACE]
while space_bar_pressed: #Repeat this loop until space_bar_pressed is False
keys=pygame.key.get_pressed()
if not keys[K_SPACE]: #Space bar has been released so set space_bar_pressed to False
space_bar_pressed=False
The problem with this loop is that you assume that pygame.key.get_pressed() will continue to return up-to-date information. However, looking at the pygame source code, it appears that it uses SDL_GetKeyState, which says as part of its documentation..
Note: Use SDL_PumpEvents to update the state array.
In other words, repeatedly calling pygame.key.get_pressed() will NOT give you the updated key state if you are not additionally calling something like pygame.event.pump(), which actually updates pygame with the new key states. So, you could quickly fix this by introducing that pump function into the loop, as currently it is just running forever.
That being said: DON'T DO THIS. If you do it this way, your game will not be able to do ANYTHING when paused: this includes showing a "paused" screen, continuing to play the music in the background, etc. What you want to do instead is keep track of if the game is paused, and if so, change how the game is updating.
In the part of your game where you update things, some items should only happen if the game is paused. Something like...
paused = False
while True:
# This function should just return True or False, not have a loop inside of it.
paused = check_for_paused()
if not paused:
# This function moves your enemies, do physics, etc.
update_game()
draw_game()
In this case, the main loop will still happen, the game will continue to be drawn, and the input will continue to be handled. However, the enemies and players will not be moving, therefore the game can be said to be "paused".
Finally, there is also the fact that you're relying on get_key_pressed(), which you probably don't want to do. See this other similar answer I've given for reasons why you should be instead using the event queue.
You should never stop running your game loop in a game even if it paused. Also pauses are generally handled through events. For example look at this code:
import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_mode((400,400))
paused = False # global
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_SPACE:
paused = not paused
if paused:
continue # skip this iteration if paused
# add your game code here
print 'game code running'
In the above code, I toggle paused every time I press spacebar. If you want to pause only while holding spacebar do this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type in (KEYDOWN, KEYUP): # checks membership in tuple
if event.key == K_SPACE:
paused = not paused
if paused:
continue # skip this iteration if paused
# add your game code here
print 'game code running'
As a general note, you should always be handling events from your event queue or Pygame will just cry and whine and do nothing (be unresponsive). Thus, never stop spinning on your game loop even if you have paused the game.
EDIT: Alternatively, as abarnert pointed out in the comments you can do a trick with equality comparisons to assure that you never get conflicts between KEYDOWN and KEYUP events:
paused = event.type == KEYDOWN
This way you won't have "syncing problems" where the code accidentally sets paused to True whenever you actually release the spacebar. This can happen if 2 KEYDOWN events happen in a row or if 2 KEYUP events happen in a row (instead of a smooth alternating sequence like KEYDOWN, KEYUP, KEYDOWN, KEYUP, etc.). It's best to not assume that all event queues feed events that are 100 percent accurate.
This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Pygame unresponsive display
(1 answer)
Closed 1 year ago.
Basically I have a loop (tick, set_caption, screen_fill, event.get(), send_frame_event, flip, repeat)
When I drag the window around on windows 7, the loop stops looping, I ended up stuck in pygame.event.get(), I have tried to define certain events only for get e.g. get([pygame.QUIT]) to no avail.
Simply calling pygame.event.clear() has the same freeze effect when dragging/moving the window.
Is there a workaround?
Not full code, but should be enough:
def start(self):
self.running = True
Clock = pygame.time.Clock()
while self.running:
self.p += 25
tickFPS = Clock.tick(self.fps)
pygame.display.set_caption("Press Esc to quit. FPS: %.2f" % (Clock.get_fps()))
self.screen.fill([self.p&0xFF,(255-self.p)&0xFF,255])
self.handleEvents()
self.raiseEvent("updateFrame")
pygame.display.flip()
def handleEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
full code at: http://pastie.org/private/wm5vqq3f7xe0xlffy1fq
Try placing a call to pygame.event.pump() inside your mainloop (or handleEvents function)
No idea if this will help or not:
I realize the problem is with moving the window window, not with re-sizing the window.
Perhaps there is some similarity between moving and re-sizing?
I found this in the documentation on re-sizing:
Then the display mode is set, several events are placed on the pygame event queue. pygame.QUIT is sent when the user has requested the program to shutdown. The window will receive pygame.ACTIVEEVENT events as the display gains and loses input focus. If the display is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the user adjusts the window dimensions. Hardware displays that draw direct to the screen will get pygame.VIDEOEXPOSE events when portions of the window must be redrawn.
and:
Note that when the user resizes the game window, pygame does not automatically update its internal screen surface. You must call set_mode() every time VIDEORESIZE is sent. This really should be more clear in the documentation.
Perhaps when moving the game window, something similar happens?