forked from berickcook/AIRIS_Public
-
Notifications
You must be signed in to change notification settings - Fork 1
/
puzzle_game_driver_custom.py
691 lines (583 loc) · 26.8 KB
/
puzzle_game_driver_custom.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
import pygame
import time, sys, csv
from pygame.locals import QUIT, KEYDOWN
from game_objects import *
from airis_stable import AIRIS
from constants import *
from other_useful_functions import pprint
import datetime
class PyGameView(object):
'''
PyGameView controls the display
'''
def __init__(self, model):
self.model = model
self.screen = pygame.display.set_mode(GAME_SCREEN_SIZE) # a pygame screen
self.surface = pygame.Surface(GAME_SCREEN_SIZE) # a pygame surface is the thing you draw on
self.show_view = True # toggle display
self.show_controls = False # toggle control display
self.mind_index_prev = 0
self.rep_map = []
w, h = GAME_MAP_GRID
for x in range(w):
self.rep_map.append([])
for y in range(h):
self.rep_map[x].append(0)
def draw(self):
# commented out because we're only updating squares if they change
# # fill background
# self.surface.fill(pygame.Color('black'))
self.draw_game_map()
self.draw_representation_map()
# draw control key
if self.show_controls:
for n, line in enumerate(PUZZLE_GAME_CONTROL_KEY):
self.draw_text(line, PUZZLE_GAME_CONTROL_KEY_START[0], \
PUZZLE_GAME_CONTROL_KEY_START[1]+14*n, 20)
# update display
pygame.display.update()
def draw_game_map(self):
# variable setup
w, h = GAME_MAP_GRID # number of positions wide and high
ms = GAME_MAP_START
# draw each position in the grid
for x in range(w):
for y in range(h):
if self.model.change_in_game_map[x][y]:
self.model.game_map[x][y].draw_game_image(self, x, y)
def draw_representation_map(self):
# variable setup
w, h = REP_MAP_GRID # number of positions wide and high
ms = REP_MAP_START
if not self.model.controller.bypass and self.model.airis.display_hold:
self.model.controller.mind_len = len(self.model.airis.display_plan)
if self.mind_index_prev != self.model.controller.mind_index:
print('Viewing plan step ' + str(self.model.controller.mind_index) + ' of ' + str(
self.model.controller.mind_len - 1) + ' (predicted result)')
self.mind_index_prev = self.model.controller.mind_index
# draw each position in the grid
try:
for x in range(w):
for y in range(h):
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 0:
self.rep_map[x][y] = self.model.floor
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 1:
self.rep_map[x][y] = self.model.character
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 2:
self.rep_map[x][y] = self.model.wall
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 3:
self.rep_map[x][y] = self.model.battery
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 4:
self.rep_map[x][y] = self.model.door
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 5:
self.rep_map[x][y] = self.model.key
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 6:
self.rep_map[x][y] = self.model.extinguisher
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 7:
self.rep_map[x][y] = self.model.fire
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 8:
self.rep_map[x][y] = self.model.right_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 9:
self.rep_map[x][y] = self.model.left_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 10:
self.rep_map[x][y] = self.model.down_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 11:
self.rep_map[x][y] = self.model.up_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 12:
self.rep_map[x][y] = self.model.open_door
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 13:
self.rep_map[x][y] = self.model.character_on_right_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 14:
self.rep_map[x][y] = self.model.character_on_left_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 15:
self.rep_map[x][y] = self.model.character_on_down_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 16:
self.rep_map[x][y] = self.model.character_on_up_arrow
if self.model.airis.models[self.model.airis.display_plan[self.model.controller.mind_index]].vis_env[x][y] == 17:
self.rep_map[x][y] = self.model.character_on_open_door
self.rep_map[x][y].draw_representation_image(self, x, y)
except:
pass
else:
for x in range(w):
for y in range(h):
if self.model.change_in_game_map[x][y]:
self.model.game_map[x][y].draw_representation_image(self, x, y)
def draw_text(self, text, x, y, size, color = (100, 100, 100)):
basicfont = pygame.font.SysFont(None, size)
text_render = basicfont.render(
text, True, color)
self.surface.blit(text_render, (x, y))
class Model(object):
'''
Model represents the state of all entities in
the environment and drawing parameters
'''
# this function initializes the model
def __init__(self, controller, airis_controlled):
'''
initialize model, environment, and default keyboard controller states
Args:
width (int): width of window in pixels
height (int): height of window in pixels
'''
# game setup parameters
self.show = True # show current model
self.controller = controller
# maze game setup
self.make_singletons()
self.current_maze = 0
self.get_next_maze()
self.player_action = "up"
# AGI setup
self.screen_input = []
for x in range(GAME_MAP_GRID[0]):
self.screen_input.append([])
for y in range(GAME_MAP_GRID[1]):
self.screen_input[x].append(0.0)
self.aux_input = [self.keys_collected, self.extinguishers_collected]
action_space = ['up', 'down', 'left', 'right', 'nothing']
#action_output_list = output range of each action [min, max, increment size]
action_output_list = [
[1, 2, 1],
[1, 2, 1],
[1, 2, 1],
[1, 2, 1],
[1, 2, 1]
]
self.airis = AIRIS(self.screen_input, self.aux_input, action_space, action_output_list)
self.airis_controlled = airis_controlled
if not airis_controlled:
self.airis.goal_type = 'Observe'
self.airis.goal_type_default = 'Observe'
self.time_counter = 0
# this function updates the model
def update(self):
pprint('-------------------------------------------- TIME STEP %d --------------------------------------------'
% self.time_counter, new_line_start=True, draw_line=False)
self.time_counter += 1
self.set_change_in_game_map(False)
# get user input
if not self.airis.display_hold or self.controller.bypass:
self.player_action = 'up'
if not self.airis_controlled:
self.player_action = self.get_action()
# input environment to airis
self.current_environment()
if self.airis_controlled:
self.player_action, _, _ = self.airis.capture_input(
self.screen_input,
self.aux_input,
self.player_action, prior=True)
else:
self.airis.capture_input(
self.screen_input,
self.aux_input,
self.player_action, prior=True)
pprint('ACTION:\t%s' % self.player_action, new_line_start=True, draw_line=False)
if self.airis.display_hold and not self.controller.bypass:
self.controller.paused = True
self.controller.mind_index = 0
print ('Awaiting plan approval...')
if not self.controller.paused or self.controller.bypass:
self.airis.display_hold = False
# update the model according to the player's input
self.game_logic(self.player_action)
# go to next level if player beats the current level
if self.batteries_collected == self.num_batteries:
self.get_next_maze()
# reset the maze if the character dies
if self.maze_reset:
self.get_next_maze()
# input environment to airis
self.current_environment()
self.airis.capture_input(
self.screen_input,
self.aux_input,
self.player_action, prior=False)
# user_input = input()
# sys.exit()
# these functions interact with AIRIS
def get_action(self):
return self.controller.player_input
def current_environment(self):
for x in range(GAME_MAP_GRID[0]):
for y in range(GAME_MAP_GRID[1]):
self.screen_input[x][y] = self.game_map[x][y].id
self.aux_input[0] = self.keys_collected
self.aux_input[1] = self.extinguishers_collected
# these functions controls game logic
def game_logic(self, player_input):
if player_input != 'nothing':
if player_input == 'up':
character_new_pos = \
(self.character_current_pos[0], \
self.character_current_pos[1] - 1)
if player_input == 'down':
character_new_pos = \
(self.character_current_pos[0], \
self.character_current_pos[1] + 1)
if player_input == 'left':
character_new_pos = \
(self.character_current_pos[0] - 1, \
self.character_current_pos[1])
if player_input == 'right':
character_new_pos = \
(self.character_current_pos[0] + 1, \
self.character_current_pos[1])
new_tile = self.game_map[character_new_pos[0]][character_new_pos[1]]
if isinstance(new_tile, Floor):
self.move_character(new_tile, character_new_pos, self.character)
elif isinstance(new_tile, Wall):
pass
elif isinstance(new_tile, Battery):
self.move_character(new_tile, character_new_pos, self.character)
self.collect_battery(character_new_pos)
elif isinstance(new_tile, RightArrow):
if player_input != 'left':
self.move_character(new_tile, \
character_new_pos, self.character_on_right_arrow)
elif isinstance(new_tile, LeftArrow):
if player_input != 'right':
self.move_character(new_tile, \
character_new_pos, self.character_on_left_arrow)
elif isinstance(new_tile, UpArrow):
if player_input != 'down':
self.move_character(new_tile, \
character_new_pos, self.character_on_up_arrow)
elif isinstance(new_tile, DownArrow):
if player_input != 'up':
self.move_character(new_tile, \
character_new_pos, self.character_on_down_arrow)
elif isinstance(new_tile, Fire):
if self.extinguishers_collected == 0:
self.reset_maze()
else:
self.move_character(new_tile, character_new_pos, self.character)
self.character_current_floor = self.floor
self.extinguishers_collected -= 1
elif isinstance(new_tile, Extinguisher):
self.move_character(new_tile, character_new_pos, self.character)
self.collect_extinguisher(character_new_pos)
elif isinstance(new_tile, Door):
if self.keys_collected == 0:
pass
else:
self.move_character(new_tile, \
character_new_pos, self.character_on_open_door)
self.keys_collected -= 1
elif isinstance(new_tile, OpenDoor):
self.move_character(new_tile, character_new_pos, self.character_on_open_door)
elif isinstance(new_tile, Key):
self.move_character(new_tile, character_new_pos, self.character)
self.collect_key(character_new_pos)
def move_character(self, new_tile, character_new_pos, character_and_floor):
character_old_floor = self.character_current_floor
character_old_pos = self.character_current_pos
if character_and_floor != self.character_on_open_door:
self.character_current_floor = new_tile
else:
self.character_current_floor = self.open_door
self.character_current_pos = character_new_pos
self.game_map\
[self.character_current_pos[0]] \
[self.character_current_pos[1]] \
= character_and_floor
self.change_in_game_map\
[self.character_current_pos[0]] \
[self.character_current_pos[1]] \
= True
self.game_map\
[character_old_pos[0]] \
[character_old_pos[1]] \
= character_old_floor
self.change_in_game_map\
[character_old_pos[0]] \
[character_old_pos[1]] \
= True
def collect_battery(self, character_new_pos):
self.batteries_collected += 1
self.character_current_floor = self.floor
def collect_extinguisher(self, character_new_pos):
self.extinguishers_collected += 1
self.character_current_floor = self.floor
def collect_key(self, character_new_pos):
self.keys_collected += 1
self.character_current_floor = self.floor
def reset_maze(self):
self.current_maze -= 1
self.maze_reset = True
# self.update()
# these functions initialize the maze levels
def load_maze(self):
game_map = self.floor_init()
with open('custom_levels/' + str(self.current_maze) + '.csv', 'r') as File:
read = csv.reader(File, delimiter=',')
for r, row in enumerate(read):
row[0] = int(row[0])
row[1] = int(row[1])
row[2] = int(row[2])
if r == 0:
self.character_start_pos = (row[0], row[1])
self.character_current_pos = (row[0], row[1])
self.character_current_floor = self.floor
self.num_batteries = row[2]
else:
if row[2] == 1:
game_map[row[0]][row[1]] = self.character
if row[2] == 2:
game_map[row[0]][row[1]] = self.wall
if row[2] == 3:
game_map[row[0]][row[1]] = self.battery
if row[2] == 4:
game_map[row[0]][row[1]] = self.door
if row[2] == 5:
game_map[row[0]][row[1]] = self.key
if row[2] == 6:
game_map[row[0]][row[1]] = self.extinguisher
if row[2] == 7:
game_map[row[0]][row[1]] = self.fire
if row[2] == 8:
game_map[row[0]][row[1]] = self.right_arrow
if row[2] == 9:
game_map[row[0]][row[1]] = self.left_arrow
if row[2] == 10:
game_map[row[0]][row[1]] = self.down_arrow
if row[2] == 11:
game_map[row[0]][row[1]] = self.up_arrow
self.game_map = game_map
def get_next_maze(self):
self.set_change_in_game_map(True)
self.current_maze += 1
try:
self.load_maze();
except FileNotFoundError:
self.current_maze = 1
try:
self.load_maze();
except FileNotFoundError:
print('ERROR: No levels to load!')
print('Use the included level editor to create levels and place them in /custom_levels')
print('They must be named 1.csv, 2.csv, 3.csv, ...')
interrupt = input()
raise Exception
self.batteries_collected = 0
self.extinguishers_collected = 0
self.keys_collected = 0
self.maze_reset = False
# these are helper functions for making maze levels
def floor_init(self):
# initialize everything to a floor tile
game_map = []
for x in range(GAME_MAP_GRID[0]):
game_map.append([])
for y in range(GAME_MAP_GRID[1]):
game_map[x].append(self.floor)
return game_map
def draw_wall_row(self, game_map, left, right, y):
for x in range(left, right+1):
game_map[x][y] = self.wall
return game_map
def draw_wall_col(self, game_map, bottom, top, x):
for y in range(bottom, top+1):
game_map[x][y] = self.wall
return game_map
def draw_fire_row(self, game_map, left, right, y):
for x in range(left, right+1):
game_map[x][y] = self.fire
return game_map
def draw_fire_col(self, game_map, bottom, top, x):
for y in range(bottom, top+1):
game_map[x][y] = self.fire
return game_map
def draw_arrow_row(self, game_map, left, right, y, type):
for x in range(left, right+1):
if type == 'r':
game_map[x][y] = self.right_arrow
if type == 'l':
game_map[x][y] = self.left_arrow
if type == 'u':
game_map[x][y] = self.up_arrow
if type == 'd':
game_map[x][y] = self.down_arrow
return game_map
def draw_arrow_col(self, game_map, bottom, top, x, type):
for y in range(bottom, top+1):
if type == 'r':
game_map[x][y] = self.right_arrow
if type == 'l':
game_map[x][y] = self.left_arrow
if type == 'u':
game_map[x][y] = self.up_arrow
if type == 'd':
game_map[x][y] = self.down_arrow
return game_map
def make_singletons(self):
self.character = Character()
self.floor = Floor()
self.wall = Wall()
self.fire = Fire()
self.extinguisher = Extinguisher()
self.battery = Battery()
self.key = Key()
self.door = Door()
self.open_door = OpenDoor()
self.right_arrow = RightArrow()
self.left_arrow = LeftArrow()
self.down_arrow = DownArrow()
self.up_arrow = UpArrow()
self.character_on_right_arrow = CharacterOnRightArrow()
self.character_on_left_arrow = CharacterOnLeftArrow()
self.character_on_down_arrow = CharacterOnDownArrow()
self.character_on_up_arrow = CharacterOnUpArrow()
self.character_on_open_door = CharacterOnOpenDoor()
# this function is used to render the game faster
# by only rendering the parts of the game that
# changed from the previous time step
def set_change_in_game_map(self, state):
self.change_in_game_map = []
for x in range(GAME_MAP_GRID[0]):
self.change_in_game_map.append([])
for y in range(GAME_MAP_GRID[1]):
self.change_in_game_map[x].append(state)
class PyGameKeyboardController(object):
'''
Keyboard controller that responds to keyboard input
'''
def __init__(self):
self.paused = False
self.player_input = 'nothing'
self.mind_index = 0
self.mind_action = ''
self.mind_len = 0
self.bypass = True
def handle_input(self):
is_there_input = False
for event in pygame.event.get():
if event.type == QUIT:
return False, is_there_input
else:
if event.type != KEYDOWN:
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
print ('mouse interrupt')
interrupt = input()
#print('mouse position = (%d,%d)') % (mouse_pos[0], mouse_pos[1])
if event.button == 4:
print('mouse wheel scroll up')
elif event.button == 5:
print('mouse wheel scroll down')
elif event.button == 1:
print('mouse left click')
elif event.button == 3:
print('mouse right click')
else:
print('event.button = %d' % event.button)
elif event.key == pygame.K_SPACE:
self.paused = not self.paused
print('Plan approved!')
elif event.key == pygame.K_k:
view.show_controls = not view.show_controls
elif event.key == pygame.K_v:
view.show_view = not view.show_view
keys = pygame.key.get_pressed() # checking pressed keys
original_player_input = self.player_input
number_of_keys_pressed = 0
if keys[pygame.K_UP]:
is_there_input = True
self.player_input = 'up'
number_of_keys_pressed += 1
if airis_controlled:
self.bypass = False
# print('up')
if keys[pygame.K_DOWN]:
is_there_input = True
self.player_input = 'down'
number_of_keys_pressed += 1
if airis_controlled:
self.bypass = True
# print('down')
if keys[pygame.K_LEFT]:
is_there_input = True
self.player_input = 'left'
number_of_keys_pressed += 1
if airis_controlled:
if self.mind_index > 0:
self.mind_index -= 1
# print('left')
if keys[pygame.K_RIGHT]:
is_there_input = True
self.player_input = 'right'
number_of_keys_pressed += 1
if airis_controlled:
if self.mind_index < self.mind_len-1:
self.mind_index += 1
# print('right')
if number_of_keys_pressed > 1:
# print('>1')
self.player_input = original_player_input
elif number_of_keys_pressed == 0:
self.player_input = 'nothing'
return True, is_there_input
if __name__ == '__main__':
# pygame setup
airis_controlled = True
pygame.init()
pygame.display.set_caption('Airis '+str(id(pygame)))
controller = PyGameKeyboardController()
model = Model(controller, airis_controlled)
if GAME_SHOW_SCREEN:
view = PyGameView(model)
# loop variable setup
running = True
# iterations = 0
first_update = True
# display the view initially
if GAME_SHOW_SCREEN and view.show_view:
view.draw()
view.screen.blit(view.surface, (0,0))
pygame.display.update()
# if airis_controlled:
# print ('Press any key to begin')
# interrupt = input()
while running:
# # output frame rate
# iterations += 1
# if time.time() - start_time > 1:
# start_time += 1
# print '%s fps' % iterations
# iterations = 0
# listen for user input
running, there_is_input = controller.handle_input()
# if theres user input
if model.airis_controlled or there_is_input or first_update:
there_is_input, first_update = False, False
# update the model
if not controller.paused:
model.update()
# display the view
if GAME_SHOW_SCREEN and view.show_view:
view.draw()
view.screen.blit(view.surface, (0,0))
pygame.display.update()
# reset user input
controller.player_input = 'nothing'
# update again if player won or died
if not controller.paused:
if model.batteries_collected == model.num_batteries or model.maze_reset:
model.update()
# display the view
if GAME_SHOW_SCREEN and view.show_view:
view.draw()
view.screen.blit(view.surface, (0,0))
pygame.display.update()
# THIS ONLY WORKS BECAUSE WHEN MODEL.UPDATE
# IS CALLED, WINNING OR DEATH WILL BE TRUE
# AND THE RETURN STATEMENT WILL KEEP THE
# REST OF A NORMAL UPDATE FROM HAPPENING
# therefore this is just a way to automatically
# load the next level or reload the current level
# on win or death without having to click anything
time.sleep(0.10) # control frame rate (in seconds)
pygame.quit()
sys.exit()