def handle_events(self):
'''
Handle user input events
'''
# Get the keys that are currently down
keys = pygame.key.get_pressed()
for event in self.input.events:
if event.type == pygame.QUIT:
print("QUIT")
self.quit()
break # No need to process any more events
# Set the username
if event.type == CustomEvent.SET_USER:
print("Username: ", event.text)
self.user = event.text
self.welcome.run()
# Handle the user input screen it it's running
if self.name_enter.is_running():
if not self.name_enter.handle_event(event):
break
continue # Skip the rest of the events while entering username
# Toggle sound
if event.type == pygame.KEYDOWN:
if event.key == self.config.controls.toggle_sound and\
not self.code_editor.is_running():
print("Toggling sound")
self.sound.toggle_game_music()
if event.key == self.config.controls.reset_game and\
not self.code_editor.is_running():
print("Resetting game")
self.reset()
break
if event.key == pygame.K_c and\
not self.code_editor.is_running():
if pygame.key.get_mods() & pygame.KMOD_CTRL:
print("Quit with cntrl-c")
self.quit()
break
# Handle welcome screen events if it's running
if self.welcome.is_running():
if not self.welcome.handle_event(event):
break
continue # Skip the rest of the events while the welcome screen is running
# Handle code editor events if it's running
if self.code_editor.is_running():
if not self.code_editor.handle_event(event):
break
# All the cool event handling happens in the level
if not self.current_level.handle_event(event, keys):
break
评论列表
文章目录