Collect Coins that are Bouncing#

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