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