Collect Coins that are Moving in a Circle

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