首页 > 解决方案 > 如何修复 pygame 菜单(太空入侵者)?

问题描述

这是我的第一个游戏,所以请原谅凌乱的代码。我正在制作一款太空侵略者游戏,我实现的一切都运行良好(精灵、游戏功能、音乐、暂停屏幕等)。我想实现一个非常简单的菜单屏幕,如果你按下 C,游戏就会开始。但是,问题在于,无论我在哪里调用菜单功能,总是有问题,这里是代码(我只是要发布菜单功能和主循环,因为我认为不需要其他所有内容)。

import pygame
import random
import math
from pygame import mixer

# Start pygame
pygame.init()

# Create Screen
screen = pygame.display.set_mode((1000, 710))

# Background Image
background = pygame.image.load('background.png').convert_alpha()

# Menu Variables
menu_font = pygame.font.Font('freesansbold.ttf', 65)
menuX = 380
menuY = 250

# Menu Function
def game_intro(x, y):
    menu = True
    while menu:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    menu = False
                if event.key == pygame.K_q:
                    pygame.quit()
                quit()
    # Menu Text
        menu_text = menu_font.render("Space Invaders", True, (255, 255, 255))
        screen.blit(menu_text, (x, y))

        pygame.display.update()

# Game Loop
running = True
while running:

# RGB - Red, Green, Blue
    screen.fill((0, 0, 0))

# Background Image
    screen.blit(background, (0, 0))

----game_intro(menuX,menuY)---IF I PUT IT HERE, THE ACTUAL GAME APPEARS FOR ONE SECOND AND IT GOES BACK TO MAIN MENU-----------

# Making the screen stay still
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

--------game_intro(menuX,menuY)--- IF I PUT IT HERE, THE GAME APPEARS ONLY WHEN 'c' IS BEING HELD DOWN-----------------

*more code*

# Updating
    pygame.display.update()

如果我把它放在 pygame.display.update() 上面,那么同样的事情会发生:游戏出现一秒钟,然后它回到菜单屏幕。我试图到处搜索,但视频要么来自 2014 年,而且有类似问题的网站没有解释如何修复它。请帮忙。

标签: pythonmenupygame

解决方案


首先,您应该将 while 循环扔出您的函数。

def game_intro(x, y):
    # Menu Text
        menu_text = menu_font.render("Space Invaders", True, (255, 255, 255))
        screen.blit(menu_text, (x, y))

丢失的代码像这样放在主循环中

...
# Making the screen stay still
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_c:
                menu = False
            if event.key == pygame.K_q:
                pygame.quit()
...

现在在你的主循环中你需要决定是绘制菜单还是游戏

if menu:
    game_intro(x, y)
else:
    #CODE THAT DRAWS THE GAME

全部一起:

import pygame
import random
import math
from pygame import mixer

# Start pygame
pygame.init()

# Create Screen
screen = pygame.display.set_mode((1000, 710))

# Background Image
background = pygame.image.load('background.png').convert_alpha()

# Menu Variables
menu_font = pygame.font.Font('freesansbold.ttf', 65)
menuX = 380
menuY = 250

# Menu Function
def game_intro(x, y):
    # Menu Text
        menu_text = menu_font.render("Space Invaders", True, (255, 255, 255))
        screen.blit(menu_text, (x, y))

# Game Loop
running = True
while running:

# RGB - Red, Green, Blue
    screen.fill((0, 0, 0))

# Background Image
    screen.blit(background, (0, 0))

# Making the screen stay still
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_c:
                menu = False
            if event.key == pygame.K_q:
                pygame.quit()

    if menu:
        game_intro(x, y)
    else:
        # CODE THAT DRAWS THE GAME

# Updating
    pygame.display.update()

这应该工作

请注意,您需要设置menuTrue某个地方才能进入菜单


推荐阅读