Sprite: Face Left or Right

../_images/sprite_face_left_or_right1.png
sprite_face_left_or_right.py
  1"""
  2Sprite Facing Left or Right
  3Face left or right depending on our direction
  4
  5Simple program to show basic sprite usage.
  6
  7Artwork from https://kenney.nl
  8
  9If Python and Arcade are installed, this example can be run from the command line with:
 10python -m arcade.examples.sprite_face_left_or_right
 11"""
 12
 13import arcade
 14
 15SPRITE_SCALING = 0.5
 16
 17WINDOW_WIDTH = 1280
 18WINDOW_HEIGHT = 720
 19WINDOW_TITLE = "Sprite Face Left or Right Example"
 20
 21MOVEMENT_SPEED = 5
 22
 23# Index of textures, first element faces left, second faces right
 24TEXTURE_LEFT = 0
 25TEXTURE_RIGHT = 1
 26
 27
 28class Player(arcade.Sprite):
 29
 30    def __init__(self, left_texture, right_texture):
 31        super().__init__(left_texture, scale=SPRITE_SCALING)
 32        self.textures.append(right_texture)
 33
 34    def update(self, delta_time: float = 1/60):
 35        self.center_x += self.change_x
 36        self.center_y += self.change_y
 37
 38        # Figure out if we should face left or right
 39        if self.change_x < 0:
 40            self.texture = self.textures[TEXTURE_LEFT]
 41        elif self.change_x > 0:
 42            self.texture = self.textures[TEXTURE_RIGHT]
 43
 44
 45class GameView(arcade.View):
 46    """
 47    Main application class.
 48    """
 49
 50    def __init__(self):
 51        """
 52        Initializer
 53        """
 54
 55        # Call the parent class initializer
 56        super().__init__()
 57
 58        # Variables that will hold sprite lists
 59        self.player_sprite_list = None
 60
 61        # Set up the player info
 62        self.player_sprite = None
 63
 64        # Set the background color
 65        self.background_color = arcade.color.AMAZON
 66
 67        # Textures for left and right facing sprites
 68        self.left_texture = arcade.load_texture(":resources:images/enemies/bee.png")
 69        self.right_texture = self.left_texture.flip_left_right()
 70
 71    def setup(self):
 72        """ Set up the game and initialize the variables. """
 73
 74        # Sprite lists
 75        self.player_sprite_list = arcade.SpriteList()
 76
 77        # Set up the player
 78        self.player_sprite = Player(self.left_texture, self.right_texture)
 79        self.player_sprite.center_x = WINDOW_WIDTH / 2
 80        self.player_sprite.center_y = WINDOW_HEIGHT / 2
 81        self.player_sprite_list.append(self.player_sprite)
 82
 83    def on_draw(self):
 84        """
 85        Render the screen.
 86        """
 87
 88        # Clear the screen with the configured background color
 89        self.clear()
 90
 91        # Draw all the sprites.
 92        self.player_sprite_list.draw()
 93
 94    def on_update(self, delta_time):
 95        """ Movement and game logic """
 96
 97        # Call update on all sprites (The sprites don't do much in this
 98        # example though.)
 99        self.player_sprite_list.update(delta_time)
100
101    def on_key_press(self, key, modifiers):
102        """Called whenever a key is pressed. """
103
104        if key in (arcade.key.UP, arcade.key.W):
105            self.player_sprite.change_y = MOVEMENT_SPEED
106        elif key in (arcade.key.DOWN, arcade.key.S):
107            self.player_sprite.change_y = -MOVEMENT_SPEED
108        elif key in (arcade.key.LEFT, arcade.key.A):
109            self.player_sprite.change_x = -MOVEMENT_SPEED
110        elif key in (arcade.key.RIGHT, arcade.key.D):
111            self.player_sprite.change_x = MOVEMENT_SPEED
112        elif key == arcade.key.ESCAPE:
113            arcade.close_window()
114
115    def on_key_release(self, key, modifiers):
116        """Called when the user releases a key. """
117
118        if key in (arcade.key.UP, arcade.key.DOWN, arcade.key.W, arcade.key.S):
119            self.player_sprite.change_y = 0
120        elif key in (arcade.key.LEFT, arcade.key.RIGHT, arcade.key.A, arcade.key.D):
121            self.player_sprite.change_x = 0
122
123
124def main():
125    """ Main function """
126    # Create a window class. This is what actually shows up on screen
127    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
128
129    # Create and setup the GameView
130    game = GameView()
131    game.setup()
132
133    # Show GameView on screen
134    window.show_view(game)
135
136    # Start the arcade game loop
137    arcade.run()
138
139
140if __name__ == "__main__":
141    main()