When I run the following code I get an error. Most of the other similar posts talks about pygame.quit()
sys.exit() which I have, but still see the error
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
is_playing= True
while is_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
pygame.quit()
sys.exit()
The error is
Traceback (most recent call last):
File "C:/Users/.............................", line 10, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
pygame.quit() should be out of the loop:
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
is_playing= True
while is_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
pygame.quit()
sys.exit()
Otherwise after the first iteration of the loop, pygame.quit() would have been called and on the second iteration the error would have occured.
You have to care about the Indentation
While pygame.quit() has to be done after the main application loop (as mentioned in another answer and the comments), drawing the scene and the update of the display has to be done in the main application loop rather than the event loop:
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
# application loop
is_playing= True
while is_playing:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
#<--| INDENTATION
# draw the scene
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
#<--| INDENTATION
# quit pygame
pygame.quit()
sys.exit()
Note, the event loop is executed once for each event, but the application loop is executed once for each frame.
Related
I keep getting an error about system not being initialised. The error is about the pygame.display.update()
Where is this meant to go?
import pygame #IMPORTS THE PYGAME CLASSES, METHODS AND ATTRIBUTES
from pygame.locals import * #IMPORTING ALL PYGAME MODULES
pygame.init() #INITIALISING PYGAME
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("On The Run")
blue = 146,244,255 #BACKGROUND COLOUR
width = 80
height = 60
x = 200 #X-POSITION OF THE CHARACTER
y = 100 #Y-POSITION OF THE CHARACTER
player1 = pygame.image.load('assets/characterMove3.jpg') #DISPLAYING THE IMAGE ONTO THE SCREEN
player1 = pygame.transform.scale(player1,(width,height)) #SCALING THE IMAGE TO SUITABLE DIMENSIONS
WINDOW.blit(player1,(x,y))
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False #WE WILL QUIT THE GAME AS THE VARIABLE run IS NOW FALSE
pygame.quit() #IT WON'T SHOW THE MOST RECENT THING I DREW UNLESS I MANUALLY UPDATE IT
pygame.display.update()
game_loop()
The problem is that pygame.quit() is called in the application loop. pygame.quit() deinitializes all Pygame modules and crashes all subsequent Pygame API calls. pygame.quit() must be the very last Pygame API call. Call pygame.quit() after the application loop:
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pygame.quit() <-- DELETE
pygame.display.update()
pygame.quit() # <-- INSERT
game_loop()
I'm creating a simple pygame program (PyGame 1.9.6 on Python 3.7), but some of the code inside my while loop doesn't seem to work. When I run the program, the window opens, but the screen doesn't fill with black, nor does the window close when I press "x"
import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
In the console, "tick" appears like normal and "event detected" appears after pressing any keys or mouse click. I don't get any errors when I run it.
If event == pygame.QUIT: should be if event.type == pygame.QUIT:
I usually use that event like this:
if event.type == pygame.QUIT:
pygame.quit()
quit()
Just as #Telan said
if event==pygame.QUIT:
should be
if event.type==pygame.QUIT:
However to properly shutdown pygame, pygame.quit() is important to shutdown pygame modules which is the reverse of pygame.init()
While sys.exit() is used to properly shutdown the main python program.
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
Full code is shown below. Enjoy!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()
I recently started learning pygame but I've had an error which shows pygame not responding after some time. It would really help if someone could find my bug! Here's my code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen,BLUE,(200,150,100,50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
From my limited knowledge of pygame, it is likely that you set running = True and did a while True: statement, causing too much memory to be used and the script to crash after some time.
You need a proper termination that uses pygame.quit(), followed by sys.exit(). Setting running = False does not terminate the script.
Try this code:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
sys.exit()
Reference
Try adding a pygame.quit() at the end of your code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen, BLUE, (200, 150, 100, 50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
pygame.quit()
I am making a new game in pygame and the blit function just is not working for me, it should be simple but I just don't know what's wrong.
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
game = True
WIDTH = 160
HEIGHT = 144
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
level = pygame.image.load('RBYG/Level.png')
def redrawGameWindow():
screen.blit(level, (0, 0))
while game:
screen.blit(level,(0, 0))
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
redrawGameWindow()
clock.tick(60)
You're missing pygame.display.flip(), you have to do that after you blit or nothing will happen. Your redrawGameWindow() function should flip the display. Also, you're calling screen.blit(level, (0, 0)) twice in your main loop, which may be unnecessary; try just keeping one of them (just make sure to flip() after).
When I run this code the resizeable option is available but when I click on it to maximize the screen, there is this weird white outline that appears. How would I fix it?.
import pygame
pygame.init()
screen = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
You have to recreate the display Surface in the VIDEORESIZE event (see pygame.event):
import pygame
pygame.init()
screen = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
pygame.display.update()