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
 17SCREEN_WIDTH = 800
 18SCREEN_HEIGHT = 600
 19SCREEN_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):
 31        left, right = arcade.load_texture_pair(":resources:images/enemies/bee.png")
 32        super().__init__(left, scale=SPRITE_SCALING)
 33        self.textures.append(right)
 34
 35    def update(self):
 36        self.center_x += self.change_x
 37        self.center_y += self.change_y
 38
 39        # Figure out if we should face left or right
 40        if self.change_x < 0:
 41            self.texture = self.textures[TEXTURE_LEFT]
 42        elif self.change_x > 0:
 43            self.texture = self.textures[TEXTURE_RIGHT]
 44
 45
 46class MyGame(arcade.Window):
 47    """
 48    Main application class.
 49    """
 50
 51    def __init__(self, width, height, title):
 52        """
 53        Initializer
 54        """
 55
 56        # Call the parent class initializer
 57        super().__init__(width, height, title)
 58
 59        # Variables that will hold sprite lists
 60        self.player_sprite_list = None
 61
 62        # Set up the player info
 63        self.player_sprite = None
 64
 65        # Set the background color
 66        self.background_color = arcade.color.AMAZON
 67
 68    def setup(self):
 69        """ Set up the game and initialize the variables. """
 70
 71        # Sprite lists
 72        self.player_sprite_list = arcade.SpriteList()
 73
 74        # Set up the player
 75        self.player_sprite = Player()
 76        self.player_sprite.center_x = SCREEN_WIDTH / 2
 77        self.player_sprite.center_y = SCREEN_HEIGHT / 2
 78        self.player_sprite_list.append(self.player_sprite)
 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.player_sprite_list.draw()
 90
 91    def on_update(self, delta_time):
 92        """ Movement and game logic """
 93
 94        # Call update on all sprites (The sprites don't do much in this
 95        # example though.)
 96        self.player_sprite_list.update()
 97
 98    def on_key_press(self, key, modifiers):
 99        """Called whenever a key is pressed. """
100
101        if key == arcade.key.UP:
102            self.player_sprite.change_y = MOVEMENT_SPEED
103        elif key == arcade.key.DOWN:
104            self.player_sprite.change_y = -MOVEMENT_SPEED
105        elif key == arcade.key.LEFT:
106            self.player_sprite.change_x = -MOVEMENT_SPEED
107        elif key == arcade.key.RIGHT:
108            self.player_sprite.change_x = MOVEMENT_SPEED
109
110    def on_key_release(self, key, modifiers):
111        """Called when the user releases a key. """
112
113        if key == arcade.key.UP or key == arcade.key.DOWN:
114            self.player_sprite.change_y = 0
115        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
116            self.player_sprite.change_x = 0
117
118
119def main():
120    """ Main function """
121    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
122    window.setup()
123    arcade.run()
124
125
126if __name__ == "__main__":
127    main()