I have created a window in pygame, the only problem is that I can't exit the window without it crashing. Is there a problem with my code that causes this?
import pygame
pygame.init()
gamedisplay = pygame.display.set_mode ((800,600))
pygame.display.set_caption ('snake')
pygame.display.update()
while True:
pass
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.quit()
quit()
Get rid of this:
while True:
pass
Related
I am trying to understand how to display fonts in pygame. But i get an error saying pygame.error: Library not initialized
This error happens after i press the cross button or quit my pygame window.
Can anyone tell my why this error is happening and how can i fix it please?
import pygame
from pygame.locals import *
import sys
from win32api import GetSystemMetrics
pygame.init()
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)-64
WIDTH_HEIGHT = (WIDTH, HEIGHT)
WINDOW = pygame.display.set_mode(WIDTH_HEIGHT)
pygame.init()
font = pygame.font.Font('freesansbold.ttf', 32)
text = ""
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
running = False
text = font.render('Hi', True, (255,255,255))
WINDOW.blit(text, (0, 0))
pygame.display.update()
This is one way to exit (just finishes the program):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
This is another way (a bit more forceful):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.draw.rect(screen, (255,0,0),(400,300,50,50))
pygame.display.flip()
As #Furas writes, the screen update code is not being called from within the loop. Python uses indentation to designate code blocks, so if the function call (or other code section) is not indented to the correct column, it is literally a completely different set of operations.
Since a piece of sample code is worth a thousand words:
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
# Handle user-events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Re-draw the screen
pygame.draw.rect(screen, (255,0,0), (400,300,50,50))
pygame.display.flip()
pygame.quit()
sys.exit()
I'm relatively new to python and very new to pygame. I'm trying to use pygame. All programs seem to work fine, except when I try to quit. The window freezes ("application not responding") and I have to force quit it. I'm using OSX, python 3.6, and running it through sublime text if that matters. Code is below:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
done = True
pygame.display.quit()
pygame.quit()
Thanks for your help!
Try this one, it works for me:
import sys
import pygame
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
you can just do this:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
this is what i do
or you can use this:
import pygame
pygame.init()
running = True
width, heigth = 800, 600
size = (width, heigth)
screen = pygame.display.set_mode(size)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
don't use pygame.display.quit()
import pygame
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
pygame.display.update()
excape = False
while not excape:
for dork in pygame.event.get():
print(dork)
if pygame.event == pygame.QUIT:
pygame.quit()
quit()
Here the print(dork) is working but when i click the exit button of the window it doesn't quit at all..
So how do i both print events and quit the application in 1 while loop?
First of all, you should update the screen in the while not excape loop.
Secondly, set the excape to True if pygame.event is equal to pygame.QUIT.
So your code will look like this:
import pygame, sys
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
excape = False
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
excape = True
pygame.quit()
sys.exit()
pygame.display.update()
You need to cycle through EVERY pygame event and check if the event is a quit.
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
excape = True
I'm trying to run code from a tutorial I found online.
But when I run this code, my background image does not appear, even if it loaded correctly.
Why?
bif = "castle.jpg"
import pygame, sys, pygame.mixer
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1280,960),0, 32)
background = pygame.image.load(bif).convert()
pygame.display.set_caption("castlevania ultimate")
hit_sound = pygame.mixer.Sound("02.wav")
hit = False
if hit is True:
hit_sound.play()
sound = pygame.mixer.Sound("castlevania_1.wav")
sound.play()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
You never call pygame.display.update() in your while loop.
Python is indentation sensitive, so the loop should look like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
Note that pygame.display.update() is now inside the while loop.