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 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_circle
 10"""
 11
 12import random
 13import arcade
 14import math
 15import os
 16
 17SPRITE_SCALING = 0.5
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprite Collect Coins Moving in Circles Example"
 22
 23
 24class Coin(arcade.Sprite):
 25
 26    def __init__(self, filename, sprite_scaling):
 27        """ Constructor. """
 28        # Call the parent class (Sprite) constructor
 29        super().__init__(filename, sprite_scaling)
 30
 31        # Current angle in radians
 32        self.circle_angle = 0
 33
 34        # How far away from the center to orbit, in pixels
 35        self.circle_radius = 0
 36
 37        # How fast to orbit, in radians per frame
 38        self.circle_speed = 0.008
 39
 40        # Set the center of the point we will orbit around
 41        self.circle_center_x = 0
 42        self.circle_center_y = 0
 43
 44    def update(self):
 45
 46        """ Update the ball's position. """
 47        # Calculate a new x, y
 48        self.center_x = self.circle_radius * math.sin(self.circle_angle) \
 49            + self.circle_center_x
 50        self.center_y = self.circle_radius * math.cos(self.circle_angle) \
 51            + self.circle_center_y
 52
 53        # Increase the angle in prep for the next round.
 54        self.circle_angle += self.circle_speed
 55
 56
 57class MyGame(arcade.Window):
 58    """ Main application class. """
 59
 60    def __init__(self, width, height, title):
 61
 62        super().__init__(width, height, title)
 63
 64        # Set the working directory (where we expect to find files) to the same
 65        # directory this .py file is in. You can leave this out of your own
 66        # code, but it is needed to easily run the examples using "python -m"
 67        # as mentioned at the top of this program.
 68        file_path = os.path.dirname(os.path.abspath(__file__))
 69        os.chdir(file_path)
 70
 71        # Sprite lists
 72        self.all_sprites_list = None
 73        self.coin_list = None
 74
 75        # Set up the player
 76        self.score = 0
 77        self.player_sprite = None
 78
 79    def start_new_game(self):
 80        """ Set up the game and initialize the variables. """
 81
 82        # Sprite lists
 83        self.all_sprites_list = arcade.SpriteList()
 84        self.coin_list = arcade.SpriteList()
 85
 86        # Set up the player
 87        self.score = 0
 88        # Character image from kenney.nl
 89        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
 90        self.player_sprite.center_x = 50
 91        self.player_sprite.center_y = 70
 92        self.all_sprites_list.append(self.player_sprite)
 93
 94        for i in range(50):
 95
 96            # Create the coin instance
 97            # Coin image from kenney.nl
 98            coin = Coin(":resources:images/items/coinGold.png", SPRITE_SCALING / 3)
 99
100            # Position the center of the circle the coin will orbit
101            coin.circle_center_x = random.randrange(SCREEN_WIDTH)
102            coin.circle_center_y = random.randrange(SCREEN_HEIGHT)
103
104            # Random radius from 10 to 200
105            coin.circle_radius = random.randrange(10, 200)
106
107            # Random start angle from 0 to 2pi
108            coin.circle_angle = random.random() * 2 * math.pi
109
110            # Add the coin to the lists
111            self.all_sprites_list.append(coin)
112            self.coin_list.append(coin)
113
114        # Don't show the mouse cursor
115        self.set_mouse_visible(False)
116
117        # Set the background color
118        arcade.set_background_color(arcade.color.AMAZON)
119
120    def on_draw(self):
121
122        # This command has to happen before we start drawing
123        arcade.start_render()
124
125        # Draw all the sprites.
126        self.all_sprites_list.draw()
127
128        # Put the text on the screen.
129        output = "Score: " + str(self.score)
130        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
131
132    def on_mouse_motion(self, x, y, dx, dy):
133        self.player_sprite.center_x = x
134        self.player_sprite.center_y = y
135
136    def on_update(self, delta_time):
137        """ Movement and game logic """
138
139        # Call update on all sprites (The sprites don't do much in this
140        # example though.)
141        self.all_sprites_list.update()
142
143        # Generate a list of all sprites that collided with the player.
144        hit_list = arcade.check_for_collision_with_list(self.player_sprite,
145                                                        self.coin_list)
146
147        # Loop through each colliding sprite, remove it, and add to the score.
148        for coin in hit_list:
149            self.score += 1
150            coin.remove_from_sprite_lists()
151
152
153def main():
154    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
155    window.start_new_game()
156    arcade.run()
157
158
159if __name__ == "__main__":
160    main()