01_views.py完整列表#
01_views.py#
1import random
2import arcade
3
4# --- Constants ---
5SPRITE_SCALING_PLAYER = 0.5
6SPRITE_SCALING_COIN = .25
7COIN_COUNT = 25
8
9SCREEN_WIDTH = 800
10SCREEN_HEIGHT = 600
11SCREEN_TITLE = "Implement Views Example"
12
13
14class MyGame(arcade.Window):
15 """ Our custom Window Class"""
16
17 def __init__(self):
18 """ Initializer """
19 # Call the parent class initializer
20 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
21
22 # Variables that will hold sprite lists
23 self.player_list = None
24 self.coin_list = None
25
26 # Set up the player info
27 self.player_sprite = None
28 self.score = 0
29
30 # Don't show the mouse cursor
31 self.set_mouse_visible(False)
32
33 self.background_color = arcade.color.AMAZON
34
35 def setup(self):
36 """ Set up the game and initialize the variables. """
37
38 # Sprite lists
39 self.player_list = arcade.SpriteList()
40 self.coin_list = arcade.SpriteList()
41
42 # Score
43 self.score = 0
44
45 # Set up the player
46 # Character image from kenney.nl
47 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
48 SPRITE_SCALING_PLAYER)
49 self.player_sprite.center_x = 50
50 self.player_sprite.center_y = 50
51 self.player_list.append(self.player_sprite)
52
53 # Create the coins
54 for i in range(COIN_COUNT):
55
56 # Create the coin instance
57 # Coin image from kenney.nl
58 coin = arcade.Sprite(":resources:images/items/coinGold.png",
59 SPRITE_SCALING_COIN)
60
61 # Position the coin
62 coin.center_x = random.randrange(SCREEN_WIDTH)
63 coin.center_y = random.randrange(SCREEN_HEIGHT)
64
65 # Add the coin to the lists
66 self.coin_list.append(coin)
67
68 def on_draw(self):
69 """ Draw everything """
70 self.clear()
71 self.coin_list.draw()
72 self.player_list.draw()
73
74 # Put the text on the screen.
75 output = f"Score: {self.score}"
76 arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
77
78 def on_mouse_motion(self, x, y, dx, dy):
79 """ Handle Mouse Motion """
80
81 # Move the center of the player sprite to match the mouse x, y
82 self.player_sprite.center_x = x
83 self.player_sprite.center_y = y
84
85 def on_update(self, delta_time):
86 """ Movement and game logic """
87
88 # Call update on all sprites (The sprites don't do much in this
89 # example though.)
90 self.coin_list.update()
91
92 # Generate a list of all sprites that collided with the player.
93 coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
94
95 # Loop through each colliding sprite, remove it, and add to the score.
96 for coin in coins_hit_list:
97 coin.remove_from_sprite_lists()
98 self.score += 1
99
100
101def main():
102 """ Main function """
103 window = MyGame()
104 window.setup()
105 arcade.run()
106
107
108if __name__ == "__main__":
109 main()