简体   繁体   中英

My menu for pygame is not working and does not show up onscreen

import pygame
import sys

#debugging version
print(pygame.ver)

# Initialise pygame
pygame.init()

# Create screen
screen = pygame.display.set_mode((1080, 720))

# Title
pygame.display.set_caption("Lunar Dungeon")

# Icon Here
icon = pygame.image.load('moon.png')
pygame.display.set_icon(icon)

# Game loop
running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            sys.exit()

# Background colour - RGB

    screen.fill((0, 0, 128))
    pygame.display.update()

#Menu
class Game():
    def __init__(self):
        pygame.init()
        self.running, self.playing = True, False
        self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False
        self.DISPLAY_W, self.DISPLAY_H = 480, 270
        self.display = pygame.Surface((self.DISPLAY_W,self.DISPLAY_H))
        self.window = pygame.display.set_mode(((self.DISPLAY_W,self.DISPLAY_H)))
        self.font_name = '8-BIT WONDER.TTF'
        #self.font_name = pygame.font.get_default_font()
        self.BLACK, self.WHITE = (0, 0, 0), (255, 255, 255)
        self.main_menu = MainMenu(self)
        self.options = OptionsMenu(self)
        self.credits = CreditsMenu(self)
        self.curr_menu = self.main_menu

    def game_loop(self):
        while self.playing:
            self.check_events()
            if self.START_KEY:
                self.playing= False
            self.display.fill(self.BLACK)
            self.draw_text('Thanks for Playing', 20, self.DISPLAY_W/2, self.DISPLAY_H/2)
            self.window.blit(self.display, (0,0))
            pygame.display.update()
            self.reset_keys()



    def check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running, self.playing = False, False
                self.curr_menu.run_display = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    self.START_KEY = True
                if event.key == pygame.K_BACKSPACE:
                    self.BACK_KEY = True
                if event.key == pygame.K_DOWN:
                    self.DOWN_KEY = True
                if event.key == pygame.K_UP:
                    self.UP_KEY = True

    def reset_keys(self):
        self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False

    def draw_text(self, text, size, x, y ):
        font = pygame.font.Font(self.font_name,size)
        text_surface = font.render(text, True, self.WHITE)
        text_rect = text_surface.get_rect()
        text_rect.center = (x,y)
        self.display.blit(text_surface,text_rect)


#pygame.quit()

The menu for my game does not appear onscreen and thus I am unable to check if it functions correctly. It only display the blue background I have made, I am unsure as to why this is as I am still relatively new to Python. I have not coded the game itself yet as I am working my way forwards from the start menu.

The Game class will never be executed:

Your Game Loop (the one after the # Game Loop comment) uses a while running .
running can only be False if the if event.type == pygame.QUIT: is hit.
if this condition is hit sys.exit() is also executed, which stops executing the program.
therefore the code after the Game Loop, including the Game class, cannot be executed.

这个节目很史诗,我希望有一天我能亲眼看看broski ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM