【问题标题】:How to fix pygame menu (space invaders)?如何修复 pygame 菜单(太空入侵者)?
【发布时间】:2020-04-25 16:59:42
【问题描述】:

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

【问题讨论】:

    标签: python menu pygame


    【解决方案1】:

    首先你应该把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()
    

    这应该可以工作

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

    【讨论】:

    • 我有一个问题,通过绘制游戏的代码,你的意思是像整个其余的代码一样?
    • 与实际游戏有关的所有内容,例如绘制玩家、绘制敌人、碰撞检测等。因为您不希望(也不需要)在菜单中执行该代码.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多