Collect Coins Moving Down¶
For a complete explanation, see:
1"""
2Sprite Collect Coins Moving Down
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_move_down
10"""
11
12import random
13import arcade
14import os
15
16# --- Constants ---
17SPRITE_SCALING_PLAYER = 0.5
18SPRITE_SCALING_COIN = 0.2
19COIN_COUNT = 50
20
21SCREEN_WIDTH = 800
22SCREEN_HEIGHT = 600
23SCREEN_TITLE = "Sprite Collect Coins Moving Down Example"
24
25
26class Coin(arcade.Sprite):
27 """
28 This class represents the coins on our screen. It is a child class of
29 the arcade library's "Sprite" class.
30 """
31
32 def reset_pos(self):
33
34 # Reset the coin to a random spot above the screen
35 self.center_y = random.randrange(SCREEN_HEIGHT + 20,
36 SCREEN_HEIGHT + 100)
37 self.center_x = random.randrange(SCREEN_WIDTH)
38
39 def update(self):
40
41 # Move the coin
42 self.center_y -= 1
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 MyGame(arcade.Window):
51 """ Our custom Window Class"""
52
53 def __init__(self):
54 """ Initializer """
55
56 # Call the parent class initializer
57 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
58
59 # Set the working directory (where we expect to find files) to the same
60 # directory this .py file is in. You can leave this out of your own
61 # code, but it is needed to easily run the examples using "python -m"
62 # as mentioned at the top of this program.
63 file_path = os.path.dirname(os.path.abspath(__file__))
64 os.chdir(file_path)
65
66 # Variables that will hold sprite lists
67 self.player_sprite_list = None
68 self.coin_sprite_list = None
69
70 # Set up the player info
71 self.player_sprite = None
72 self.score = 0
73
74 # Don't show the mouse cursor
75 self.set_mouse_visible(False)
76
77 arcade.set_background_color(arcade.color.AMAZON)
78
79 def setup(self):
80 """ Set up the game and initialize the variables. """
81
82 # Sprite lists
83 self.player_sprite_list = arcade.SpriteList()
84 self.coin_sprite_list = arcade.SpriteList()
85
86 # Score
87 self.score = 0
88
89 # Set up the player
90 # Character image from kenney.nl
91 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
92 self.player_sprite.center_x = 50
93 self.player_sprite.center_y = 50
94 self.player_sprite_list.append(self.player_sprite)
95
96 # Create the coins
97 for i in range(COIN_COUNT):
98
99 # Create the coin instance
100 # Coin image from kenney.nl
101 coin = Coin(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
102
103 # Position the coin
104 coin.center_x = random.randrange(SCREEN_WIDTH)
105 coin.center_y = random.randrange(SCREEN_HEIGHT)
106
107 # Add the coin to the lists
108 self.coin_sprite_list.append(coin)
109
110 def on_draw(self):
111 """ Draw everything """
112 arcade.start_render()
113 self.coin_sprite_list.draw()
114 self.player_sprite_list.draw()
115
116 # Put the text on the screen.
117 output = f"Score: {self.score}"
118 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
119
120 def on_mouse_motion(self, x, y, dx, dy):
121 """ Handle Mouse Motion """
122
123 # Move the center of the player sprite to match the mouse x, y
124 self.player_sprite.center_x = x
125 self.player_sprite.center_y = y
126
127 def on_update(self, delta_time):
128 """ Movement and game logic """
129
130 # Call update on all sprites (The sprites don't do much in this
131 # example though.)
132 self.coin_sprite_list.update()
133
134 # Generate a list of all sprites that collided with the player.
135 hit_list = arcade.check_for_collision_with_list(self.player_sprite,
136 self.coin_sprite_list)
137
138 # Loop through each colliding sprite, remove it, and add to the score.
139 for coin in hit_list:
140 coin.remove_from_sprite_lists()
141 self.score += 1
142
143
144def main():
145 window = MyGame()
146 window.setup()
147 arcade.run()
148
149
150if __name__ == "__main__":
151 main()