Change coins

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