本文整理汇总了Python中Menu.Menu.handleEvent方法的典型用法代码示例。如果您正苦于以下问题:Python Menu.handleEvent方法的具体用法?Python Menu.handleEvent怎么用?Python Menu.handleEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Menu.Menu
的用法示例。
在下文中一共展示了Menu.handleEvent方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from Menu import Menu [as 别名]
# 或者: from Menu.Menu import handleEvent [as 别名]
class Game():
FPS = 60
SCREEN_SIZE = (1280, 720)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
DARK_GREY = (10, 10, 10)
MUSIC_FILE = 'data/singsingsing.ogg'
def __init__(self):
pygame.init()
self.font = pygame.font.SysFont('sans-serif', 18, True)
self.surface = pygame.display.set_mode(self.SCREEN_SIZE, 0, 32)
self.state = 'intro'
self.hiscores = Score.read_high_score()
self.menu = Menu(("Start game", "Quit"))
pygame.mixer.music.load(self.MUSIC_FILE)
pygame.mixer.music.play(-1)
def start(self):
while True:
if self.state == 'intro':
# Show the intro and when it's done we move directly to game state
intro = Intro(pygame, self.surface);
new_state = intro.show()
self.state = new_state
elif self.state == 'menu':
# Show the menu and run the menu loop
self.hiscores = Score.read_high_score()
self.show_menu()
elif self.state == 'game':
# Run a game and run the game loop
self.load_level()
self.game_loop()
elif self.state == 'quit':
# Quit, we're done.
pygame.quit()
sys.exit()
# Contains the main menu loop and handles starting and quitting while showing the high scores
def show_menu(self):
pygame.mouse.set_visible(1)
self.menu.drawMenu()
while True:
pygame.time.Clock().tick(self.FPS)
for event in pygame.event.get():
self.menu.handleEvent(event)
if event.type == Menu.MENUCLICKEDEVENT or event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
if event.text == "Quit":
self.state = 'quit'
return
elif event.text == 'Start game':
self.state = 'game'
self.menu.deactivate()
return
elif event.type == KEYDOWN and event.key == K_RETURN:
self.state = 'game'
self.menu.deactivate()
return
if self.menu.isActive():
self.menu.drawMenu()
Score.draw_high_scores(self.hiscores, self.surface)
pygame.display.flip()
# Loads and resets a level
def load_level(self):
self.up = False
self.speed = 6
self.nameinput = None
self.level = Level(self)
# The main game loop that updates the level as the game progresses
def game_loop(self):
pygame.mouse.set_visible(0)
while True:
pygame.time.Clock().tick(self.FPS)
events = pygame.event.get()
for event in events:
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
self.state = 'quit'
return
elif self.state == 'game':
if event.type == KEYDOWN:
if event.key == K_UP:
self.up = True
if event.type == KEYUP:
if event.key == K_UP:
self.up = False
elif self.state == 'gameover':
if event.type == KEYUP:
if event.key == K_RETURN:
if self.nameinput != None:
name = self.nameinput.value
if name == '':
name = 'no name'
Score.update_highscore(name, self.level.get_score(), self.hiscores)
self.state = 'menu'
return
#.........这里部分代码省略.........