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 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_collect_rotating
 10"""
 11import random
 12import arcade
 13
 14# --- Constants ---
 15SPRITE_SCALING_PLAYER = 0.5
 16SPRITE_SCALING_COIN = 0.2
 17COIN_COUNT = 50
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprite Collect Rotating Coins Example"
 22
 23
 24class Coin(arcade.Sprite):
 25
 26    def update(self):
 27        # Rotate the coin.
 28        # The arcade.Sprite class has an "angle" attribute that controls
 29        # the sprite rotation. Change this, and the sprite rotates.
 30        self.angle += self.change_angle
 31
 32
 33class MyGame(arcade.Window):
 34    """ Our custom Window Class"""
 35
 36    def __init__(self):
 37        """ Initializer """
 38        # Call the parent class initializer
 39        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 40
 41        # Variables that will hold sprite lists
 42        self.player_list = None
 43        self.coin_list = None
 44
 45        # Set up the player info
 46        self.player_sprite = None
 47        self.score = 0
 48
 49        # Don't show the mouse cursor
 50        self.set_mouse_visible(False)
 51
 52        self.background_color = arcade.color.AMAZON
 53
 54    def setup(self):
 55        """ Set up the game and initialize the variables. """
 56
 57        # Sprite lists
 58        self.player_list = arcade.SpriteList()
 59        self.coin_list = arcade.SpriteList()
 60
 61        # Score
 62        self.score = 0
 63
 64        # Set up the player
 65        # Character image from kenney.nl
 66        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 67                                           scale=SPRITE_SCALING_PLAYER)
 68        self.player_sprite.center_x = 50
 69        self.player_sprite.center_y = 50
 70        self.player_list.append(self.player_sprite)
 71
 72        # Create the coins
 73        for i in range(COIN_COUNT):
 74            # Create the coin instance
 75            # Coin image from kenney.nl
 76            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
 77
 78            # Position the coin
 79            coin.center_x = random.randrange(SCREEN_WIDTH)
 80            coin.center_y = random.randrange(SCREEN_HEIGHT)
 81
 82            # Set up the initial angle, and the "spin"
 83            coin.angle = random.randrange(360)
 84            coin.change_angle = random.randrange(-5, 6)
 85
 86            # Add the coin to the lists
 87            self.coin_list.append(coin)
 88
 89    def on_draw(self):
 90        """ Draw everything """
 91        self.clear()
 92        self.coin_list.draw()
 93        self.player_list.draw()
 94
 95        # Put the text on the screen.
 96        output = f"Score: {self.score}"
 97        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
 98
 99    def on_mouse_motion(self, x, y, dx, dy):
100        """ Handle Mouse Motion """
101
102        # Move the center of the player sprite to match the mouse x, y
103        self.player_sprite.center_x = x
104        self.player_sprite.center_y = y
105
106    def on_update(self, delta_time):
107        """ Movement and game logic """
108
109        # Call update on all sprites (The sprites don't do much in this
110        # example though.)
111        self.coin_list.update()
112
113        # Generate a list of all sprites that collided with the player.
114        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
115
116        # Loop through each colliding sprite, remove it, and add to the score.
117        for coin in hit_list:
118            coin.remove_from_sprite_lists()
119            self.score += 1
120
121
122def main():
123    """ Main function """
124    window = MyGame()
125    window.setup()
126    arcade.run()
127
128
129if __name__ == "__main__":
130    main()