因此,每当玩家终生用完时,我希望屏幕返回菜单/标题屏幕。在游戏开始时,一切似乎都在起作用。但一旦它死了,它仍然会回到标题屏幕。 "退出"按钮仍然可以退出游戏,但是" Run!"无论我按下回车键多少次,按钮都不再起作用。
知道它有什么问题吗?
调用标题屏幕的代码: game.py:
def reason(self, *args, **kwargs):
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
self.level_manager.current_state.exit()
self.music.stop()
return title.Title()
if Game.lives == 0:
self.music.stop()
return title.Title()
title.py:
import pygame
from pygame.locals import *
import sys
import time
import random
import state
import game
class Title(state.State):
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
def __init__(self):
display = pygame.display.get_surface()
self.background = pygame.image.load("data/sprites/background.png").convert()
self.font_renderer = pygame.font.Font("data/fonts/GothicSolid.ttf", 40)
self.title_renderer = pygame.font.Font("data/fonts/GothicSolid.ttf", 65)
self.title = self.title_renderer.render("Road to Graduation 1", True, (255, 255, 255))
self.title_color = "white"
self.start_option = self.font_renderer.render("Run!", True, (132, 92, 186))
self.exit_option = self.font_renderer.render("Quit", True, (0, 0, 0))
self.menu_choice = 1
self.music = pygame.mixer.Sound("data/title_highscore.wav")
self.music.play(loops=-1)
def enter(self):
pass
def exit(self):
pass
def reason(self, *args, **kwargs):
for event in pygame.event.get():
if event.type == KEYUP:
if event.key in (K_UP, K_DOWN):
self.next()
if event.key == K_RETURN:
if self.menu_choice == 1:
self.music.stop()
return game.Game()
elif self.menu_choice == 2:
pygame.quit()
sys.exit()
def act(self, *args, **kwargs):
self.animate_title()
display = pygame.display.get_surface()
display.blit(self.background, (0, 0))
display.blit(self.title, (display.get_width()/2 - self.title.get_width()/2, 100))
display.blit(self.start_option, (display.get_width()/2 - self.start_option.get_width()/2, display.get_height()/2 - self.start_option.get_height()))
display.blit(self.exit_option, (display.get_width()/2 - self.exit_option.get_width()/2, display.get_height()/2))
pygame.display.update()
def next(self):
if self.menu_choice == 1:
self.start_option = self.font_renderer.render("Run!", True, (0, 0, 0))
self.exit_option = self.font_renderer.render("Quit", True, (132, 92, 186))
self.menu_choice = 2
else:
self.start_option = self.font_renderer.render("Run!", True, (132, 92, 186))
self.exit_option = self.font_renderer.render("Quit", True, (0, 0, 0))
self.menu_choice = 1