def notify(self, event): if isinstance(event, events.TickEvent): #Every CPU-tick handle input events for event in pygame.event.get(): if event.type == pygame.VIDEORESIZE: resize_ev = events.ResizeWindowEvent(event.w, event.h) self.event_manager.post(resize_ev) if event.type == pygame.QUIT: quit_ev = events.QuitEvent() self.event_manager.post(quit_ev) if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: quit_ev = events.QuitEvent() self.event_manager.post(quit_ev) if not self.joystick: #Handle events generated by keyboard if event.type == pygame.KEYDOWN: key_ev = events.KeyPressed(event.key) self.event_manager.post(key_ev) if event.type == pygame.KEYUP: key_ev = events.KeyReleased(event.key) self.event_manager.post(key_ev) #Handle events generated by mouse if event.type == pygame.MOUSEMOTION: direction_X, direction_Y = pygame.mouse.get_pos() mouse_ev = events.MouseMoved(direction_X, direction_Y) self.event_manager.post(mouse_ev) if event.type == pygame.MOUSEBUTTONDOWN: attack_request_ev = events.MouseButtonDown() self.event_manager.post(attack_request_ev) #Handle events generated by game pad if self.joystick: ''' XBox game pad 2. axis: X 3, Y 4 Sony game pad 2. axis: X 3, Y 2 ''' horiz_axis_pos = self.joystick.get_axis(3) vert_axis_pos = self.joystick.get_axis(2) #Game pad events will be sent every tick #Get second axis values and send this as an event axis_ev = events.AxisMoved(horiz_axis_pos, vert_axis_pos) self.event_manager.post(axis_ev) if self.joystick.get_hat(0): #Get hat values and send this as an event hat_x, hat_y = self.joystick.get_hat(0) hat_ev = events.HatMoved(hat_x, hat_y) self.event_manager.post(hat_ev) if event.type == pygame.JOYBUTTONDOWN: #Check if R1 (on Sony controller) button pressed if self.joystick.get_button(7): attack_request_ev = events.MouseButtonDown() self.event_manager.post(attack_request_ev)
def notify(self, event): if isinstance(event, events.TickEvent): for event in event.pygameEvents: eventPost = None if event.type == QUIT: eventPost = events.QuitEvent() elif event.type == KEYDOWN and event.key == K_ESCAPE: eventPost = events.QuitEvent() elif event.type == KEYDOWN and event.key == K_f: eventPost = events.ToggleFullscreenEvent() if eventPost: self.eventManager.post(eventPost)
def quit(self): """ Gracefully quit the simulator. """ quitEvent = events.QuitEvent() AppState.get_state().get_event_manager().post_event(quitEvent)
def run(self): while self.on: for event in pygame.event.get(): if event.type == pygame.QUIT: self.evManager.Post(e.QuitEvent()) else: self.evManager.Post(e.PygameEvent(event)) self.evManager.Post(e.TickEvent())
def wire_events(self): self.imp.add_listener(events.QuitEvent().create([self.on_quit])) self.imp.add_listener(events.VideoResizeEvent().create( [self.on_resize])) self.imp.add_listener(events.KeyDownEvent().create([self.on_quit])) self.imp.add_listener( events.UserEvent(events.UserEvents.UNIT_TEST).create( [self.on_unit_test])) events.UserEvent(events.UserEvents.UNIT_TEST).post()
def run(self): shift = False ctrl = False alt = False clock = pygame.time.Clock() while self._run.is_set(): for event in pygame.event.get(): if event.type == pygame.QUIT: events.put(events.QuitEvent()) continue if event.type == pygame.ACTIVEEVENT: events.put(events.RefreshEvent()) continue if event.type == pygame.VIDEORESIZE: events.put(events.ResizeEvent(event.w, event.h)) continue if event.type not in [pygame.KEYDOWN, pygame.KEYUP]: continue if event.key in [pygame.K_RSHIFT, pygame.K_LSHIFT]: shift = (event.type == pygame.KEYDOWN) elif event.key in [pygame.K_RCTRL, pygame.K_LCTRL]: ctrl = (event.type == pygame.KEYDOWN) elif event.key in [pygame.K_RALT, pygame.K_LALT]: alt = (event.type == pygame.KEYDOWN) elif event.type == pygame.KEYDOWN: if shift: if event.key == pygame.K_DOWN: event.key = keys.KEY_SF elif event.key == pygame.K_UP: event.key = keys.KEY_SR elif event.key == pygame.K_LEFT: event.key = keys.KEY_SLEFT elif event.key == pygame.K_RIGHT: event.key = keys.KEY_SRIGHT elif event.key == pygame.K_TAB: event.key = keys.KEY_STAB events.put( events.KeyboardDownEvent(event.key, event.unicode)) elif event.type == pygame.KEYUP: events.put( events.KeyboardUpEvent(event.key, chr(event.key & 0xff))) clock.tick(120)
def Notify(self, event): if isinstance(event, e.TickEvent): for event in pygame.event.get(): ev = None if event.type == pygame.QUIT: ev = e.QuitEvent() if event.type == pygame.KEYDOWN: if event.key == pygame.K_1 \ or event.key == pygame.K_KP2: ev = e.GetPiece(1) elif event.key == pygame.K_2 \ or event.key == pygame.K_KP2: ev = e.GetPiece(2) elif event.key == pygame.K_3 \ or event.key == pygame.K_KP3: ev = e.GetPiece(3) elif event.key == pygame.K_4 \ or event.key == pygame.K_KP4: ev = e.GetPiece(4) elif event.key == pygame.K_5 \ or event.key == pygame.K_KP5: ev = e.GetPiece(5) elif event.key == pygame.K_z: ev = e.RotPiece("rotCCW") elif event.key == pygame.K_x: ev = e.RotPiece("rotCW") elif event.key == pygame.K_LSHIFT: ev = e.RotPiece("flip") elif event.key == pygame.K_COMMA \ or event.key == pygame.K_LESS: ev = e.NextPiece("b") elif event.key == pygame.K_PERIOD \ or event.key == pygame.K_GREATER: ev = e.NextPiece("f") elif event.key == pygame.K_UP: ev = e.MovePiece("up") elif event.key == pygame.K_DOWN: ev = e.MovePiece("down") elif event.key == pygame.K_LEFT: ev = e.MovePiece("left") elif event.key == pygame.K_RIGHT: ev = e.MovePiece("right") elif event.key == pygame.K_KP_ENTER \ or event.key == pygame.K_RETURN: ev = e.PlacePiece() if ev: self.evManager.Post(ev)
def get_interaction_from_input(self): """ Get the interaction the agent should enact from user input """ for event in pygame.event.get(): if event.type == pygame.QUIT: quitEvent = events.QuitEvent() AppState.get_state().get_event_manager().post_event(quitEvent) return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: return self.interaction_memory.find_interaction_by_name_and_result( "Step") elif event.key == pygame.K_LEFT: return self.interaction_memory.find_interaction_by_name_and_result( "Turn Left") elif event.key == pygame.K_RIGHT: return self.interaction_memory.find_interaction_by_name_and_result( "Turn Right") elif event.key == pygame.K_SLASH: return self.choose_from_list()
def wire_events(self): imp.IMP().add_delegate(events.QuitEvent().listen(self.on_quit)) imp.IMP().add_delegate(events.WindowResizedEvent().listen( self.on_resize)) imp.IMP().add_delegate( events.KeyDownEvent(pygame.K_ESCAPE).listen(self.on_pause)) imp.IMP().add_delegate( events.UserEvent(CustomEvent.GAME_OVER).listen(self.on_game_over)) imp.IMP().add_delegate( events.UserEvent(CustomEvent.UNDO_STACK_CLEARED).listen( self.on_redo_undo_changed)) imp.IMP().add_delegate( events.UserEvent(CustomEvent.REDO_STACK_CLEARED).listen( self.on_redo_undo_changed)) imp.IMP().add_delegate( events.UserEvent(CustomEvent.UNDO_ENABLED).listen( self.on_redo_undo_changed)) imp.IMP().add_delegate( events.UserEvent(CustomEvent.REDO_ENABLED).listen( self.on_redo_undo_changed)) imp.IMP().add_delegate( events.UserEvent(CustomEvent.UPDATE_SCORE).listen( self.on_update_score))
def notify(self, event): if isinstance(event, events.TickEvent): for event in pygame.event.get(): # Common events if event.type == pygame.VIDEORESIZE: resize_ev = events.ResizeWindowEvent(event.w, event.h) self.event_manager.post(resize_ev) if event.type == pygame.QUIT: quit_ev = events.QuitEvent() self.event_manager.post(quit_ev) if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: quit_ev = events.QuitEvent() self.event_manager.post(quit_ev) # Controls ready, input events are sent to the game if self.controls_ready: # Pause if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: # K_RETURN is the enter key self.event_manager.post(events.TogglePauseEvent()) elif event.key == pygame.K_BACKSPACE: reset_ev = events.ResetWorld() self.event_manager.post(reset_ev) # Handle events generated by mouse if self.use_mouse_to_aim_and_fire: if event.type == pygame.MOUSEMOTION: direction_X, direction_Y = pygame.mouse.get_pos() mouse_ev = events.MouseMoved( direction_X, direction_Y) self.event_manager.post(mouse_ev) elif event.type == pygame.MOUSEBUTTONDOWN: attack_request_ev = events.MouseButtonDown() self.event_manager.post(attack_request_ev) # Handle hats if self.use_hat_to_aim > -1: if event.type == pygame.JOYHATMOTION: if event.hat == self.use_hat_to_aim: input_event = events.SentInputAction( Actions.AIM_X, event) self.event_manager.post(input_event) if self.use_hat_to_move > -1: if event.type == pygame.JOYHATMOTION: if event.hat == self.use_hat_to_move: input_event = events.SentInputAction( Actions.JUMP, event) self.event_manager.post(input_event) # Handle other control events if (self.key_used(event)): action = self.determine_action(event) if action: input_event = events.SentInputAction(action, event) self.event_manager.post(input_event) # If custom keys are not initialized else: self.control_setting_screen.update_input_layout( self.actions_map, self.use_mouse_to_aim_and_fire, self.use_hat_to_aim, self.use_hat_to_move, self.use_default_controls) self.control_setting_screen.draw() if event.type == pygame.MOUSEBUTTONDOWN: action = self.control_setting_screen.hit_ui_element( event.pos) if (action): # If UI element was hit # Move with hat deactivate if (action == controlSettingScreen.SelectedUI. MOVE_WITH_HAT and self.use_hat_to_move > -1): self.use_hat_to_move = -1 self.control_setting_screen.activate_move_btns( ) self.control_setting_screen.deactivate_hat_move_btn( ) # Aim with hat deactivate elif (action == controlSettingScreen.SelectedUI.AIM_WITH_HAT and self.use_hat_to_aim > -1): self.use_hat_to_aim = -1 self.control_setting_screen.deactivate_hat_aim_btn( ) if not self.use_mouse_to_aim_and_fire: self.control_setting_screen.activate_aim_btns( ) # Mouse aim toggle elif (action == controlSettingScreen.SelectedUI. AIM_WITH_MOUSE): if self.use_mouse_to_aim_and_fire: self.use_mouse_to_aim_and_fire = False self.control_setting_screen.deactivate_mouse_aim_btn( ) if self.use_hat_to_aim < 0: self.control_setting_screen.activate_aim_btns( ) else: self.use_mouse_to_aim_and_fire = True self.control_setting_screen.deactivate_aim_btns( ) self.control_setting_screen.activate_mouse_aim_btn( ) self.control_setting_screen.deactivate_hat_aim_btn( ) self.use_hat_to_aim = -1 # Default controls toggle elif (action == controlSettingScreen.SelectedUI. USE_DEFAULT_CONTROLS): self.use_default_controls = ( not self.use_default_controls) action = self.control_setting_screen.currently_selected if event.type == pygame.JOYHATMOTION: # Aim with hat activate if (action == controlSettingScreen.SelectedUI.AIM_WITH_HAT and not event.hat == self.use_hat_to_aim): self.use_hat_to_aim = event.hat self.control_setting_screen.deactivate_aim_btns() self.control_setting_screen.activate_hat_aim_btn() self.control_setting_screen.deactivate_mouse_aim_btn( ) # Move with hat activate elif (action == controlSettingScreen.SelectedUI.MOVE_WITH_HAT and not event.hat == self.use_hat_to_move): self.use_hat_to_move = event.hat self.control_setting_screen.deactivate_move_btns() self.control_setting_screen.activate_hat_move_btn() if (event.type != pygame.MOUSEMOTION and #Filter not relevant event.type != pygame.ACTIVEEVENT and event.type != pygame.VIDEORESIZE and event.type != pygame.VIDEOEXPOSE and event.type != pygame.MOUSEBUTTONUP and event.type != pygame.JOYBUTTONUP): if (not self.key_used(event) and -1 < action and action < 8): self.save_key(event, action) # Done ? if (controlSettingScreen.SelectedUI.READY == self. control_setting_screen.currently_selected): self.event_manager.post(events.TogglePauseEvent()) self.controls_ready = True if self.use_default_controls: self.init_default_controls() if self.use_mouse_to_aim_and_fire: self.remove_aim_controls() elif self.use_hat_to_aim > -1: self.remove_aim_controls() self.event_manager.post( events.ToggleContinuousAttack()) else: self.event_manager.post( events.ToggleContinuousAttack()) if self.use_hat_to_move > -1: self.remove_movement_controls() self.control_setting_screen = None
def wire_events(self): imp.IMP().add_listener(events.QuitEvent().create(self.on_quit)) imp.IMP().add_listener(events.VideoResizeEvent().create( self.on_resize)) imp.IMP().add_listener(events.VideoResizeEvent().create( lambda event: self.text.center_on((event.w / 2, event.h / 2))))
def wire_events(self): self.screen.wire_events() self.event_dispatcher += events.QuitEvent().listen(self.on_quit)