CAKE
CAKE

Reputation: 51

Keep getting an error about pygame not being initialised

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))
blue = 146,244,255 #BACKGROUND COLOUR
width  = 80 
height = 60
x = 200 #X-POSITION OF THE CHARACTER
y = 100 #Y-POSITION OF THE CHARACTER


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()

Upvotes: 1

Views: 41

Answers (1)

Rabbid76
Rabbid76

Reputation: 210918

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()

Upvotes: 2

Related Questions