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