找零硬币#

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 = 1
16
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_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 MyGame(arcade.Window):
32 """
33 Main application class.a
34 """
35
36 def __init__(self, width, height, title):
37 super().__init__(width, height, title)
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
47 def setup(self):
48 """ Set up the game and initialize the variables. """
49
50 # Sprite lists
51 self.player_list = arcade.SpriteList()
52 self.coin_list = arcade.SpriteList()
53
54 # Set up the player
55 self.score = 0
56 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
57 "femalePerson_idle.png", scale=0.5)
58 self.player_sprite.center_x = 50
59 self.player_sprite.center_y = 50
60 self.player_list.append(self.player_sprite)
61
62 for i in range(50):
63 # Create the coin instance
64 coin = Collectable(":resources:images/items/coinGold.png", scale=SPRITE_SCALING)
65 coin.width = 30
66 coin.height = 30
67
68 # Position the coin
69 coin.center_x = random.randrange(SCREEN_WIDTH)
70 coin.center_y = random.randrange(SCREEN_HEIGHT)
71
72 # Add the coin to the lists
73 self.coin_list.append(coin)
74
75 # Don't show the mouse cursor
76 self.set_mouse_visible(False)
77
78 # Set the background color
79 self.background_color = arcade.color.AMAZON
80
81 def on_draw(self):
82 """
83 Render the screen.
84 """
85
86 # This command has to happen before we start drawing
87 self.clear()
88
89 # Draw all the sprites.
90 self.coin_list.draw()
91 self.player_list.draw()
92
93 # Put the text on the screen.
94 output = f"Score: {self.score}"
95 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
96
97 def on_mouse_motion(self, x, y, dx, dy):
98 """
99 Called whenever the mouse moves.
100 """
101 self.player_sprite.center_x = x
102 self.player_sprite.center_y = y
103
104 def on_update(self, delta_time):
105 """ Movement and game logic """
106
107 # Call update on all sprites (The sprites don't do much in this
108 # example though.)
109 self.player_list.update()
110 self.coin_list.update()
111
112 # Generate a list of all sprites that collided with the player.
113 hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
114
115 # Loop through each colliding sprite, change it, and add to the score.
116 for coin in hit_list:
117 # Have we collected this?
118 if not coin.changed:
119 # No? Then do so
120 coin.append_texture(arcade.load_texture(":resources:images/pinball/bumper.png"))
121 coin.set_texture(1)
122 coin.changed = True
123 coin.width = 30
124 coin.height = 30
125 self.score += 1
126
127
128def main():
129 """ Main function """
130 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
131 window.setup()
132 arcade.run()
133
134
135if __name__ == "__main__":
136 main()