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.5
17SPRITE_SCALING_COIN = 0.2
18COIN_COUNT = 50
19
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22SCREEN_TITLE = "Sprite Collect Moving and Bouncing Coins Example"
23
24
25class Coin(arcade.Sprite):
26
27 def __init__(self, filename, sprite_scaling):
28
29 super().__init__(filename, sprite_scaling)
30
31 self.change_x = 0
32 self.change_y = 0
33
34 def update(self):
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 > SCREEN_WIDTH:
45 self.change_x *= -1
46
47 if self.bottom < 0:
48 self.change_y *= -1
49
50 if self.top > SCREEN_HEIGHT:
51 self.change_y *= -1
52
53
54class MyGame(arcade.Window):
55 """ Our custom Window Class"""
56
57 def __init__(self):
58 """ Initializer """
59 # Call the parent class initializer
60 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
61
62 # Variables that will hold sprite lists
63 self.all_sprites_list = None
64 self.coin_list = None
65
66 # Set up the player info
67 self.player_sprite = None
68 self.score = 0
69
70 # Don't show the mouse cursor
71 self.set_mouse_visible(False)
72
73 arcade.set_background_color(arcade.color.AMAZON)
74
75 def setup(self):
76 """ Set up the game and initialize the variables. """
77
78 # Sprite lists
79 self.all_sprites_list = arcade.SpriteList()
80 self.coin_list = arcade.SpriteList()
81
82 # Score
83 self.score = 0
84
85 # Set up the player
86 # Character image from kenney.nl
87 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
88 "femalePerson_idle.png", SPRITE_SCALING_PLAYER)
89 self.player_sprite.center_x = 50
90 self.player_sprite.center_y = 50
91 self.all_sprites_list.append(self.player_sprite)
92
93 # Create the coins
94 for i in range(50):
95
96 # Create the coin instance
97 # Coin image from kenney.nl
98 coin = Coin(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
99
100 # Position the coin
101 coin.center_x = random.randrange(SCREEN_WIDTH)
102 coin.center_y = random.randrange(SCREEN_HEIGHT)
103 coin.change_x = random.randrange(-3, 4)
104 coin.change_y = random.randrange(-3, 4)
105
106 # Add the coin to the lists
107 self.all_sprites_list.append(coin)
108 self.coin_list.append(coin)
109
110 def on_draw(self):
111 """ Draw everything """
112 self.clear()
113 self.all_sprites_list.draw()
114
115 # Put the text on the screen.
116 output = f"Score: {self.score}"
117 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
118
119 def on_mouse_motion(self, x, y, dx, dy):
120 """ Handle Mouse Motion """
121
122 # Move the center of the player sprite to match the mouse x, y
123 self.player_sprite.center_x = x
124 self.player_sprite.center_y = y
125
126 def on_update(self, delta_time):
127 """ Movement and game logic """
128
129 # Call update on all sprites (The sprites don't do much in this
130 # example though.)
131 self.all_sprites_list.update()
132
133 # Generate a list of all sprites that collided with the player.
134 hit_list = arcade.check_for_collision_with_list(self.player_sprite,
135 self.coin_list)
136
137 # Loop through each colliding sprite, remove it, and add to the score.
138 for coin in hit_list:
139 coin.remove_from_sprite_lists()
140 self.score += 1
141
142
143def main():
144 window = MyGame()
145 window.setup()
146 arcade.run()
147
148
149if __name__ == "__main__":
150 main()