【问题标题】:Function won't run on command函数不会在命令上运行
【发布时间】:2021-10-13 14:32:51
【问题描述】:

游戏从主菜单开始,当鼠标悬停在 start 上时按下回车键,fade 函数应该运行,然后是 black_screen 函数,最后是游戏函数。但是,main_menu 函数运行,并且在 keypress 事件上,仅运行语句中的播放声音。

import os
import pygame 
import pygame_menu
from pygame import mixer
from pygame.locals import *
import sys 


# Initialization

pygame.mixer.init()
pygame.init() 


 
# Center the Game Application
os.environ['SDL_VIDEO_CENTERED'] = '1'
 
# Game Resolution
width=800
height=600
screen=pygame.display.set_mode((width, height))
 
# Text Renderer
def text_format(message, textFont, textSize, textColor):
    newFont=pygame.font.Font(textFont, textSize)
    newText = newFont.render(message, 0, textColor)
 
    return newText
 
 
# Colors
white=(255, 255, 255)
black=(0, 0, 0)
gray=(50, 50, 50)
red=(255, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
yellow=(255, 255, 0)
AMP_PINK=(242, 145, 233)
 
# Game Fonts
hamberger = "./assets/fonts/hamberger.ttf"
 
 
# Sounds
menukeys = mixer.Sound("assets\sounds\keypress.wav")


mixer.music.load("assets\sounds\strolling.wav")
mixer.music.play(-1)


# Game Framerate
clock = pygame.time.Clock()
FPS=60



def game():
    main_menu()
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

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




def black_screen():
    blacc = pygame.Surface((width,height))
    blacc.fill((0,0,0))
    pygame.display.update()
    pygame.time.delay(2)
    game()


def fade(): 
    global width, height
    fade = pygame.Surface((width,height))
    fade.fill((0,0,0))
    for alpha in range(0, 300):
        fade.set_alpha(alpha)
        screen.blit(fade, (0,0))
        pygame.display.update()
        pygame.time.delay(6)
        black_screen()


# Main Menu
def main_menu():
 
    menu=True
    selected="start"
 
    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_UP:
                    mixer.Sound.play(menukeys)
                    selected="start"
                elif event.key==pygame.K_DOWN:
                    mixer.Sound.play(menukeys)
                    selected="quit"
                if event.key==pygame.K_RETURN:
                    #Starts the game
                    mixer.Sound.play(menukeys)
                    if selected=="start":
                        fade()
                    if selected=="quit":
                        mixer.Sound.play(menukeys)
                        pygame.quit()
                        quit()
 
        # Main Menu UI
        screen.fill(AMP_PINK)
        title=text_format("SNATCHED", hamberger, 90, black)
        if selected=="start":
            text_start=text_format("START", hamberger, 75, white)
        else:
            text_start = text_format("START", hamberger, 75, black)
        if selected=="quit":
            text_quit=text_format("QUIT", hamberger, 75, white)
        else:
            text_quit = text_format("QUIT", hamberger, 75, black)
 
        title_rect=title.get_rect()
        start_rect=text_start.get_rect()
        quit_rect=text_quit.get_rect()
 
        # Main Menu Text
        screen.blit(title, (width/2 - (title_rect[2]/2), 80))
        screen.blit(text_start, (width/2 - (start_rect[2]/2), 300))
        screen.blit(text_quit, (width/2 - (quit_rect[2]/2), 360))
        pygame.display.update()
        clock.tick(FPS)
        pygame.display.set_caption("Snatched - Main Menu")


#Initialize the Game
main_menu()
pygame.quit()
quit()

【问题讨论】:

  • 函数和函数内部的pygame.Surface都被称为fade

标签: python python-3.x pygame


【解决方案1】:

名称fade 使用了两次。该函数和该函数内的pygame.Surface 对象都称为fade

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多