0

I (tried) to create a game menu and placed my GameLoop in the Class = Menü. When I try to start the code it highlights the line pygame.display.update() and tells me that: Display mode not set.

Class Menü(Highscore):

    def __init__(self):
        Highscore.__init__(self)
        #Fenster erstellen
        self.fenster_height = 1000  # Fensterhöhe definieren
        self.fenster_width = 600  # Fensterbreite definieren
        self.screen = pygame.display.set_mode((self.fenster_height, self.fenster_width))
        self.run = True
        #self.start_img = self.image.load('pong_test.png').convert_alpha()  # Bild für start_img ausw#hlen
        #self.quit_img = self.image.load('pong_test.png').convert_alpha()
        #self.auslesen = Highscore.readHS()

        pygame.display.set_caption('Spielekiste')
        #pygame.start_img = pygame.image.load('pong_test.png').convert_alpha()
        #pygame.quit_img = pygame.image.load('pong_test.png').convert_alpha()
        #self.screen.fill((96, 144, 189))  # Screen mit Farbe (255,255,255) füllen

    def runMenü(self):


        while True:  # Während "run = TRUE"
            for event in pygame.event.get():
                       # fenster schließen
                if event.type == pygame.QUIT:

                    pygame.quit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    print('test')

            pygame.display.update()  # Fenster wird aktualisiert
menu = Menü

menu.runMenü(menu)
Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
Shpend
  • 5
  • 2

1 Answers1

0

First of all, in order to create a class in python, we use the class keyword, whereas in your code, you used the word Class, which will throw an error since it isn't a keyword in python.

So replace this line: Class Menü(Highscore): with this line: class Menü(Highscore):

Now to address your issue, see this answer. Basically, there are two ways in which you can prevent the error that you are getting:

The First Way

run = True
while run:  # Während "run = TRUE"
    for event in pygame.event.get():
               # fenster schließen
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            print('test')

    pygame.display.update()  # Fenster wird aktualisiert

pygame.quit()

In your current game loop (the while loop inside the runMenü function), when the pygame.QUIT event is triggered, you immediately quit the program without stopping your game loop. So if you replace the code inside your runMenü function with the code I wrote, you won't get an error.

The Second Way

while True:  # Während "run = TRUE"
    for event in pygame.event.get():
               # fenster schließen
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)
        if event.type == pygame.MOUSEBUTTONDOWN:
            print('test')

    pygame.display.update()  # Fenster wird aktualisiert

Here, I'm exiting the program after using pygame.quit() to quit pygame. That way, your program won't throw an error because I'm terminating it with sys.exit(0). Don't forget to add import sys at the top of your code (under import pygame).

I would personally use the first method because it stops the loop and then terminates the program, which I think is better.

Modified Code:

import pygame
pygame.init()

class Menü(Highscore):
    def __init__(self):
        Highscore.__init__(self)
        #Fenster erstellen
        self.fenster_height = 1000  # Fensterhöhe definieren
        self.fenster_width = 600  # Fensterbreite definieren
        self.screen = pygame.display.set_mode((self.fenster_height, self.fenster_width))
        self.run = True
        #self.start_img = self.image.load('pong_test.png').convert_alpha()  # Bild für start_img ausw#hlen
        #self.quit_img = self.image.load('pong_test.png').convert_alpha()
        #self.auslesen = Highscore.readHS()

        pygame.display.set_caption('Spielekiste')
        #pygame.start_img = pygame.image.load('pong_test.png').convert_alpha()
        #pygame.quit_img = pygame.image.load('pong_test.png').convert_alpha()
        #self.screen.fill((96, 144, 189))  # Screen mit Farbe (255,255,255) füllen

    def runMenü(self):
        run = True
        while run:  # Während "run = TRUE"
            for event in pygame.event.get():
                       # fenster schließen
                if event.type == pygame.QUIT:
                    run = False
                if event.type == pygame.MOUSEBUTTONDOWN:
                    print('test')

            pygame.display.update()  # Fenster wird aktualisiert

        pygame.quit()


menu = Menü()
menu.runMenü()
Roni
  • 597
  • 5
  • 21
  • Oh nice it works. Thanks man :') – Shpend Feb 11 '22 at 11:00
  • @Shpend I'm happy to hear that :) Please accept the answer by clicking the check mark. That way, other people with the same issue will be able to see this answer and benefit from it. – Roni Feb 11 '22 at 11:52