1

Im a beginner in python just to learn it and have been watching tutorials, specifically Tech w Tim. I was following a tutorial and got the error "pygame.error: display Surface quit". I tried to follow some tips from other similar questions but I couldn't get them to work.

Here is the code.

import pygame

width = 500
height = 500
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Client")

clientNumber = 0

class Player():
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = (x, y, width, height)
        self.vel = 3

    def draw(self, win):
        pygame.draw.rect(win, self.color, self.rect)

    def move(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT]:
            self.x -= self.vel

        if keys[pygame.K_RIGHT]:
            self.x += self.vel

        if keys[pygame.K_UP]:
            self.y -= self.vel

        if keys[pygame.K_DOWN]:
            self.y += self.vel

        self.rect = (self.x, self.y, self.width, self.height)

def redrawWindow(win, player):
    win.fill((255,255,255))
    player.draw(win)
    pygame.display.update()

def main():
    run = True
    p = Player(50, 50, 100, 100, (0, 255, 0))
    clock = pygame.time.Clock()

    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()

        p.move
        redrawWindow(win, p)

main()

This is what I get in the terminal too

Traceback (most recent call last):
  File "c:/Users/Owen/Documents/Oof/Client.py", line 60, in <module>
    main()
  File "c:/Users/Owen/Documents/Oof/Client.py", line 58, in main    
    redrawWindow(win, p)
  File "c:/Users/Owen/Documents/Oof/Client.py", line 41, in redrawWindow
    win.fill((255,255,255))
pygame.error: display Surface quit
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
Ghostly S
  • 13
  • 2

1 Answers1

0

When pygame.quit() is executed, all PyGame modules will be uninstalled. Every further call to a PyGame function throws an exception.
You call pygame.quit () in the middle of the loop, but because the loop executes once to the end, you will get an exception on the following PyGame API calls.
Do not call pygame.quit() in the event loop, but call it once after the application loop:

def main():
    # [...]
    
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                # pygame.quit() <----- delete

        p.move
        redrawWindow(win, p)

    pygame.quit() # <----- insert
Rabbid76
  • 177,135
  • 25
  • 101
  • 146