-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_game.py
603 lines (477 loc) · 18.7 KB
/
my_game.py
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
"""
Simple program to show moving a sprite with the keyboard.
This program uses the Arcade library found at http://arcade.academy
Artwork from https://kenney.nl/assets/space-shooter-redux
"""
import arcade
import arcade.gui
import random
import time
from pyglet.math import Vec2
# Import sprites from local file my_sprites.py
from my_sprites import Enemy, Weapon, WeaponType, EntityType, EnemyState
from my_helpers import GameState, MAP_LAYER_CONFIG, Gore
# Set the scaling of all sprites in the game
SCALING = 1
# Draw bitmaps without smooth interpolation
DRAW_PIXELATED = True
# should we draw hitboxes, and other info relevant when debugging
DEBUG_MODE = True
# Tiles are squares
TILE_SIZE = 16
# Move the map down from the top left corner by this much
# This will create an area on the screen for score etc.
GUI_HEIGHT = 2 * TILE_SIZE * SCALING
MAP_WIDTH_TILES = 30
MAP_HEIGHT_TILES = 30
# Fonts
MAIN_FONT_NAME = "Kenney Pixel"
# Set the size of the screen
SCREEN_WIDTH = MAP_WIDTH_TILES * TILE_SIZE * SCALING
SCREEN_HEIGHT = MAP_HEIGHT_TILES * TILE_SIZE * SCALING + GUI_HEIGHT
# Variables controlling the player
PLAYER_SPEED = 6000
PLAYER_SHOT_SPEED = 300
PLAYER_SIGHT_RANGE = SCREEN_WIDTH/4 # How far can the player see?
# Amount of players
NUM_OF_PLAYERS = 2
FIRE_KEY = arcade.key.SPACE
class GameView(arcade.View):
"""
The view with the game itself
"""
def __init__(self, game_state: GameState):
"""
level: The level number to load
player_sprite_list: The Players to add to the level
"""
super(GameView, self).__init__()
self.game_state = game_state
def on_show_view(self):
"""
This is run once when we switch to this view
"""
# Set up the player info
# FIXME: Move this into the Player class
# Get list of joysticks and select the first to be given to the player
joysticks = arcade.get_joysticks()
if joysticks:
print("Found {} joystick(s)".format(len(joysticks)))
joystick = joysticks[0]
else:
print("No joysticks found")
joystick = None
# Emitters like Gore
self.emitters = []
self.player_score = 0
# Get list of joysticks
joysticks = arcade.get_joysticks()
if joysticks:
print("Found {} joystick(s)".format(len(joysticks)))
# Use 1st joystick found
self.joystick = joysticks[0]
# Communicate with joystick
self.joystick.open()
# Map joysticks functions to local functions
self.joystick.on_joybutton_press = self.on_joybutton_press
self.joystick.on_joybutton_release = self.on_joybutton_release
self.joystick.on_joyaxis_motion = self.on_joyaxis_motion
self.joystick.on_joyhat_motion = self.on_joyhat_motion
else:
print("No joysticks found")
self.joystick = None
# Set the background color
arcade.set_background_color(arcade.color.BLACK)
def on_draw(self):
"""
Render the screen.
"""
# Clear screen so we can draw new stuff
self.clear()
# Draw the sprite list from the map if configured to be drawn
for layer_name, layer_sprites in self.game_state.tilemap.sprite_lists.items():
if MAP_LAYER_CONFIG[layer_name].get("draw", True):
if not MAP_LAYER_CONFIG[layer_name].get("line_of_sight", False):
# If the layer is not configured as line_of_sight, all tiles will be drawn
layer_sprites.draw(pixelated=DRAW_PIXELATED)
else:
# Run through line_of_sight tiles
for s in layer_sprites:
if s.seen:
# If the tile has already been seen, draw it and skip the rest.
s.draw(pixelated=DRAW_PIXELATED)
else:
# If player has line of sight to an unseen tile, it's marked as seen
try:
if arcade.has_line_of_sight(
point_1 = s.position,
point_2 = self.game_state.players[0].position,
walls = self.game_state.tilemap.sprite_lists["impassable"],
check_resolution = TILE_SIZE*2,
max_distance = PLAYER_SIGHT_RANGE
):
s.seen = True
except ZeroDivisionError:
# An error may occur in the has_line_of_sight() function
# if the distance between point_1 and point_2 is too close to zero.
# In that case we assume that the tile has already been seen.
pass
# Draw players score on screen
arcade.draw_text(
f"SCORE: {self.player_score}", # Text to show
10, # X position
SCREEN_HEIGHT - 20, # Y positon
arcade.color.WHITE, # Color of text
font_size=TILE_SIZE,
font_name=MAIN_FONT_NAME,
bold=True,
)
# Draw the player sprite and its objects (weapon & emotes)
for p in self.game_state.players:
# Draw the player sprite
# Draw the player sprite and its attacks and emotes
p.draw(pixelated=DRAW_PIXELATED)
p.draw_sprites(draw_attack_points=DEBUG_MODE, pixelated=DRAW_PIXELATED)
for e in self.game_state.enemies:
e.draw(pixelated=DRAW_PIXELATED)
e.draw_sprites(draw_attack_points=DEBUG_MODE, pixelated=DRAW_PIXELATED)
def on_update(self, delta_time: float = 1/60):
"""
Movement and game logic
"""
# Change view if current level has been cleared
if self.game_state.level_clear:
print("INFO: Level cleared")
view = LevelFinishView(self.game_state)
self.window.show_view(view)
sum_of_all_players_hp = 0
# Check for player collisions
for p in self.game_state.players:
sum_of_all_players_hp += p.hp
# Ignore dead Players.
if p.hp == 0:
continue
for e in self.game_state.enemies:
# Check if the enemy's weapon has hit the player
if e.equipped_weapon is not None and e.equipped_weapon.attack_point is not None:
if p.collides_with_point(e.equipped_weapon.attack_point):
p.hp -= e.equipped_weapon.strength
e.equipped_weapon.attack_point = None
p.pause_timer = 0.2
self.game_state.physics_engine.apply_impulse(p, e.equipped_weapon.knock_back_force)
# Check if the player's weapon has hit the enemy
if p.equipped_weapon is not None and p.equipped_weapon.attack_point is not None:
if e.collides_with_point(p.equipped_weapon.attack_point):
e.hp -= p.equipped_weapon.strength
p.equipped_weapon.attack_point = None
e.pause_timer = 0.2
self.game_state.physics_engine.apply_impulse(e, p.equipped_weapon.knock_back_force)
# When enemy health drops below 1 then kill.
if e.hp < 1:
# Fast splat.
self.emitters.append(Gore(e.position, amount=300, speed=3, lifetime=0.5, start_fade=250, scale=1.2))
# Slow blood spot.
self.emitters.append(Gore(e.position, amount=100, speed=0.1, lifetime=3, start_fade=20, scale=1.4))
# Kill the enemy.
e.kill()
# Pick up weapons from tilemap if the players are standing on any
for w in self.game_state.tilemap.sprite_lists["weapons"]:
if arcade.check_for_collision(p, w):
# create a weapon type based on the tile id. If the tile is not a weapon, raise an error
new_weapon_type = WeaponType(w.properties["tile_id"])
p.add_weapon(Weapon(new_weapon_type))
# Remove weapon from tilemap, as the player has picked it up
w.kill()
# Updates the player_sprite_list.
p.update()
# When all players HP drops below 1 then GAME OVER.
if sum_of_all_players_hp == 0:
self.game_over()
# Update the enemies
self.game_state.enemies.update()
self.game_state.physics_engine.step()
for e in self.emitters:
e.update()
def game_over(self):
"""
Call this when the game is over
"""
# Freeze to see how the player/players died.
time.sleep(2)
# Create a game over view
game_over_view = GameOverView(score=self.player_score)
# Change to game over view
self.window.show_view(game_over_view)
def on_key_press(self, key, modifiers):
for p in self.game_state.players:
p.on_key_press(key, modifiers)
# End the game if the escape key is pressed
if key == arcade.key.ESCAPE:
self.game_over()
elif key == arcade.key.R and DEBUG_MODE:
# Restarts the game when Debug mode is True and you pressed R.
game_view = GameView()
self.window.show_view(game_view)
print("Game Reset! 🔁 -- Turn Debug mode off to remove this feature! ✔")
def on_key_release(self, key, modifiers):
for p in self.game_state.players:
p.on_key_release(key, modifiers)
class IntroView(arcade.View):
"""
View to show instructions
"""
def on_show_view(self):
"""
This is run once when we switch to this view
"""
self.opening_sound = arcade.load_sound("data/audio/rpg/opening_sound.wav")
self.opening_sound_player = self.opening_sound.play()
# Set the background color
arcade.set_background_color(arcade.csscolor.SLATE_GREY)
# Reset the viewport, necessary if we have a scrolling game and we need
# to reset the viewport back to the start so we can see what we draw.
arcade.set_viewport(0, self.window.width, 0, self.window.height)
button_scaling = 1.6
# Make the title Sprite
self.title = arcade.Sprite(
"images/GUI/title_game_start.png",
button_scaling*1.5
)
self.title.center_x = SCREEN_WIDTH//2
self.title.center_y = 350
# Makes the manager that contains the GUI button and enables it to the game.
self.manager = arcade.gui.UIManager()
self.manager.enable()
# Makes the play button.
self.one_player = arcade.gui.UITextureButton(
x=50,
y=125,
width=100,
height=100,
texture=arcade.load_texture("images/GUI/button_players_one.png"),
texture_hovered=arcade.load_texture("images/GUI/button_players_one_chosen.png"),
scale=button_scaling*2,
style=None
)
# Makes the play button.
self.two_player = arcade.gui.UITextureButton(
x=350,
y=125,
width=100,
height=100,
texture=arcade.load_texture("images/GUI/button_players_two.png"),
texture_hovered=arcade.load_texture("images/GUI/button_players_two_chosen.png"),
scale=button_scaling*2,
style=None
)
# Adds the play button to the manager.
self.manager.add(self.one_player)
self.manager.add(self.two_player)
# Makes it to when the player presses the play button it starts the game.
self.one_player.on_click = self.set_one_player
self.two_player.on_click = self.set_two_players
def on_draw(self):
"""
Draw this view
"""
self.clear()
# Draws the title and the manager which has the play button.
self.title.draw(pixelated=DRAW_PIXELATED)
self.manager.draw()
# Info how to also start the game.
arcade.draw_text(
"Select number of players.",
self.window.width / 2,
60,
arcade.color.BLACK,
font_size=15,
font_name=MAIN_FONT_NAME,
anchor_x="center",
bold=True
)
def set_one_player(self, event=None):
"""
Sets the normal value of players to one.
"""
self.start_game(1)
def set_two_players(self, event=None):
"""
Sets the normal value of players to two.
"""
self.start_game(2)
def on_key_press(self, key: int, modifiers: int):
"""
Start the game when any key is pressed
"""
if key == arcade.key.KEY_1:
self.start_game(1)
if key == arcade.key.KEY_2:
self.start_game(2)
def start_game(self, no_of_players):
"""
Starts the game.
"""
# The initial game state
game_state = GameState(
no_of_players=no_of_players,
window=self.window,
map_no=0,
map_width_tiles=MAP_WIDTH_TILES,
map_height_tiles=MAP_HEIGHT_TILES,
tile_size=TILE_SIZE,
)
# Prevent the sound from playing after the game starts
self.opening_sound.stop(self.opening_sound_player)
print("INFO: Number of players:", no_of_players)
game_view = GameView(game_state)
self.window.show_view(game_view)
class GameOverView(arcade.View):
"""
View to show when the game is over
"""
def __init__(self, score, window=None):
"""
Create a Game Over-view. Pass the final score to display.
"""
self.score = score
super().__init__(window)
def setup_old(self, score: int):
"""
Call this from the game so we can show the score.
"""
self.score = score
def on_show_view(self):
"""
This is run once when we switch to this view
"""
button_scaling = 1.6
# Set the background color
arcade.set_background_color(arcade.csscolor.BLACK)
# Reset the viewport, necessary if we have a scrolling game and we need
# to reset the viewport back to the start so we can see what we draw.
arcade.set_viewport(0, self.window.width, 0, self.window.height)
# Make the title Sprite
self.title = arcade.Sprite(
filename="images/GUI/title_game_over.png",
scale=button_scaling*2,
center_x=SCREEN_WIDTH//2,
center_y=350,
)
self.subtitle = arcade.Sprite(
filename=f"images/GUI/end_text_{random.randint(1, 5)}.png",
scale=button_scaling*2,
center_x=self.title.center_x,
center_y=self.title.center_y - 50
)
# Makes the manager that contains the GUI button and enables it to the game.
self.manager = arcade.gui.UIManager()
self.manager.enable()
# Makes the play button.
gui_play_button = arcade.gui.UITextureButton(
x=150,
y=125,
width=100,
height=100,
texture=arcade.load_texture("images/GUI/restart_button_unhovered.png"),
texture_hovered=arcade.load_texture("images/GUI/restart_button_hovered.png"),
scale=button_scaling,
style=None
)
# Adds the play button to the manager.
self.manager.add(gui_play_button)
# Makes it to when the player presses the play button it starts the game.
gui_play_button.on_click = self.restart
def on_draw(self):
"""
Draw this view
"""
self.clear()
# Draws the game over title and the under title.
self.title.draw(pixelated=DRAW_PIXELATED)
self.subtitle.draw(pixelated=DRAW_PIXELATED)
# Draws the manager.
self.manager.draw()
def on_key_press(self, key: int, modifiers: int):
"""
Return to intro screen when any key is pressed.
"""
if key == arcade.key.SPACE:
intro_view = IntroView()
self.window.show_view(intro_view)
def restart(self, event):
"""
Return to intro screen when the restart button pressed
"""
intro_view = IntroView()
self.window.show_view(intro_view)
class LevelFinishView(arcade.View):
"""
View to show when the game is over
"""
def __init__(self, game_state:GameState):
"""
Create a Game Over-view. Pass the final score to display.
"""
self.game_state = game_state
# Set all movements to false so there's no auto-moving when the next level starts.
for p in self.game_state.players:
p.all_keys_off()
super().__init__(self.game_state.window)
def setup_old(self, score: int):
"""
Call this from the game so we can show the score.
"""
pass
def on_show_view(self):
"""
This is run once when we switch to this view
"""
# Set the background color
arcade.set_background_color(arcade.csscolor.BLACK)
def on_draw(self):
"""
Draw this view
"""
self.clear()
# Congratulations message!
arcade.draw_text(
"You beat the level!",
self.window.width / 2,
250,
arcade.color.TROLLEY_GREY,
font_size=35,
font_name=MAIN_FONT_NAME,
anchor_x="center",
bold=True
)
# Instructions message
arcade.draw_text(
"Press SPACE to continue your adventure..",
self.window.width / 2,
200,
arcade.color.TROLLEY_GREY,
font_size=15,
font_name=MAIN_FONT_NAME,
anchor_x="center",
bold=True
)
def on_key_press(self, key: int, modifiers: int):
"""
Go to the next map when SPACE key is pressed.
"""
if key == arcade.key.SPACE:
self.game_state.next_map()
v = GameView(self.game_state)
self.window.show_view(v)
def main():
"""
Main method
"""
# Create a window to hold views
window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT)
# Game starts in the intro view
start_view = IntroView()
window.show_view(start_view)
arcade.run()
if __name__ == "__main__":
main()