Collect Coins that are Bouncing

sprite_collect_coins_move_bouncing.py
1"""
2Sprite Collect Moving and Bouncing Coins
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_bouncing
10"""
11
12import random
13import arcade
14
15# --- Constants ---
16SPRITE_SCALING_PLAYER = 0.75
17SPRITE_SCALING_COIN = 0.3
18COIN_COUNT = 50
19
20WINDOW_WIDTH = 1280
21WINDOW_HEIGHT = 720
22WINDOW_TITLE = "Sprite Collect Moving and Bouncing Coins Example"
23
24
25class Coin(arcade.Sprite):
26
27 def __init__(self, filename, scale):
28
29 super().__init__(filename, scale=scale)
30
31 self.change_x = 0
32 self.change_y = 0
33
34 def update(self, delta_time: float = 1 / 60):
35
36 # Move the coin
37 self.center_x += self.change_x
38 self.center_y += self.change_y
39
40 # If we are out-of-bounds, then 'bounce'
41 if self.left < 0:
42 self.change_x *= -1
43
44 if self.right > WINDOW_WIDTH:
45 self.change_x *= -1
46
47 if self.bottom < 0:
48 self.change_y *= -1
49
50 if self.top > WINDOW_HEIGHT:
51 self.change_y *= -1
52
53
54class GameView(arcade.View):
55
56 def __init__(self):
57 """ Initializer """
58 # Call the parent class initializer
59 super().__init__()
60
61 # Variables that will hold sprite lists
62 self.all_sprites_list = None
63 self.coin_list = None
64
65 # Set up the player info
66 self.player_sprite = None
67 self.score = 0
68
69 # Don't show the mouse cursor
70 self.window.set_mouse_visible(False)
71
72 self.background_color = arcade.color.AMAZON
73
74 def setup(self):
75 """ Set up the game and initialize the variables. """
76
77 # Sprite lists
78 self.all_sprites_list = arcade.SpriteList()
79 self.coin_list = arcade.SpriteList()
80
81 # Score
82 self.score = 0
83
84 # Set up the player
85 # Character image from kenney.nl
86 self.player_sprite = arcade.Sprite(
87 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
88 scale=SPRITE_SCALING_PLAYER,
89 )
90 self.player_sprite.center_x = 50
91 self.player_sprite.center_y = 50
92 self.all_sprites_list.append(self.player_sprite)
93
94 # Create the coins
95 for i in range(50):
96
97 # Create the coin instance
98 # Coin image from kenney.nl
99 coin = Coin(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
100
101 # Position the coin
102 coin.center_x = random.randrange(WINDOW_WIDTH)
103 coin.center_y = random.randrange(WINDOW_HEIGHT)
104 coin.change_x = random.randrange(-3, 4)
105 coin.change_y = random.randrange(-3, 4)
106
107 # Add the coin to the lists
108 self.all_sprites_list.append(coin)
109 self.coin_list.append(coin)
110
111 def on_draw(self):
112 """ Draw everything """
113 self.clear()
114 self.all_sprites_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.all_sprites_list.update(delta_time)
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_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 """ Main function """
146 # Create a window class. This is what actually shows up on screen
147 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
148
149 # Create and setup the GameView
150 game = GameView()
151 game.setup()
152
153 # Show GameView on screen
154 window.show_view(game)
155
156 # Start the arcade game loop
157 arcade.run()
158
159
160if __name__ == "__main__":
161 main()