Different Levels of Clearing Coins

sprite_collect_coins_diff_levels.py
1"""
2Sprite Collect Coins with Different Levels
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_diff_levels
10"""
11
12import random
13import arcade
14
15SPRITE_SCALING = 1.0
16
17WINDOW_WIDTH = 1280
18WINDOW_HEIGHT = 720
19WINDOW_TITLE = "Sprite Collect Coins with Different Levels Example"
20
21
22class FallingCoin(arcade.Sprite):
23 """ Simple sprite that falls down """
24
25 def update(self):
26 """ Move the coin """
27
28 # Fall down
29 self.center_y -= 2
30
31 # Did we go off the screen? If so, pop back to the top.
32 if self.top < 0:
33 self.bottom = WINDOW_HEIGHT
34
35
36class RisingCoin(arcade.Sprite):
37 """ Simple sprite that falls up """
38
39 def update(self):
40 """ Move the coin """
41
42 # Move up
43 self.center_y += 2
44
45 # Did we go off the screen? If so, pop back to the bottom.
46 if self.bottom > WINDOW_HEIGHT:
47 self.top = 0
48
49
50class GameView(arcade.View):
51 """
52 Main application class.
53 """
54
55 def __init__(self):
56 """ Initialize """
57
58 # Call the parent class initializer
59 super().__init__()
60
61 # Variables that will hold sprite lists
62 self.player_list = arcade.SpriteList()
63 self.coin_list = arcade.SpriteList()
64
65 # Set up the player info
66 # Set up the player
67 self.player_sprite = arcade.Sprite(
68 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
69 scale=SPRITE_SCALING,
70 )
71 self.player_list.append(self.player_sprite)
72
73 self.score = 0
74 self.level = 1
75
76 # Don't show the mouse cursor
77 self.window.set_mouse_visible(False)
78
79 # Set the background color
80 self.background_color = arcade.color.AMAZON
81
82 def level_1(self):
83 for i in range(20):
84
85 # Create the coin instance
86 coin = arcade.Sprite(
87 ":resources:images/items/coinGold.png",
88 scale=SPRITE_SCALING / 3,
89 )
90
91 # Position the coin
92 coin.center_x = random.randrange(WINDOW_WIDTH)
93 coin.center_y = random.randrange(WINDOW_HEIGHT)
94
95 # Add the coin to the lists
96 self.coin_list.append(coin)
97
98 def level_2(self):
99 for i in range(30):
100
101 # Create the coin instance
102 coin = FallingCoin(
103 ":resources:images/items/coinBronze.png",
104 scale=SPRITE_SCALING / 2,
105 )
106
107 # Position the coin
108 coin.center_x = random.randrange(WINDOW_WIDTH)
109 coin.center_y = random.randrange(WINDOW_HEIGHT, WINDOW_HEIGHT * 2)
110
111 # Add the coin to the lists
112 self.coin_list.append(coin)
113
114 def level_3(self):
115 for i in range(30):
116
117 # Create the coin instance
118 coin = RisingCoin(
119 ":resources:images/items/coinSilver.png",
120 scale=SPRITE_SCALING / 2,
121 )
122
123 # Position the coin
124 coin.center_x = random.randrange(WINDOW_WIDTH)
125 coin.center_y = random.randrange(-WINDOW_HEIGHT, 0)
126
127 # Add the coin to the lists
128 self.coin_list.append(coin)
129
130 def reset(self):
131 """ Set up the game and initialize the variables. """
132
133 self.score = 0
134 self.level = 1
135
136 # Sprite lists
137 self.coin_list.clear()
138
139 self.player_sprite.center_x = 50
140 self.player_sprite.center_y = 50
141
142 self.level_1()
143
144 def on_draw(self):
145 """
146 Render the screen.
147 """
148
149 # This command has to happen before we start drawing
150 self.clear()
151
152 # Draw all the sprites.
153 arcade.draw_sprite(self.player_sprite)
154 self.coin_list.draw()
155
156 # Put the text on the screen.
157 output = f"Score: {self.score}"
158 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 15)
159
160 output = f"Level: {self.level}"
161 arcade.draw_text(output, 10, 35, arcade.color.WHITE, 15)
162
163 def on_mouse_motion(self, x, y, dx, dy):
164 """
165 Called whenever the mouse moves.
166 """
167 self.player_sprite.center_x = x
168 self.player_sprite.center_y = y
169
170 def on_update(self, delta_time):
171 """ Movement and game logic """
172
173 # Call update on all sprites (The sprites don't do much in this
174 # example though.)
175 self.coin_list.update()
176
177 # Generate a list of all sprites that collided with the player.
178 hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
179
180 # Loop through each colliding sprite, remove it, and add to the score.
181 for coin in hit_list:
182 coin.remove_from_sprite_lists()
183 self.score += 1
184
185 # See if we should go to level 2
186 if len(self.coin_list) == 0 and self.level == 1:
187 self.level += 1
188 self.level_2()
189 # See if we should go to level 3
190 elif len(self.coin_list) == 0 and self.level == 2:
191 self.level += 1
192 self.level_3()
193
194
195def main():
196 """ Main function """
197 # Create a window class. This is what actually shows up on screen
198 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
199
200 # Create and setup the GameView
201 game = GameView()
202 game.reset()
203
204 # Show GameView on screen
205 window.show_view(game)
206
207 # Start the arcade game loop
208 arcade.run()
209
210
211if __name__ == "__main__":
212 main()