Collect Coins that are Moving in a Circle#

Screenshot of using sprites to collect coins
sprite_collect_coins_move_circle.py#
  1"""
  2Sprite Collect Coins Moving in Circles
  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_circle
 10"""
 11
 12from __future__ import annotations
 13
 14import random
 15import arcade
 16import math
 17
 18SPRITE_SCALING = 0.5
 19
 20SCREEN_WIDTH = 800
 21SCREEN_HEIGHT = 600
 22SCREEN_TITLE = "Sprite Collect Coins Moving in Circles Example"
 23
 24
 25class Coin(arcade.Sprite):
 26
 27    def __init__(self, filename, scale):
 28        """ Constructor. """
 29        # Call the parent class (Sprite) constructor
 30        super().__init__(filename, scale=scale)
 31
 32        # Current angle in radians
 33        self.circle_angle = 0
 34
 35        # How far away from the center to orbit, in pixels
 36        self.circle_radius = 0
 37
 38        # How fast to orbit, in radians per frame
 39        self.circle_speed = 0.008
 40
 41        # Set the center of the point we will orbit around
 42        self.circle_center_x = 0
 43        self.circle_center_y = 0
 44
 45    def update(self):
 46
 47        """ Update the ball's position. """
 48        # Calculate a new x, y
 49        self.center_x = self.circle_radius * math.sin(self.circle_angle) \
 50            + self.circle_center_x
 51        self.center_y = self.circle_radius * math.cos(self.circle_angle) \
 52            + self.circle_center_y
 53
 54        # Increase the angle in prep for the next round.
 55        self.circle_angle += self.circle_speed
 56
 57
 58class MyGame(arcade.Window):
 59    """ Main application class. """
 60
 61    def __init__(self, width, height, title):
 62
 63        super().__init__(width, height, title)
 64
 65        # Sprite lists
 66        self.all_sprites_list = None
 67        self.coin_list = None
 68
 69        # Set up the player
 70        self.score = 0
 71        self.player_sprite = None
 72
 73    def start_new_game(self):
 74        """ Set up the game and initialize the variables. """
 75
 76        # Sprite lists
 77        self.all_sprites_list = arcade.SpriteList()
 78        self.coin_list = arcade.SpriteList()
 79
 80        # Set up the player
 81        self.score = 0
 82        # Character image from kenney.nl
 83        self.player_sprite = arcade.Sprite(
 84            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 85            scale=SPRITE_SCALING
 86        )
 87        self.player_sprite.center_x = 50
 88        self.player_sprite.center_y = 70
 89        self.all_sprites_list.append(self.player_sprite)
 90
 91        for i in range(50):
 92
 93            # Create the coin instance
 94            # Coin image from kenney.nl
 95            coin = Coin(":resources:images/items/coinGold.png", scale=SPRITE_SCALING / 3)
 96
 97            # Position the center of the circle the coin will orbit
 98            coin.circle_center_x = random.randrange(SCREEN_WIDTH)
 99            coin.circle_center_y = random.randrange(SCREEN_HEIGHT)
100
101            # Random radius from 10 to 200
102            coin.circle_radius = random.randrange(10, 200)
103
104            # Random start angle from 0 to 2pi
105            coin.circle_angle = random.random() * 2 * math.pi
106
107            # Add the coin to the lists
108            self.all_sprites_list.append(coin)
109            self.coin_list.append(coin)
110
111        # Don't show the mouse cursor
112        self.set_mouse_visible(False)
113
114        # Set the background color
115        self.background_color = arcade.color.AMAZON
116
117    def on_draw(self):
118
119        # This command has to happen before we start drawing
120        self.clear()
121
122        # Draw all the sprites.
123        self.all_sprites_list.draw()
124
125        # Put the text on the screen.
126        output = "Score: " + str(self.score)
127        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
128
129    def on_mouse_motion(self, x, y, dx, dy):
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            self.score += 1
147            coin.remove_from_sprite_lists()
148
149
150def main():
151    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
152    window.start_new_game()
153    arcade.run()
154
155
156if __name__ == "__main__":
157    main()