通过鼠标移动#
这是一个显示精灵基本用法的示例。用你的鼠标收集硬币,并记录分数!

源代码#
sprite_collect_coins.py#
1"""
2Sprite Collect Coins
3
4A simple game demonstrating an easy way to create and use sprites.
5
6Artwork from https://kenney.nl
7
8If Python and Arcade are installed, this example can be run from the
9command line with:
10python -m arcade.examples.sprite_collect_coins
11"""
12
13import random
14import arcade
15
16# --- Constants ---
17SPRITE_SCALING_PLAYER = 0.5
18SPRITE_SCALING_COIN = .25
19COIN_COUNT = 50
20
21SCREEN_WIDTH = 800
22SCREEN_HEIGHT = 600
23SCREEN_TITLE = "Sprite Collect Coins Example"
24
25
26class MyGame(arcade.Window):
27 """ Our custom Window Class"""
28
29 def __init__(self):
30 """ Initializer """
31 # Call the parent class initializer
32 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
33
34 # Variables that will hold sprite lists
35 self.player_list = None
36 self.coin_list = None
37
38 # Create a variable to hold the player sprite
39 self.player_sprite = None
40
41 # Variables to hold the score and the Text object displaying it
42 self.score = 0
43 self.score_display = None
44
45 # Hide the mouse cursor while it's over the window
46 self.set_mouse_visible(False)
47
48 self.background_color = arcade.color.AMAZON
49
50 def setup(self):
51 """ Set up the game and initialize the variables. """
52
53 # Create the sprite lists
54 self.player_list = arcade.SpriteList()
55 self.coin_list = arcade.SpriteList()
56
57 # Reset the score and the score display
58 self.score = 0
59 self.score_display = arcade.Text(
60 text="Score: 0", x=10, y=20,
61 color=arcade.color.WHITE, font_size=14)
62
63 # Set up the player
64 # Character image from kenney.nl
65 img = ":resources:images/animated_characters/female_person/femalePerson_idle.png"
66 self.player_sprite = arcade.Sprite(img, scale=SPRITE_SCALING_PLAYER)
67 self.player_sprite.position = 50, 50
68 self.player_list.append(self.player_sprite)
69
70 # Create the coins
71 for i in range(COIN_COUNT):
72
73 # Create the coin instance
74 # Coin image from kenney.nl
75 coin = arcade.Sprite(":resources:images/items/coinGold.png",
76 scale=SPRITE_SCALING_COIN)
77
78 # Position the coin
79 coin.center_x = random.randrange(SCREEN_WIDTH)
80 coin.center_y = random.randrange(SCREEN_HEIGHT)
81
82 # Add the coin to the lists
83 self.coin_list.append(coin)
84
85 def on_draw(self):
86 """ Draw everything """
87
88 # Clear the screen to only show the background color
89 self.clear()
90
91 # Draw the sprites
92 self.coin_list.draw()
93 self.player_list.draw()
94
95 # Draw the score Text object on the screen
96 self.score_display.draw()
97
98 def on_mouse_motion(self, x, y, dx, dy):
99 """ Handle Mouse Motion """
100
101 # Move the player sprite to place its center on the mouse x, y
102 self.player_sprite.position = x, y
103
104 def on_update(self, delta_time):
105 """ Movement and game logic """
106
107 # Generate a list of all sprites that collided with the player.
108 coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite,
109 self.coin_list)
110
111 # Keep track of the score from before collisions occur
112 old_score = self.score
113
114 # Loop through each colliding sprite, remove it, and add to the score.
115 for coin in coins_hit_list:
116 coin.remove_from_sprite_lists()
117 self.score += 1
118
119 # Update the score display if the score changed this tick
120 if old_score != self.score:
121 self.score_display.text = f"Score: {self.score}"
122
123
124def main():
125 """ Main function """
126 window = MyGame()
127 window.setup()
128 arcade.run()
129
130
131if __name__ == "__main__":
132 main()