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