Move with Walls#

Screenshot of using sprites to detect wall collisions
sprite_move_walls.py#
  1"""
  2Sprite Move With Walls
  3
  4Simple program to show basic sprite usage.
  5
  6Artwork from https://kenney.nl
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.sprite_move_walls
 10"""
 11
 12from __future__ import annotations
 13
 14import arcade
 15
 16SPRITE_SCALING = 0.5
 17
 18SCREEN_WIDTH = 800
 19SCREEN_HEIGHT = 600
 20SCREEN_TITLE = "Sprite Move with Walls Example"
 21
 22MOVEMENT_SPEED = 5
 23
 24
 25class MyGame(arcade.Window):
 26    """ Main application class. """
 27
 28    def __init__(self, width, height, title):
 29        """
 30        Initializer
 31        """
 32        super().__init__(width, height, title)
 33
 34        # Sprite lists
 35        self.coin_list = None
 36        self.wall_list = None
 37        self.player_list = None
 38
 39        # Set up the player
 40        self.player_sprite = None
 41        self.physics_engine = None
 42
 43    def setup(self):
 44        """ Set up the game and initialize the variables. """
 45
 46        # Sprite lists
 47        self.player_list = arcade.SpriteList()
 48        self.wall_list = arcade.SpriteList()
 49
 50        # Set up the player
 51        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 52                                           scale=SPRITE_SCALING)
 53        self.player_sprite.center_x = 50
 54        self.player_sprite.center_y = 64
 55        self.player_list.append(self.player_sprite)
 56
 57        # -- Set up the walls
 58        # Create a row of boxes
 59        for x in range(173, 650, 64):
 60            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
 61                                 scale=SPRITE_SCALING)
 62            wall.center_x = x
 63            wall.center_y = 200
 64            self.wall_list.append(wall)
 65
 66        # Create a column of boxes
 67        for y in range(273, 500, 64):
 68            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
 69                                 scale=SPRITE_SCALING)
 70            wall.center_x = 465
 71            wall.center_y = y
 72            self.wall_list.append(wall)
 73
 74        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
 75                                                         self.wall_list)
 76
 77        # Set the background color
 78        self.background_color = arcade.color.AMAZON
 79
 80    def on_draw(self):
 81        """
 82        Render the screen.
 83        """
 84
 85        # This command has to happen before we start drawing
 86        self.clear()
 87
 88        # Draw all the sprites.
 89        self.wall_list.draw()
 90        self.player_list.draw()
 91
 92    def on_key_press(self, key, modifiers):
 93        """Called whenever a key is pressed. """
 94
 95        if key == arcade.key.UP:
 96            self.player_sprite.change_y = MOVEMENT_SPEED
 97        elif key == arcade.key.DOWN:
 98            self.player_sprite.change_y = -MOVEMENT_SPEED
 99        elif key == arcade.key.LEFT:
100            self.player_sprite.change_x = -MOVEMENT_SPEED
101        elif key == arcade.key.RIGHT:
102            self.player_sprite.change_x = MOVEMENT_SPEED
103
104    def on_key_release(self, key, modifiers):
105        """Called when the user releases a key. """
106
107        if key == arcade.key.UP or key == arcade.key.DOWN:
108            self.player_sprite.change_y = 0
109        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
110            self.player_sprite.change_x = 0
111
112    def on_update(self, delta_time):
113        """ Movement and game logic """
114
115        # Call update on all sprites (The sprites don't do much in this
116        # example though.)
117        self.physics_engine.update()
118
119
120def main():
121    """ Main function """
122    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
123    window.setup()
124    arcade.run()
125
126
127if __name__ == "__main__":
128    main()