forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharcade_game.py
More file actions
251 lines (198 loc) · 7.67 KB
/
Copy patharcade_game.py
File metadata and controls
251 lines (198 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Basic arcade shooter
#
# Imports
import arcade
import random
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Arcade Space Shooter"
SCALING = 2.0
# Classes
class FlyingSprite(arcade.Sprite):
"""Base class for all flying sprites
Flying sprites include enemies and clouds
"""
def update(self):
"""Update the position of the sprite
When it moves off screen to the left, remove it
"""
# Move the sprite
super().update()
# Remove us if we're off screen
if self.right < 0:
self.remove_from_sprite_lists()
class SpaceShooter(arcade.Window):
"""Space Shooter side scroller game
Player starts on the left, enemies appear on the right
Player can move anywhere, but not off screen
Enemies fly to the left at variable speed
Collisions end the game
"""
def __init__(self, width: int, height: int, title: str):
"""Initialize the game"""
super().__init__(width, height, title)
# Setup the empty sprite lists
self.enemies_list = arcade.SpriteList()
self.clouds_list = arcade.SpriteList()
self.all_sprites = arcade.SpriteList()
def setup(self):
"""Get the game ready to play"""
# Set the background color
arcade.set_background_color(arcade.color.SKY_BLUE)
# Setup the player
self.player = arcade.Sprite("images/jet.png", SCALING)
self.player.center_y = self.height / 2
self.player.left = 10
self.all_sprites.append(self.player)
# Spawn a new enemy every second
arcade.schedule(self.add_enemy, 1.0)
# Spawn a new cloud every 3 seconds
arcade.schedule(self.add_cloud, 3.0)
# Load our background music
# Sound source: http://ccmixter.org/files/Apoxode/59262
# License: https://creativecommons.org/licenses/by/3.0/
self.background_music = arcade.load_sound(
"sounds/Apoxode_-_Electric_1.wav"
)
# Load our other sounds
# Sound sources: Jon Fincher
self.collision_sound = arcade.load_sound("sounds/Collision.wav")
self.move_up_sound = arcade.load_sound("sounds/Rising_putter.wav")
self.move_down_sound = arcade.load_sound("sounds/Falling_putter.wav")
# Start the background music
arcade.play_sound(self.background_music)
# Unpause everything and reset the collision timer
self.paused = False
self.collided = False
self.collision_timer = 0.0
def add_enemy(self, delta_time: float):
"""Adds a new enemy to the screen
Arguments:
delta_time {float} -- How much time has passed since the last call
"""
# First, create the new enemy sprite
enemy = FlyingSprite("images/missile.png", SCALING)
# Set its position to a random height and off screen right
enemy.left = random.randint(self.width, self.width + 10)
enemy.top = random.randint(10, self.height - 10)
# Set its speed to a random speed heading left
enemy.velocity = (random.randint(-200, -50), 0)
# Add it to the enemies list
self.enemies_list.append(enemy)
self.all_sprites.append(enemy)
def add_cloud(self, delta_time: float):
"""Adds a new cloud to the screen
Arguments:
delta_time {float} -- How much time has passed since the last call
"""
# First, create the new cloud sprite
cloud = FlyingSprite("images/cloud.png", SCALING)
# Set its position to a random height and off screen right
cloud.left = random.randint(self.width, self.width + 10)
cloud.top = random.randint(10, self.height - 10)
# Set its speed to a random speed heading left
cloud.velocity = (random.randint(-50, -20), 0)
# Add it to the enemies list
self.clouds_list.append(cloud)
self.all_sprites.append(cloud)
def on_key_press(self, symbol: int, modifiers: int):
"""Handle user keyboard input
Q: Quit the game
P: Pause the game
I/J/K/L: Move Up, Left, Down, Right
Arrows: Move Up, Left, Down, Right
Arguments:
symbol {int} -- Which key was pressed
modifiers {int} -- Which modifiers were pressed
"""
if symbol == arcade.key.Q:
# Quit immediately
arcade.close_window()
if symbol == arcade.key.P:
self.paused = not self.paused
if symbol == arcade.key.I or symbol == arcade.key.UP:
self.player.change_y = 250
arcade.play_sound(self.move_up_sound)
if symbol == arcade.key.K or symbol == arcade.key.DOWN:
self.player.change_y = -250
arcade.play_sound(self.move_down_sound)
if symbol == arcade.key.J or symbol == arcade.key.LEFT:
self.player.change_x = -250
if symbol == arcade.key.L or symbol == arcade.key.RIGHT:
self.player.change_x = 250
def on_key_release(self, symbol: int, modifiers: int):
"""Undo movement vectors when movement keys are released
Arguments:
symbol {int} -- Which key was pressed
modifiers {int} -- Which modifiers were pressed
"""
if (
symbol == arcade.key.I
or symbol == arcade.key.K
or symbol == arcade.key.UP
or symbol == arcade.key.DOWN
):
self.player.change_y = 0
if (
symbol == arcade.key.J
or symbol == arcade.key.L
or symbol == arcade.key.LEFT
or symbol == arcade.key.RIGHT
):
self.player.change_x = 0
def on_update(self, delta_time: float):
"""Update the positions and statuses of all game objects
If we're paused, do nothing
Once everything has moved, check for collisions between
the player and the list of enemies
Arguments:
delta_time {float} -- Time since the last update
"""
# Did we collide with something earlier? If so, update our timer
if self.collided:
self.collision_timer += delta_time
# If we've paused for two seconds, we can quit
if self.collision_timer > 2.0:
arcade.close_window()
# Stop updating things as well
return
# If we're paused, don't update anything
if self.paused:
return
# Did we hit anything? If so, end the game
if self.player.collides_with_list(self.enemies_list):
self.collided = True
self.collision_timer = 0.0
arcade.play_sound(self.collision_sound)
# Update everything
for sprite in self.all_sprites:
sprite.center_x = int(
sprite.center_x + sprite.change_x * delta_time
)
sprite.center_y = int(
sprite.center_y + sprite.change_y * delta_time
)
# self.all_sprites.update()
# Keep the player on screen
if self.player.top > self.height:
self.player.top = self.height
if self.player.right > self.width:
self.player.right = self.width
if self.player.bottom < 0:
self.player.bottom = 0
if self.player.left < 0:
self.player.left = 0
def on_draw(self):
"""Draw all game objects"""
arcade.start_render()
self.all_sprites.draw()
if __name__ == "__main__":
# Create a new Space Shooter window
space_game = SpaceShooter(
int(SCREEN_WIDTH * SCALING), int(SCREEN_HEIGHT * SCALING), SCREEN_TITLE
)
# Setup to play
space_game.setup()
# Run the game
arcade.run()