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.
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 recently made a small game and then turned it into an application using pyinstaller. When running, there are two ways to close it. It's fullscreen so clicking the red x button isn't included, but basically I have this
if keys[pygame.K_ESCAPE]:
pygame.quit()
and this
if exit_button.colliderect(cursor_rect) and click==1:
pygame.quit()
When the exit button is clicked, the game shuts down without a problem. The cursor shows the little loading symbol for a brief moment immediately after. However, when escape is pressed, the window closes but a message pops up saying, "Failed to execute script". I am on windows 10, the game was made with pygame on pycharm, and the exe was made with pyinstaller --onefile -w game.py.
Maybe it's a bug in the code, but it works perfectly fine in the IDE and I'm doing nothing different. I can also confirm it's not missing images or media. Thanks.
pygame.quit() doesn't quit your program, it uninitializes all pygame modules. It's meant to be used when you want the program to continue but without pygame. Your program will crash if you use certain pygame functions after calling pygame.quit().
If you want to terminate your program, simply let the program end naturally or call quit(). Many people like to call pygame.quit() before calling quit(), but this is actually not necessary.
Just want to add a little something.
As you'll see in most pygame projects (source), we use both pygame.quit() and sys.exit().
Assuming that your intention is to shut down both the pygame program and the Python script it's running from, you'll use something like the below code.
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
First pygame.quit() is called to stop all the pygame modules that activate when you run pygame.init(), followed by sys.exit() which stops the python script.
This is how I've been doing it for several projects that I've created and I've never had any errors while exiting the program, whether it be manually or through the code.
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'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()