03_views.py Full Listing
03_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 InstructionView(arcade.View):
15 """ View to show instructions """
16
17 def on_show_view(self):
18 """ This is run once when we switch to this view """
19 self.window.background_color = arcade.csscolor.DARK_SLATE_BLUE
20
21 # Reset the viewport, necessary if we have a scrolling game and we need
22 # to reset the viewport back to the start so we can see what we draw.
23 self.window.default_camera.use()
24
25 def on_draw(self):
26 """ Draw this view """
27 self.clear()
28 arcade.draw_text("Instructions Screen", self.window.width / 2, self.window.height / 2,
29 arcade.color.WHITE, font_size=50, anchor_x="center")
30 arcade.draw_text("Click to advance", self.window.width / 2, self.window.height / 2-75,
31 arcade.color.WHITE, font_size=20, anchor_x="center")
32
33 def on_mouse_press(self, _x, _y, _button, _modifiers):
34 """ If the user presses the mouse button, start the game. """
35 game_view = GameView()
36 game_view.setup()
37 self.window.show_view(game_view)
38
39
40class GameView(arcade.View):
41 """ Our custom Window Class"""
42
43 def __init__(self):
44 """ Initializer """
45 # Call the parent class initializer
46 super().__init__()
47
48 # Variables that will hold sprite lists
49 self.player_list = None
50 self.coin_list = None
51
52 # Set up the player info
53 self.player_sprite = None
54 self.score_text = arcade.Text("Score: 0", 10, 10, arcade.color.WHITE, 14)
55 self.score = 0
56
57 # Don't show the mouse cursor
58 self.window.set_mouse_visible(False)
59
60 self.window.background_color = arcade.color.AMAZON
61
62 def setup(self):
63 """ Set up the game and initialize the variables. """
64
65 # Sprite lists
66 self.player_list = arcade.SpriteList()
67 self.coin_list = arcade.SpriteList()
68
69 # Score
70 self.score = 0
71
72 # Set up the player
73 # Character image from kenney.nl
74 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
75 SPRITE_SCALING_PLAYER)
76 self.player_sprite.center_x = 50
77 self.player_sprite.center_y = 50
78 self.player_list.append(self.player_sprite)
79
80 # Create the coins
81 for i in range(COIN_COUNT):
82
83 # Create the coin instance
84 # Coin image from kenney.nl
85 coin = arcade.Sprite(":resources:images/items/coinGold.png",
86 SPRITE_SCALING_COIN)
87
88 # Position the coin
89 coin.center_x = random.randrange(SCREEN_WIDTH)
90 coin.center_y = random.randrange(SCREEN_HEIGHT)
91
92 # Add the coin to the lists
93 self.coin_list.append(coin)
94
95 def on_draw(self):
96 """ Draw everything """
97 self.clear()
98 self.coin_list.draw()
99 self.player_list.draw()
100
101 # Put the text on the screen.
102 output = f"Score: {self.score}"
103 self.score_text.text = output
104 self.score_text.draw()
105
106
107 def on_mouse_motion(self, x, y, dx, dy):
108 """ Handle Mouse Motion """
109
110 # Move the center of the player sprite to match the mouse x, y
111 self.player_sprite.center_x = x
112 self.player_sprite.center_y = y
113
114 def on_update(self, delta_time):
115 """ Movement and game logic """
116
117 # Call update on all sprites (The sprites don't do much in this
118 # example though.)
119 self.coin_list.update()
120
121 # Generate a list of all sprites that collided with the player.
122 coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
123
124 # Loop through each colliding sprite, remove it, and add to the score.
125 for coin in coins_hit_list:
126 coin.remove_from_sprite_lists()
127 self.score += 1
128
129
130def main():
131 """ Main function """
132
133 window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
134 start_view = InstructionView()
135 window.show_view(start_view)
136 arcade.run()
137
138
139if __name__ == "__main__":
140 main()