Collect Coins Moving Down#

For a complete explanation, see Chapter 22 of Arcade Academy - Learn Python.

Screenshot of using sprites to collect coins
sprite_collect_coins_move_down.py#
  1"""
  2Sprite Collect Coins Moving Down
  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_coins_move_down
 10"""
 11
 12import random
 13import arcade
 14
 15# --- Constants ---
 16SPRITE_SCALING_PLAYER = 0.5
 17SPRITE_SCALING_COIN = 0.2
 18COIN_COUNT = 50
 19
 20SCREEN_WIDTH = 800
 21SCREEN_HEIGHT = 600
 22SCREEN_TITLE = "Sprite Collect Coins Moving Down Example"
 23
 24
 25class Coin(arcade.Sprite):
 26    """
 27    This class represents the coins on our screen. It is a child class of
 28    the arcade library's "Sprite" class.
 29    """
 30
 31    def reset_pos(self):
 32
 33        # Reset the coin to a random spot above the screen
 34        self.center_y = random.randrange(SCREEN_HEIGHT + 20,
 35                                         SCREEN_HEIGHT + 100)
 36        self.center_x = random.randrange(SCREEN_WIDTH)
 37
 38    def update(self):
 39
 40        # Move the coin
 41        self.center_y -= 1
 42
 43        # See if the coin has fallen off the bottom of the screen.
 44        # If so, reset it.
 45        if self.top < 0:
 46            self.reset_pos()
 47
 48
 49class MyGame(arcade.Window):
 50    """ Our custom Window Class"""
 51
 52    def __init__(self):
 53        """ Initializer """
 54
 55        # Call the parent class initializer
 56        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 57
 58        # Variables that will hold sprite lists
 59        self.player_sprite_list = None
 60        self.coin_sprite_list = None
 61
 62        # Set up the player info
 63        self.player_sprite = None
 64        self.score = 0
 65
 66        # Don't show the mouse cursor
 67        self.set_mouse_visible(False)
 68
 69        self.background_color = arcade.color.AMAZON
 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        self.coin_sprite_list = arcade.SpriteList()
 77
 78        # Score
 79        self.score = 0
 80
 81        # Set up the player
 82        # Character image from kenney.nl
 83        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 84                                           scale=SPRITE_SCALING_PLAYER)
 85        self.player_sprite.center_x = 50
 86        self.player_sprite.center_y = 50
 87        self.player_sprite_list.append(self.player_sprite)
 88
 89        # Create the coins
 90        for i in range(COIN_COUNT):
 91
 92            # Create the coin instance
 93            # Coin image from kenney.nl
 94            coin = Coin(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
 95
 96            # Position the coin
 97            coin.center_x = random.randrange(SCREEN_WIDTH)
 98            coin.center_y = random.randrange(SCREEN_HEIGHT)
 99
100            # Add the coin to the lists
101            self.coin_sprite_list.append(coin)
102
103    def on_draw(self):
104        """ Draw everything """
105        self.clear()
106        self.coin_sprite_list.draw()
107        self.player_sprite_list.draw()
108
109        # Put the text on the screen.
110        output = f"Score: {self.score}"
111        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
112
113    def on_mouse_motion(self, x, y, dx, dy):
114        """ Handle Mouse Motion """
115
116        # Move the center of the player sprite to match the mouse x, y
117        self.player_sprite.center_x = x
118        self.player_sprite.center_y = y
119
120    def on_update(self, delta_time):
121        """ Movement and game logic """
122
123        # Call update on all sprites (The sprites don't do much in this
124        # example though.)
125        self.coin_sprite_list.update()
126
127        # Generate a list of all sprites that collided with the player.
128        hit_list = arcade.check_for_collision_with_list(self.player_sprite,
129                                                        self.coin_sprite_list)
130
131        # Loop through each colliding sprite, remove it, and add to the score.
132        for coin in hit_list:
133            coin.remove_from_sprite_lists()
134            self.score += 1
135
136
137def main():
138    window = MyGame()
139    window.setup()
140    arcade.run()
141
142
143if __name__ == "__main__":
144    main()