-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
143 lines (100 loc) · 4.2 KB
/
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
import time
import pygame
from pygame import Vector2, Surface
from enums.movement_keys import movement_keys
from rooms.entrance_zone import EntranceZone
from rooms.surface_zone import SurfaceZone
from tickable.renderable.collidable.entities.living.players.player import Player, get_players
from tickable.renderable.collidable.walls.door import Door
from tickable.renderable.collidable.walls.wall import Wall
from tickable.renderable.display.health_bar import HealthBar
from tickable.tickable import Tickable
pygame.init()
def load_icon(window_name: str, icon_name: str):
pygame.display.set_caption(window_name)
icon = pygame.image.load(f'assets/{icon_name}.png')
pygame.display.set_icon(icon)
class Game:
def __init__(self):
self.screen: Surface = pygame.display.set_mode((1280, 720))
load_icon("Dungeon Crawler", "big-skeleton-face")
self.debug = True
self.clicked_positions: [Vector2, Vector2] = [None, None]
self.past = time.time()
self.dt = 0
self.height = self.screen.get_height()
self.width = self.screen.get_width()
self.bottom_left = Vector2(0, self.height)
self.bottom_right = Vector2(self.width, self.height)
self.top_left = Vector2(0, 0)
self.top_right = Vector2(self.width, 0)
self.center = Vector2(self.width / 2, self.height / 2)
self.current_room = None
self.entrance_zone = None
self.surface_zone = None
self.over = False
def init(self):
rizzler = Player((0, 0), "rizzler", 64, movement_keys["WASD"], pygame.K_LSHIFT, self)
player = Player((0, 0), "pro", 64, movement_keys["ARROW"], pygame.K_RSHIFT, self)
HealthBar(rizzler, (self.screen.get_width() - 100 - 300, self.screen.get_height() - 140), 300, 40, self)
HealthBar(player, (100, self.screen.get_height() - 140), 300, 40, self)
self.current_room = self.surface_zone = SurfaceZone(self)
self.entrance_zone = EntranceZone(self)
self.surface_zone.load()
players = get_players()
i = 0
for player in players:
i += 1
if i == 1:
player.position = self.current_room.spawn_1
else:
player.position = self.current_room.spawn_2
player.set_scale(self.current_room.entity_scale)
def tick(self):
if not self.over:
present = time.time()
self.dt = present - self.past
self.past = present
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
return False
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
self.screen.fill((0, 0, 0))
if self.over:
font_1 = pygame.font.Font(None, 150)
text_1 = font_1.render("Game Over!", True, (255, 0, 0))
text_1_rect = text_1.get_rect(center=self.center)
self.screen.blit(text_1, text_1_rect)
font_2 = pygame.font.Font(None, 50)
text_2 = font_2.render("Press ESC to exit", True, (200, 0, 0))
text_2_rect = text_2.get_rect(center=(self.center.x, self.center.y + 100))
self.screen.blit(text_2, text_2_rect)
if not self.over:
for tickable in Tickable.tickables:
tickable.tick()
pygame.display.flip()
return True
def handle_debug_mouse_click(self):
pos = pygame.mouse.get_pos()
print(f'POSITION SELECTED: ({pos[0]}, {pos[1]})')
print(f'skeleton = Skeleton(({pos[0]}, {pos[1]}), 32)')
debug_pos_1 = self.clicked_positions[0]
debug_pos_2 = self.clicked_positions[1]
if debug_pos_1 is None:
self.clicked_positions[0] = Vector2(pos)
return
if debug_pos_2 is None:
self.clicked_positions[1] = pos
debug_pos_2 = pos
print(debug_pos_1)
print(debug_pos_2)
wall = Wall(debug_pos_1, debug_pos_2, self)
print(wall.to_string())
self.clicked_positions = [None, None]
def end(self):
self.over = True
self.current_room.unload()
for tickable in Tickable.tickables:
tickable.unload()