Alex
Alex

Reputation: 35

Why is my pygame window opening and immediately closing?

Pygame window opens and the closes immediately.

I have copied the code from a youtube channel and it still opens and closes immediately

import pygame
pygame.init()



class Game():
    def __init__(self):
        self.width = 800
        self.height = 600
        self.win = pygame.display.set_mode((self.width, self.height))


    def run(self):
        run = True

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

        pygame.quit()


Game = Game()

The while loop should be waiting for me to quit but its auto executing the quit function

Upvotes: 1

Views: 340

Answers (2)

user11389575
user11389575

Reputation:

By calling using game = Game() and then game.run(), the code should work.

Upvotes: 0

ggorlen
ggorlen

Reputation: 56865

You need to call your run method as follows:

game = Game()
game.run()

Upvotes: 1

Related Questions