如何修复pygame菜单(空间入侵者)?

2024-04-25 11:39:45 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一个游戏,请原谅我的代码太乱了。我正在制作一个太空入侵者游戏,我实现的一切都很好(精灵、游戏功能、音乐、暂停屏幕等)。我想实现一个非常简单的菜单屏幕,如果你按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年的,有类似问题的网站没有解释如何解决。请帮忙


Tags: importevent游戏if屏幕display菜单screen
1条回答
网友
1楼 · 发布于 2024-04-25 11:39:45

首先,应该将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))

丢失的代码会像这样放入main循环中

...
# 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()

这应该行得通

请注意,您需要将menu设置为True才能进入菜单

相关问题 更多 >