I've been using pygame inside a conda environment. The installation went well, but whenever I call flip, it doesn't open any window from the terminal.
Here's the code that's supposed to open the window:
screen.blit(background_image, (0, 0))
pygame.display.flip()
pygame.display.update()
After pygame starts, it shows this message:
WARNING: 140: This application, or a library it uses, is using the
deprecated Carbon Component Manager for hosting Audio Units. Support
for this will be removed in a future release. Also, this makes the
host incompatible with version 3 audio units. Please transition to the
API's in AudioComponent.h.
Here's the entire code.
It's a sudoku game that should display a board with Sudoku being solved.
(This answer is not directed to the original question but to anyone who might have the same problem)
In the game loop you have to process/check events so that pygame knows your game hasn't crashed:
screen = pygame.display.set_mode((1000, 500))
while True:
for event in pygame.event.get():
# process events
# Update your sprites
pygame.display.update()
Related
I want to close the Pygame-Window after a specific event occurred and open it again after another event occurred without stopping the whole script. When I stop pygame with pygame.quit() and want to register a font or do whatever it gives me an error that says that pygame is not initialized so I coded it so it would call pygame.init() again after it closed the pygame window but as I read here pygame thinks it is still initialized and when I call pygame.init() again it won't really do anything. I don't get the error anymore but the code gets stuck at that point where I want to use pygame again, after I closed the pygame window with pygame.quit() and initialized it again, and then after 2 seconds, it just stops the script. How can I prevent pygame from doing this and reopen the pygame window without restarting the script?
Thank you.
What you are looking for is pygame.display.quit()
the following script display a pygame window for 1 second then close it
import pygame
import time
pygame.display.set_mode((1024, 668))
time.sleep(1)
pygame.display.quit()
pygame.quit() is the counterpart of pygame.init(). Not what you're trying to do.
I am writing a game using Pygame. When the game window opens, I currently only have the program call pygame.display.update after the game has changed something that shows on the screen. That works fine for limiting screen refreshes to only when necessary. I have discovered a side effect of moving the game window that causes the screen to get corrupted, which requires the program to force a refresh even if the program itself doesn't require one.
My question is if there is a pygame event (I didn't see one) or something else, that I can use to force the game to refresh the screen after a window move.
Okay, I happened across a code snippet that reports events to the console here:
http://www.poketcode.com/en/pygame/events/index.html
Running this was useful because I noticed that every time I moved the window partially off-screen, pygame triggered a VideoExpose event when the window area moved back on screen.
So, I added the following bit of code to my event loop and it worked great!:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.VIDEOEXPOSE:
game.update_panels(force=True)
game.update_panels(force=False)
I'm learning how to use pygame, and I'm just trying to open up a window for the game I'm creating.
The program compiles fine, and I tried drawing a circle to see if that would change anything but in both scenarios I still just get a blank window that freezes. My computer has plenty of storage space and only 2 applications open, and yet this is the only application that is freezing.
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
I have to force quit Python because it stops responding. I have Python version 3.7.4 and Pygame version 1.9.6.
Any advice?
A minimal, typical PyGame application
needs a game loop
has to handle the events, by either pygame.event.pump() or pygame.event.get().
has to update the Surface whuch represents the display respectively window, by either pygame.display.flip() or pygame.display.update().
See also Python Pygame Introduction
Simple example, which draws a red circle in the center of the window: repl.it/#Rabbid76/PyGame-MinimalApplicationLoop
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
# main application loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the display
window.fill(0)
# draw the scene
pygame.draw.circle(window, (255, 0, 0), (250, 250), 100)
# update the display
pygame.display.flip()
I'm working with pygame on a graphical application, involving some video computation, and mouse events listening. I'm using a raspberry 3, raspbian jessie and python2.7.
As the title said : i'm loosing some mouse events, especially when the CPU load is high. I managed to reproduce this behavior in this small exemple :
import pygame
import time
pygame.init()
pygame.display.set_caption('Crash!')
window = pygame.display.set_mode((300, 300))
running = True
Rectplace = pygame.draw.rect(window, (255, 0, 0),(100, 100, 100, 100))
pygame.display.update()
while running:
time.sleep(0.1)
for event in pygame.event.get():
print(`event`)
if event.type == pygame.QUIT:
running = False
When running this script, most of the mouse wheel events (buttons 4&5) are discarded on fast roll. Removing the time.sleep(0.1),that simulate CPU load, make the event listener perfectly reliable.
As i can't remove the slow computation part, nor optimize it more, what should i do to get back these events ?
Thank you for sharing your brains !
My guess is that pygame use a limited size circular event queue. When full, each new event replaces the oldest event. If you get more events than you can ever handle, then let them go, as you will have to discard them anyway.
If you have sporadic slow computations, so that catching up may be feasible, then you must break up the computation into pieces short enough in time that you can get events before the default queue is full. When you get them, either process immediately or put into a larger catch_up queue. The best way to do that depends on the details of the code.
Or investigate the suggested thread solution.
I just started learning how to use Pygame earlier this week. While messing around with it I discovered that if I make a fullscreen window and leave it open for about 10 seconds and close it, all the other windows I have open will shrink in size and my screen is at a lower resolution for a few seconds.
These things happen with this chunk of code:
import pygame
pygame.init()
pygame.display.set_mode((500, 500),
pygame.FULLSCREEN)
running = True
while running is True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Why would this code change the state of my other windows and change my screen resolution temporarily?
I am working with Pygame 2.0.1 with Python 3.9.1 using PyCharm 2020.3.1 on macOS Big Sur 11.1.
Change
pygame.display.set_mode((500, 500), pygame.FULLSCREEN)
to
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
Pygame will automatically detect the screen size.