Collect Coins Moving Down
For a complete explanation, see Chapter 22 of Arcade Academy - Learn Python.

sprite_collect_coins_move_down.py
1"""
2Sprite Collect Coins Moving Down
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_down
10"""
11
12import random
13import arcade
14
15# --- Constants ---
16SPRITE_SCALING_PLAYER = 0.5
17SPRITE_SCALING_COIN = 0.3
18COIN_COUNT = 50
19
20WINDOW_WIDTH = 1280
21WINDOW_HEIGHT = 720
22WINDOW_TITLE = "Sprite Collect Coins Moving Down Example"
23
24
25class Coin(arcade.Sprite):
26 """
27 This class represents the coins on our screen. It is a child class of
28 the Arcade library's "Sprite" class.
29 """
30
31 def reset_pos(self):
32 # Reset the coin to a random spot above the screen
33 self.center_y = random.randrange(WINDOW_HEIGHT + 20,
34 WINDOW_HEIGHT + 100)
35 self.center_x = random.randrange(WINDOW_WIDTH)
36
37 def update(self, delta_time: float = 1/60):
38 # Take frame time into account
39 time_step = delta_time * 60
40
41 # Move the coin 1 pixel down per 1/60th of a second
42 self.center_y -= 1 * time_step
43
44 # See if the coin has fallen off the bottom of the screen.
45 # If so, reset it.
46 if self.top < 0:
47 self.reset_pos()
48
49
50class GameView(arcade.View):
51
52 def __init__(self):
53 """ Initializer """
54
55 # Call the parent class initializer
56 super().__init__()
57
58 # Variables that will hold sprite lists
59 self.player_sprite_list = None
60 self.coin_sprite_list = None
61
62 # Set up the player info
63 self.player_sprite = None
64 self.score = 0
65
66 # Don't show the mouse cursor
67 self.window.set_mouse_visible(False)
68
69 self.background_color = arcade.color.AMAZON
70
71 def setup(self):
72 """ Set up the game and initialize the variables. """
73
74 # Sprite lists
75 self.player_sprite_list = arcade.SpriteList()
76 self.coin_sprite_list = arcade.SpriteList()
77
78 # Score
79 self.score = 0
80
81 # Set up the player
82 # Character image from kenney.nl
83 self.player_sprite = arcade.Sprite(
84 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
85 scale=SPRITE_SCALING_PLAYER,
86 )
87 self.player_sprite.center_x = 50
88 self.player_sprite.center_y = 50
89 self.player_sprite_list.append(self.player_sprite)
90
91 # Create the coins
92 for i in range(COIN_COUNT):
93
94 # Create the coin instance
95 # Coin image from kenney.nl
96 coin = Coin(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
97
98 # Position the coin
99 coin.center_x = random.randrange(WINDOW_WIDTH)
100 coin.center_y = random.randrange(WINDOW_HEIGHT)
101
102 # Add the coin to the lists
103 self.coin_sprite_list.append(coin)
104
105 def on_draw(self):
106 """ Draw everything """
107 self.clear()
108 self.coin_sprite_list.draw()
109 self.player_sprite_list.draw()
110
111 # Put the text on the screen.
112 output = f"Score: {self.score}"
113 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
114
115 def on_mouse_motion(self, x, y, dx, dy):
116 """ Handle Mouse Motion """
117
118 # Move the center of the player sprite to match the mouse x, y
119 self.player_sprite.center_x = x
120 self.player_sprite.center_y = y
121
122 def on_update(self, delta_time):
123 """ Movement and game logic """
124
125 # Call update on all sprites (The sprites don't do much in this
126 # example though.)
127 self.coin_sprite_list.update(delta_time)
128
129 # Generate a list of all sprites that collided with the player.
130 hit_list = arcade.check_for_collision_with_list(self.player_sprite,
131 self.coin_sprite_list)
132
133 # Loop through each colliding sprite, remove it, and add to the score.
134 for coin in hit_list:
135 coin.remove_from_sprite_lists()
136 self.score += 1
137
138
139def main():
140 """ Main function """
141 # Create a window class. This is what actually shows up on screen
142 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
143
144 # Create and setup the GameView
145 game = GameView()
146 game.setup()
147
148 # Show GameView on screen
149 window.show_view(game)
150
151 # Start the arcade game loop
152 arcade.run()
153
154
155if __name__ == "__main__":
156 main()