So im just following a simple Pygame tutorial on youtube and it should work but for some reason every time i try to close the app it crashes and doesn't close normally, here's the code:
import pygame
# Initialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((800, 600))
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# Run game until x is pressed
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
im using the latest version of pycharm and python.
Add a pygame.display.quit() at the last line, you need to tell it to close the window.
Related
I am trying to recreate this pygame but i keep getting a black screen without anything displayed. I get no error's so i don't know where i need to start.
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
test_surface = pygame.image.load('graphics/Sky.png')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(test_surface,(0, 0))
pygame.display.update()
clock.tick(60)
The problem got solved after reinstalling python and pycharm. Not sure if both were needed but it did the trick for me, thanks all.
I'm new to pygame. I have written the following code, but the generated window doesn't allow me to close it.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Looping and the game running are two different things. If you want to close the game after the loop ends, you should do so with pygame.quit(). Importing sys and adding sys.exit() as afterwards lets you exit the Python script altogether. Depending on which IDE you use, this may not happen automatically.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
I'm a Macbook user and I have just recently started learning programming games using python.
my issue is that every time I try to close the screen it doesn't close.
import pygame
import sys
pygame.init()
width = 800
hight = 600
screen = pygame.display.set_mode((width , hight))
game_over= False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT :
sys.exit()
When I point the cursor to the screen it turns to a circle... Cursur, so usually I just press force to quit "on the snake icone" in the dock
So how can I fix this and make the screen close just by pressing the close button ?
I'm using python 3.
Thank you.
Full edited and tested code :
import pygame
import sys
pygame.init()
width = 800
hight = 600
running = True
screen = pygame.display.set_mode((width , hight))
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT :
pygame.quit()
sys.exit()
Test on Linux mint with visual studio code. Tested more than 5 times. Expected results.
Currently, when I play any audio using pygame's mixer I get an audio stream called Python (v3.8): Audio Stream with the python logo on the side. I'd like to change the name and logo to that of my application, how can I do this?
Here is a screenshot of the audio stream:
https://imgur.com/mJQ5FY9.png
Here is an example of the code:
from pygame import mixer
mixer.init()
mixer.music.load("forest.ogg")
mixer.music.play()
while not mixer.get_busy():
pass
What you see is a system generated window, but not a Pygame window. Some systems may not even create a window in this case. You cannot change the symbol and the label of this window with Pygame.
As an option you can create your own window and set the caption with pygame.display.set_caption() and the icon with pygame.display.set_icon() (On some systems, the icon must be set before the window is created)
Minimal example:
import pygame
song_name = "zelda_chest.mp3"
pygame.init()
font = pygame.font.SysFont(None, 40)
title_surf = font.render(song_name, True, (255, 255, 0))
pygame.display.set_icon(pygame.image.load("MusicNote.png"))
window = pygame.display.set_mode((max(400, title_surf.get_width()+20), 100))
pygame.display.set_caption(song_name)
pygame.mixer.init()
pygame.mixer.music.load(song_name)
pygame.mixer.music.play()
window_center = window.get_rect().center
clock = pygame.time.Clock()
run = True
while run and pygame.mixer.music.get_busy():
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(title_surf, title_surf.get_rect(center = window_center))
pygame.display.flip()
pygame.quit()
exit()
You need:
pygame.display.set_icon(your_icon)
your_icon needs to be a pygame.Surface (e.g. a loaded image (pygame.image.load(path))
I have a problem when going from fullscreen mode to a smaller screen in Pygame. The window appears on the top left and I can't see any exit button, nor can I drag it to the center. Here is the code I am using:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
run = True
while run:
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
# Exit fullscreen with escape key
if event.key == pygame.K_ESCAPE:
if screen.get_flags() & FULLSCREEN:
pygame.display.set_mode((400, 400))
else:
pygame.display.set_mode((0, 0), FULLSCREEN)
I have tried to center the smaller window on the screen which usually works fine, but not in this case.
import os
# .........
if screen.get_flags() & FULLSCREEN:
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.display.set_mode((400, 400))
else:
pygame.display.set_mode((0, 0), FULLSCREEN)
Any ideas how to center the window after exiting fullscreen?
This is a bug in pygame.
If you need this behavior, someone who reported the issue on github found a workaround (https://github.com/pygame/pygame/issues/2360)
Hopefully it'll be fixed in 2.0.2, I've written a patch to fix it (https://github.com/pygame/pygame/pull/2460)