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