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