def handle_hat_moved(self, x, y):
        """Hat controlls player movement.

        :param x: x position
        :type x: int
        :param y: y position
        :type y: int
        """
        if x < 0:
            #Walk left
            ev = events.EntityMovesLeftRequest(self.world.player)
            self.event_manager.post(ev)
        elif x > 0:
            #Walk right
            ev = events.EntityMovesRightRequest(self.world.player)
            self.event_manager.post(ev)
        else:
            ev = events.EntityStopMovingLeftRequest(self.world.player)
            self.event_manager.post(ev)
            ev = events.EntityStopMovingRightRequest(self.world.player)
            self.event_manager.post(ev)
        if y > 0:  # and self.world.state[self.world.player].grounded:
            #Jump
            ev = events.EntityJumpRequest(self.world.player)
            self.event_manager.post(ev)
 def unpause_game(self):
     '''When game unpauses, send requests, that
     stop player from movement.
     '''
     self.world.game_paused = False
     ev1 = events.EntityStopMovingLeftRequest(self.world.player)
     self.event_manager.post(ev1)
     ev2 = events.EntityStopMovingRightRequest(self.world.player)
     self.event_manager.post(ev2)
     self.key_up = False
     self.key_down = False
     self.key_left = False
     self.key_right = False
    def handle_key_released(self, key):
        """Player stops movement, when key is released.

        :param key: key pressed
        :type key: pygame constant
        """
        if key == pygame.K_a:
            ev = events.EntityStopMovingLeftRequest(self.world.player)
            self.event_manager.post(ev)
        elif key == pygame.K_d:
            ev = events.EntityStopMovingRightRequest(self.world.player)
            self.event_manager.post(ev)
        elif key == pygame.K_UP:
            self.key_up = False
        elif key == pygame.K_DOWN:
            self.key_down = False
        elif key == pygame.K_LEFT:
            self.key_left = False
        elif key == pygame.K_RIGHT:
            self.key_right = False
示例#4
0
 def stop_movement(self):
     """Simply stops walking."""
     ev = events.EntityStopMovingLeftRequest(self.entity_ID)
     self.event_manager.post(ev)
     ev = events.EntityStopMovingRightRequest(self.entity_ID)
     self.event_manager.post(ev)
 def handle_input(self, event):
     #Movement
     if (event.action == Actions.MOVE_LEFT
             and event.input.type == pygame.KEYDOWN):
         ev = events.EntityMovesLeftRequest(self.world.player)
         self.event_manager.post(ev)
     elif (event.input.type == pygame.KEYDOWN
           and event.action == Actions.MOVE_RIGHT):
         ev = events.EntityMovesRightRequest(self.world.player)
         self.event_manager.post(ev)
     elif (event.action == Actions.MOVE_LEFT
           and event.input.type == pygame.KEYUP):
         ev = events.EntityStopMovingLeftRequest(self.world.player)
         self.event_manager.post(ev)
     elif (event.input.type == pygame.KEYUP
           and event.action == Actions.MOVE_RIGHT):
         ev = events.EntityStopMovingRightRequest(self.world.player)
         self.event_manager.post(ev)
     elif (event.input.type == pygame.KEYDOWN
           and event.action == Actions.JUMP):
         ev = events.EntityJumpRequest(self.world.player)
         self.event_manager.post(ev)
     #Keys + aiming
     elif ((event.input.type == pygame.KEYDOWN
            or event.input.type == pygame.KEYUP)
           and is_aim_action(event.action)):
         self.handle_keys_aiming(event)
     #Joystick button + movement
     elif (input_is_a_joystickbutton(event.input)
           and event.action == Actions.MOVE_LEFT):
         if (event.input.type == pygame.JOYBUTTONDOWN):
             ev = events.EntityMovesLeftRequest(self.world.player)
             self.event_manager.post(ev)
         elif (event.input.type == pygame.JOYBUTTONUP):
             ev = events.EntityStopMovingLeftRequest(self.world.player)
             self.event_manager.post(ev)
     elif (input_is_a_joystickbutton(event.input)
           and event.action == Actions.MOVE_RIGHT):
         if (event.input.type == pygame.JOYBUTTONDOWN):
             ev = events.EntityMovesRightRequest(self.world.player)
             self.event_manager.post(ev)
         elif (event.input.type == pygame.JOYBUTTONUP):
             ev = events.EntityStopMovingRightRequest(self.world.player)
             self.event_manager.post(ev)
     elif (input_is_a_joystickbutton(event.input)
           and event.action == Actions.JUMP):
         if (event.input.type == pygame.JOYBUTTONDOWN):
             ev = events.EntityJumpRequest(self.world.player)
             self.event_manager.post(ev)
     #Joystick button + aiming
     elif ((event.input.type == pygame.JOYBUTTONDOWN
            or event.input.type == pygame.JOYBUTTONUP)
           and is_aim_action(event.action)):
         self.handle_keys_aiming(event)
     #Joystick axis + aiming
     elif (event.input.type == pygame.JOYAXISMOTION
           and is_aim_action(event.action)):
         self.handle_axis_aiming(event)
     #Joystick axis + moving
     elif (event.input.type == pygame.JOYAXISMOTION
           and event.action == Actions.MOVE_LEFT):
         offset = 0.5
         value = event.input.value
         if abs(value) > offset:
             ev = events.EntityMovesLeftRequest(self.world.player)
         else:
             ev = events.EntityStopMovingLeftRequest(self.world.player)
         self.event_manager.post(ev)
     elif (event.input.type == pygame.JOYAXISMOTION
           and event.action == Actions.MOVE_RIGHT):
         offset = 0.5
         value = event.input.value
         if abs(value) > offset:
             ev = events.EntityMovesRightRequest(self.world.player)
         else:
             ev = events.EntityStopMovingRightRequest(self.world.player)
         self.event_manager.post(ev)
     elif (event.input.type == pygame.JOYAXISMOTION
           and event.action == Actions.JUMP):
         offset = 0.5
         value = event.input.value
         if abs(value) > offset:
             ev = events.EntityJumpRequest(self.world.player)
             self.event_manager.post(ev)
     #Joystick hat + aiming
     elif (event.input.type == pygame.JOYHATMOTION
           and is_aim_action(event.action)):
         self.handle_hat_aiming(event)
     #Joystick hat + moving
     elif (event.input.type == pygame.JOYHATMOTION
           and is_movement_action(event.action)):
         x_value, y_value = event.input.value
         if (x_value < 0):
             ev = events.EntityMovesLeftRequest(self.world.player)
             self.event_manager.post(ev)
         elif (x_value > 0):
             ev = events.EntityMovesRightRequest(self.world.player)
             self.event_manager.post(ev)
         else:
             ev = events.EntityStopMovingLeftRequest(self.world.player)
             self.event_manager.post(ev)
             ev = events.EntityStopMovingRightRequest(self.world.player)
             self.event_manager.post(ev)
         if (y_value > 0):
             ev = events.EntityJumpRequest(self.world.player)
             self.event_manager.post(ev)