-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonal_project.py
More file actions
280 lines (217 loc) · 10 KB
/
Copy pathpersonal_project.py
File metadata and controls
280 lines (217 loc) · 10 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
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
import pygame
import time
import constants, file_preparer, other_functions
pygame.font.init()
WINDOW = constants.WINDOW
assets = file_preparer.load_assets(constants.ASSET_INFO)
other_hitboxes = file_preparer.get_hitboxes(constants.ALL_HITBOX_COORDS)
#SOUNDTRACKS = file_preparer.e(constants.SOUNDTRACK_INFO)
def check_keys():
for event in pygame.event.get():
pass
keys_pressed = pygame.key.get_pressed()
return keys_pressed
def rect_with_coords(coord_set1, coord_set2):
coord_diff = (coord_set2[0]-coord_set1[0], coord_set2[1]-coord_set1[1])
rect = pygame.Rect(coord_set1, coord_diff)
return rect
def alter_map_offset(map_offset, char_direction, colliding_char_hitboxes, keys_pressed):
for index, key_combo in enumerate(constants.KEY_COMBOS):
if index not in colliding_char_hitboxes:
if keys_pressed[key_combo[0]] and keys_pressed[key_combo[1]]:
char_direction = constants.KEY_COMBOS[key_combo][0]
map_offset[0] += constants.KEY_COMBOS[key_combo][1]
map_offset[1] += constants.KEY_COMBOS[key_combo][2]
return map_offset, char_direction
def check_list_collision(hitbox_list1, hitbox_list2):
colliding_hitboxes = []
for index, hitbox in enumerate(hitbox_list1):
if hitbox.collidelistall(hitbox_list2):
colliding_hitboxes.append(index)
return colliding_hitboxes
def get_wall_hitbox(map_offset, saved_coords, saved_time, keys_pressed):
if keys_pressed[pygame.K_q] and saved_time + 0.5 < time.time():
rect_coords = (-map_offset[0], -map_offset[1])
if saved_coords == 0:
saved_coords = tuple(rect_coords)
rect = 0
else:
rect = rect_with_coords(saved_coords, rect_coords)
saved_coords = 0
saved_time = time.time()
else:
rect = 0
return rect, saved_coords, saved_time
def get_point_coords(map_offset, saved_time):
if saved_time + 0.5 < time.time():
point_coords = (-map_offset[0], -map_offset[1])
saved_time = time.time()
return point_coords, saved_time
return 0, saved_time
def get_non_permeable_hitboxes(objects):
non_permeable_hitboxes = []
for object in objects:
if not object.is_permeable:
non_permeable_hitboxes.append(object.hitbox)
return non_permeable_hitboxes
def get_hitboxes_from_list(object_list):
hitboxes = []
for object in object_list:
hitboxes.append(object.hitbox)
return hitboxes
def collect_items(assets, detection_hitbox, items_collected, keys_pressed, saved_time):
for index, asset in enumerate(assets):
if asset.check_collision(detection_hitbox) and asset.is_collectable:
if keys_pressed[pygame.K_SPACE]:
asset.status_var = True
asset.orig_hitbox.topleft = (1000000,1000000)
saved_time = time.time()
items_collected.append(index)
return items_collected, saved_time
def handle_mechanisms(other_objects, detection_hitbox, keys_pressed):
for switch in other_objects[0]:
if keys_pressed[pygame.K_SPACE]:
switch.flip(detection_hitbox, other_objects[1])
for door in other_objects[1]:
door.reverse()
def get_touching_asset_index(other_objects, detection_hitbox):
for index, switch in enumerate(other_objects[0]):
if switch.asset.check_collision(detection_hitbox):
return index
for index, door in enumerate(other_objects[1]):
if door.asset.check_collision(detection_hitbox):
return index
return "lol"
def take_damage(hitboxes, char_health):
for hitbox in hitboxes:
if hitbox.check_collision(hitboxes[5].hitbox):
char_health -= hitbox.dmg_amount
if char_health > 1000:
char_health = 1000
elif char_health < 0:
char_health = 0
return char_health
def handle_health_system(assets, other_hitboxes, char_health):
char_health = take_damage(other_hitboxes, char_health)
hearts = int(char_health/200)
for count in range(hearts):
heart_spacing = count * 100 + 100
WINDOW.blit(assets[-3].image, (constants.WIDTH - heart_spacing, constants.HEIGHT - 100))
return char_health
def handle_collected_items(assets, words, items_collected):
collectable_info = constants.COLLECTABLE_INFO
for item in items_collected:
asset = assets[item]
coords = collectable_info[asset.name][0]
words[collectable_info[asset.name][1]].is_visible = True
asset.orig_hitbox.topleft = (coords)
def open_collectable_doors(assets, doors, items_collected):
major_collectable_num = 0
minor_collectable_num = 0
omega_collectable_num = 0
for item_index in items_collected:
if item_index in constants.MAJOR_COLLECTABLE_ASSET_INDICES:
major_collectable_num += 1
if item_index in constants.MINOR_COLLECTABLE_ASSET_INDICES:
minor_collectable_num += 1
if item_index in constants.OMEGA_COLLECTABLE_ASSET_INDICES:
omega_collectable_num += 1
if major_collectable_num == 3:
doors[13].state = True
if minor_collectable_num == 10:
doors[59].state = True
doors[39].state = True
if omega_collectable_num == 1:
assets[14].is_movable = False
def handle_projectiles(projectiles, collidable_hitboxes, char_health):
for projectile in projectiles:
projectile.update_position()
if projectile.check_all_collision(collidable_hitboxes):
if projectile.asset.check_collision(collidable_hitboxes[-1]):
char_health -= projectile.dmg_amount
projectile.reset_position()
return char_health
def modify_assets(assets, other_objects, other_hitboxes, items_collected, char_health, keys_pressed, saved_time):
if saved_time + 0.5 < time.time():
items_collected, saved_time = collect_items(assets, other_hitboxes[4].hitbox, items_collected, keys_pressed, saved_time)
handle_collected_items(assets, other_objects[3], items_collected)
char_health = handle_projectiles(other_objects[2], get_non_permeable_hitboxes(assets + other_hitboxes) + [other_hitboxes[5].hitbox], char_health)
handle_mechanisms(other_objects, other_hitboxes[4].hitbox, keys_pressed)
open_collectable_doors(assets, other_objects[1], items_collected)
return items_collected, char_health, saved_time
def update_all_positions(assets, other_hitboxes, map_offset):
WINDOW.fill(constants.BLACK)
for object in assets + other_hitboxes:
object.update_position(map_offset)
for asset in assets:
asset.blit(WINDOW)
pygame.draw.rect(WINDOW, constants.BLACK, other_hitboxes[-1].hitbox)
for hitbox in other_hitboxes:
break
#other_functions.draw_rect_list(WINDOW, [all_hitboxes[1]], constants.RED)
#draw_rect_list(DIRECTION_HITBOXES, constants.GREEN)
pygame.draw.rect(WINDOW, constants.BLUE, hitbox.hitbox)
#pygame.draw.rect(WINDOW, constants.RED, other_hitboxes[5].hitbox)
# pygame.draw.rect(WINDOW, BLACK, (0,HEIGHT-HUD_HEIGHT,WIDTH,HUD_HEIGHT))
def draw_game_gui(assets, other_hitboxes, char_health):
char_health = handle_health_system(assets, other_hitboxes, char_health)
return char_health
def run_game(assets, other_hitboxes, clock):
running = True
map_offset = [0, -224]
char_direction = 180
char_health = 1000
saved_time = time.time()
times_taken = []
object_coords = []
wall_hitbox_list = []
asset_list_indices = []
other_objects = file_preparer.get_switches_and_doors(assets)
saved_coords = 0
items_collected = []
while running:
time1 = time.time()
keys_pressed = check_keys()
non_permeable_hitboxes = get_non_permeable_hitboxes(assets + other_hitboxes)
if keys_pressed[pygame.K_1] and keys_pressed[pygame.K_3] and keys_pressed[pygame.K_m] and not keys_pressed[pygame.K_2]:
colliding_char_hitboxes = []
else:
colliding_char_hitboxes = check_list_collision(get_hitboxes_from_list(other_hitboxes[:4]), non_permeable_hitboxes)
map_offset, char_direction = alter_map_offset(map_offset, char_direction, colliding_char_hitboxes, keys_pressed)
assets[-1].rotate(char_direction)
for word in other_objects[3]:
word.check_collision(other_hitboxes[4].hitbox)
word.update(map_offset)
items_collected, char_health, saved_time = modify_assets(assets, other_objects, other_hitboxes, items_collected, char_health, keys_pressed, saved_time)
update_all_positions(assets, other_hitboxes, map_offset)
char_health = draw_game_gui(assets, other_hitboxes, char_health)
#if keys_pressed[pygame.K_o]:
# point_coords, saved_time = get_point_coords(map_offset, saved_time)
# if point_coords != 0:
# object_coords.append(point_coords)
if keys_pressed[pygame.K_i]:
touching_asset_index = get_touching_asset_index(other_objects, other_hitboxes[4].hitbox)
if touching_asset_index != "lol" and saved_time + 0.3 < time.time():
asset_list_indices.append(touching_asset_index)
saved_time = time.time()
if keys_pressed[pygame.K_ESCAPE]:
running = False
if char_health <= 0:
map_offset = [0,0]
pygame.display.update()
wall_hitbox, saved_coords, saved_time = get_wall_hitbox(map_offset, saved_coords, saved_time, keys_pressed)
if wall_hitbox != 0:
wall_hitbox_list.append(wall_hitbox)
time2 = time.time()
times_taken.append(time2 - time1)
print(wall_hitbox_list)
print(items_collected)
print(asset_list_indices)
print(object_coords)
print("\nSPF = " + str(round(sum(times_taken)/len(times_taken), 7)) + "\n")
def main(assets, other_hitboxes):
clock = pygame.time.Clock()
pygame.display.set_caption("My Personal Project")
run_game(assets, other_hitboxes, clock)
pygame.quit()
main(assets, other_hitboxes)