本文整理匯總了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
#.........這裏部分代碼省略.........