pygame and gnome-shell using TOO much CPU [duplicate] - python

i am new to pygame and i was wondering what an event loop is and what clock does in this situation, like what is clock.tick(60)? I don't understand any explanations online
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.
That meas that the loop:
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
runs 60 times per second.
for event in pygame.event.get() handles the internal events an retrieves a list of external events (the events are removed from the internal event queue).
If you press the close button of the window, than the causes the QUIT event and you'll get the event by for event in pygame.event.get(). See pygame.event for the different event types. e.g. KEYDOWN occurs once when a key is pressed.
e.g. The following loop prints the names of the a key once it it pressed:
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))

Related

Pygame window does not close properly when clock.tick in the loop

I'm working on a project and when I add the clock.tick to my main game loop, my pygame window doesnt close.
def game_loop():
"""The main game loop that runs as the game runs. Returns when the pygame window is closed."""
global running
global timer
while running:
while timer > screen.fixed_fps:
fixed_update()
timer -= screen.fixed_fps
update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
return
screen.clock.tick(screen.fps)
timer += delta_time()
pygame.quit()
return
When I click the X, the screen freezes until I let go, but unless I click the X in a very specific time frame( I usually need to click it 20 times to close) it doesnt work.
This is likely because of the way you are handling the QUIT event in your event loop. The pygame.event.get() function returns all of the events that have occurred since the last time it was called, so if there is a delay in your loop, the QUIT event may not be handled until multiple events have accumulated. To fix this, you should move the QUIT event handling code to the top of your event loop so that it is handled as soon as the event occurs. Also you could try to add pygame.display.update() after the event handling to make sure it is updated.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
return
while timer > screen.fixed_fps:
fixed_update()
timer -= screen.fixed_fps
update()
screen.clock.tick(screen.fps)
timer += delta_time()

How can I execute a function when a specific key is pressed and released in python [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
How can I make a sprite move when key is held down
(6 answers)
Closed 12 months ago.
I'm trying to write a 2D game in Python and I'm trying to call a function when I press and release Spacebar from my keyboard. If I'm using the function "is_pressed()" from keyboard it will be continuously called and glitch my program. Can you help, please?
You have to use the keyboard events instead. 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 and get continuous movement. The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
e.g.:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# do something
# [...]
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
# do something
# [...]

Pygame window immediately opens and closes

Originally after I did some searching here, I found a question with the exact same problem I had:
Pygame window not responding after a few seconds . I reviewed all the answers and tried them, but none of them worked. I tried using for loops to loop through every single event;
run = True
while run:
for event in pygame.event.get():
if event == pygame.QUIT()
run = False
But the window still closed. I also tried:
run = True
while run:
event = pygame.event.get()
if event == pygame.QUIT():
run = False
Which had the same results as the one above.
Can anyone help?
Edit: I use PyCharm and MacOS Catalina.
pygame.QUIT is a constante, but pygame.QUIT() is a call statement. Remove the braces. Anyway, the condition won't work, because you have to compare the type attribute of the event to the event type constant (see pygame.event). Furthermore the : is missing at the end of the if-statement.
if event == pygame.QUIT()
if event.type == pygame.QUIT:
Furthermore the Indentation is not correct:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

Python pygame freezes during music playing

Hello I am building a simple mp3 player. I have it now so it plays a sound fine when I enable the function, but the gui temporarily freezes while waiting for the sound file to finish. I would like to be able to hit stop, exit, etc while the file plays. Any advice?
def Play():
soundfile = 'test.wav'
pygame.mixer.music.load(soundfile)
pygame.mixer.music.play(0)
events = pygame.event.get()
while pygame.mixer.music.get_busy():
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.Q:
sys.exit()
elif event.type == pygame.MOUSEMOTION:
print "mouse at (%d, %d)" % event.pos
When pygame.event.get() is not being called, the game window will freeze. Since you're calling pygame.event.get() outside of your while loop, the window event queue is only getting flushed once. The simple solution is to just replace the events variable with pygame.event.get() directly inline so that it gets called continuously while get_busy() returns true.
In general for these situations, a better solution would be a little refactoring where you have pygame.event.get() called once centrally in your program and instead of calling a function called Play() that handles managing your event queue, you instead have a function called handlePlayForOneFrame(list_of_relevant_events_this_frame).

Pygame, screen only updates when exiting pygame window?

I'm fairly new to pygame and i needed some help because my code is not working properly.
Okay so here's the problem: I want the screen to turn white when i run it but it remains black, however when i press the exit, it turns white for about a second and then closes.
This also happens when i put a picture (like the player.png) it appears for about a second before exiting. I don't know what i'm doing wrong,
please help fix the code and explain why it is happening?
here is the code:
import pygame
pygame.init()
screen = pygame.display.set_mode((640,480))
image = pygame.image.load('player.png')
gameExit = False
gameLoop = True
pygame.display.update()
white = (255,255,255)
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
for event in pygame.event.get():
screen.fill(white)
pygame.display.update()
pygame.quit()
quit()
PS. I don't get any errors
Python is indentation sensitive. In your code, you call pygame.display.update() only once your main loop ends.
Also, you only paint the background white in the case there's an event in the event queue between the two for loops, and then you fill the background for every event in the queue.
Note that this could also lead to a situation in which your QUIT event is "swallowed" by the second loop.
So this
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
for event in pygame.event.get():
screen.fill(white)
pygame.display.update()
should be
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
screen.fill(white)
pygame.display.update()
Ok, so I agree with Sloth, but instead of doing all the screen updating and filling and blitting at the end, making the script less readable, you should make like an animate function that runs at the end of the while loop each time, and put the blitting and screen updating stuff in that function. Sorry for the run-on sentence.
Oh and you don't need your GameLoop variable.

Categories

Resources