This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
I just bought an M1 MacBook Air today for coding a pygame project, but at very beginning I had an issue with pygame video system. it won't initialize pygame window. The following code snippet works in PC, but M1 Mac just won't initialize it. the error message I got is "pygame error: video system not initialize"
I am not sure if this issue is just for M1 Mac or it is an issue for all Mac users. Does anyone know the answer and solution for this error?
Thank you very much!
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.quit():
run = False
I initially thought this was an M1 mac problem, since pygame doesn't have good support for M1 mac yet.
However I see the problem:
if event.type == pygame.quit():
should be
if event.type == pygame.QUIT
Right now you're running the function pygame.quit(), which uninitializes your video system.
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 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.
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()
I am currently making a basic 2D platform game and all I have made so far is the background for the start menu. But when I run it does not run my game. Once in a while it will run it and everything will show up but there is a black block covering part of the window?
Can someone explain why this is?
bif=("cloud.jpg")
import pygame,sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((813,555),0,32)
pygame.display.set_caption('Red Dwarf')
background=pygame.image.load(bif).convert()
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
screen.blit(background,(0,0))
pygame.display.update()
As I answered in your other question already: Indendation...!
Your pygame.display.update() is outside the while loop. This means that it is not called until the while loop has finished and that's not going to happen here, therefore the display is just never ever updated.
Indend the pygame.display.update() to the level of screen.blit(background,(0,0)) and it will work just fine.