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