Animated Sprites

Screen shot of rotating sprites
sprite_collect_rotating.py
  1"""
  2Sprite Collect Rotating Coins
  3
  4Simple program to show basic sprite usage.
  5
  6Artwork from http://kenney.nl
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.sprite_collect_rotating
 10"""
 11
 12import random
 13import arcade
 14import os
 15
 16# --- Constants ---
 17SPRITE_SCALING_PLAYER = 0.5
 18SPRITE_SCALING_COIN = 0.2
 19COIN_COUNT = 50
 20
 21SCREEN_WIDTH = 800
 22SCREEN_HEIGHT = 600
 23SCREEN_TITLE = "Sprite Collect Rotating Coins Example"
 24
 25
 26class Coin(arcade.Sprite):
 27
 28    def update(self):
 29        # Rotate the coin.
 30        # The arcade.Sprite class has an "angle" attribute that controls
 31        # the sprite rotation. Change this, and the sprite rotates.
 32        self.angle += self.change_angle
 33
 34
 35class MyGame(arcade.Window):
 36    """ Our custom Window Class"""
 37
 38    def __init__(self):
 39        """ Initializer """
 40        # Call the parent class initializer
 41        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 42
 43        # Set the working directory (where we expect to find files) to the same
 44        # directory this .py file is in. You can leave this out of your own
 45        # code, but it is needed to easily run the examples using "python -m"
 46        # as mentioned at the top of this program.
 47        file_path = os.path.dirname(os.path.abspath(__file__))
 48        os.chdir(file_path)
 49
 50        # Variables that will hold sprite lists
 51        self.player_list = None
 52        self.coin_list = None
 53
 54        # Set up the player info
 55        self.player_sprite = None
 56        self.score = 0
 57
 58        # Don't show the mouse cursor
 59        self.set_mouse_visible(False)
 60
 61        arcade.set_background_color(arcade.color.AMAZON)
 62
 63    def setup(self):
 64        """ Set up the game and initialize the variables. """
 65
 66        # Sprite lists
 67        self.player_list = arcade.SpriteList()
 68        self.coin_list = arcade.SpriteList()
 69
 70        # Score
 71        self.score = 0
 72
 73        # Set up the player
 74        # Character image from kenney.nl
 75        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
 76        self.player_sprite.center_x = 50
 77        self.player_sprite.center_y = 50
 78        self.player_list.append(self.player_sprite)
 79
 80        # Create the coins
 81        for i in range(COIN_COUNT):
 82            # Create the coin instance
 83            # Coin image from kenney.nl
 84            coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
 85
 86            # Position the coin
 87            coin.center_x = random.randrange(SCREEN_WIDTH)
 88            coin.center_y = random.randrange(SCREEN_HEIGHT)
 89
 90            # Set up the initial angle, and the "spin"
 91            coin.angle = random.randrange(360)
 92            coin.change_angle = random.randrange(-5, 6)
 93
 94            # Add the coin to the lists
 95            self.coin_list.append(coin)
 96
 97    def on_draw(self):
 98        """ Draw everything """
 99        arcade.start_render()
100        self.coin_list.draw()
101        self.player_list.draw()
102
103        # Put the text on the screen.
104        output = f"Score: {self.score}"
105        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
106
107    def on_mouse_motion(self, x, y, dx, dy):
108        """ Handle Mouse Motion """
109
110        # Move the center of the player sprite to match the mouse x, y
111        self.player_sprite.center_x = x
112        self.player_sprite.center_y = y
113
114    def on_update(self, delta_time):
115        """ Movement and game logic """
116
117        # Call update on all sprites (The sprites don't do much in this
118        # example though.)
119        self.coin_list.update()
120
121        # Generate a list of all sprites that collided with the player.
122        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
123
124        # Loop through each colliding sprite, remove it, and add to the score.
125        for coin in hit_list:
126            coin.remove_from_sprite_lists()
127            self.score += 1
128
129
130def main():
131    """ Main method """
132    window = MyGame()
133    window.setup()
134    arcade.run()
135
136
137if __name__ == "__main__":
138    main()