def update(self, delta_time): """ Update the sprites """ # Keep track of how long this function takes. start_time = timeit.default_timer() # If we have force to apply to the player (from hitting the arrow # keys), apply it. self.player.body.apply_force_at_local_point(self.force, (0, 0)) # check_collision(self.player) # See if the player is standing on an item. # If she is, apply opposite force to the item below her. # So if she moves left, the box below her will have # a force to move to the right. grounding = check_grounding(self.player) if self.force[0] and grounding and grounding['body']: grounding['body'].apply_force_at_world_point((-self.force[0], 0), grounding['position']) # Check for sprites that fall off the screen. # If so, get rid of them. for sprite in self.dynamic_sprite_list: if sprite.shape.body.position.y < 0: # Remove sprites from physics space self.space.remove(sprite.shape, sprite.shape.body) # Remove sprites from physics list sprite.kill() # Update physics # Use a constant time step, don't use delta_time # See "Game loop / moving time forward" # http://www.pymunk.org/en/latest/overview.html#game-loop-moving-time-forward self.space.step(1 / 60.0) # If we are dragging an object, make sure it stays with the mouse. Otherwise # gravity will drag it down. if self.shape_being_dragged is not None: self.shape_being_dragged.shape.body.position = self.last_mouse_position self.shape_being_dragged.shape.body.velocity = 0, 0 # Resync the sprites to the physics objects that shadow them resync_physics_sprites(self.dynamic_sprite_list) # Scroll the viewport if needed self.scroll_viewport() # Save the time it took to do this. self.processing_time = timeit.default_timer() - start_time
def on_key_press(self, symbol: int, modifiers: int): """ Handle keyboard presses. """ if symbol == arcade.key.RIGHT: # Add force to the player, and set the player friction to zero self.force = (PLAYER_MOVE_FORCE, 0) self.player.shape.friction = 0 elif symbol == arcade.key.LEFT: # Add force to the player, and set the player friction to zero self.force = (-PLAYER_MOVE_FORCE, 0) self.player.shape.friction = 0 elif symbol == arcade.key.UP: # find out if player is standing on ground grounding = check_grounding(self.player) if grounding['body'] != None and abs(grounding['normal'].x / grounding['normal'].y) < self.player.shape.friction: # She is! Go ahead and jump self.player.body.apply_impulse_at_local_point((0, PLAYER_JUMP_IMPULSE)) elif symbol == arcade.key.SPACE: self.punch() elif symbol == arcade.key.G: self.grab()