1"""
2Sprite Change Coins
3
4This shows how you can change a sprite once it is hit, rather than eliminate it.
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_change_coins
10"""
11
12import random
13import arcade
14import os
15
16SPRITE_SCALING = 1
17
18SCREEN_WIDTH = 800
19SCREEN_HEIGHT = 600
20SCREEN_TITLE = "Sprite Change Coins"
21
22
23class Collectable(arcade.Sprite):
24 """ This class represents something the player collects. """
25
26 def __init__(self, filename, scale):
27 super().__init__(filename, scale)
28 # Flip this once the coin has been collected.
29 self.changed = False
30
31
32class MyGame(arcade.Window):
33 """
34 Main application class.a
35 """
36
37 def __init__(self, width, height, title):
38 super().__init__(width, height, title)
39
40 # Set the working directory (where we expect to find files) to the same
41 # directory this .py file is in. You can leave this out of your own
42 # code, but it is needed to easily run the examples using "python -m"
43 # as mentioned at the top of this program.
44 file_path = os.path.dirname(os.path.abspath(__file__))
45 os.chdir(file_path)
46
47 # Sprite lists
48 self.player_list = None
49 self.coin_list = None
50
51 # Set up the player
52 self.score = 0
53 self.player_sprite = None
54
55 def setup(self):
56 """ Set up the game and initialize the variables. """
57
58 # Sprite lists
59 self.player_list = arcade.SpriteList()
60 self.coin_list = arcade.SpriteList()
61
62 # Set up the player
63 self.score = 0
64 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", 0.5)
65 self.player_sprite.center_x = 50
66 self.player_sprite.center_y = 50
67 self.player_list.append(self.player_sprite)
68
69 for i in range(50):
70 # Create the coin instance
71 coin = Collectable(":resources:images/items/coinGold.png", SPRITE_SCALING)
72 coin.width = 30
73 coin.height = 30
74
75 # Position the coin
76 coin.center_x = random.randrange(SCREEN_WIDTH)
77 coin.center_y = random.randrange(SCREEN_HEIGHT)
78
79 # Add the coin to the lists
80 self.coin_list.append(coin)
81
82 # Don't show the mouse cursor
83 self.set_mouse_visible(False)
84
85 # Set the background color
86 arcade.set_background_color(arcade.color.AMAZON)
87
88 def on_draw(self):
89 """
90 Render the screen.
91 """
92
93 # This command has to happen before we start drawing
94 arcade.start_render()
95
96 # Draw all the sprites.
97 self.coin_list.draw()
98 self.player_list.draw()
99
100 # Put the text on the screen.
101 output = f"Score: {self.score}"
102 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
103
104 def on_mouse_motion(self, x, y, dx, dy):
105 """
106 Called whenever the mouse moves.
107 """
108 self.player_sprite.center_x = x
109 self.player_sprite.center_y = y
110
111 def on_update(self, delta_time):
112 """ Movement and game logic """
113
114 # Call update on all sprites (The sprites don't do much in this
115 # example though.)
116 self.player_list.update()
117 self.coin_list.update()
118
119 # Generate a list of all sprites that collided with the player.
120 hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
121
122 # Loop through each colliding sprite, change it, and add to the score.
123 for coin in hit_list:
124 # Have we collected this?
125 if not coin.changed:
126 # No? Then do so
127 coin.append_texture(arcade.load_texture(":resources:images/pinball/bumper.png"))
128 coin.set_texture(1)
129 coin.changed = True
130 coin.width = 30
131 coin.height = 30
132 self.score += 1
133
134
135def main():
136 """ Main method """
137 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
138 window.setup()
139 arcade.run()
140
141
142if __name__ == "__main__":
143 main()