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