Animated Sprites

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