-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
92 lines (72 loc) · 2.76 KB
/
common.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
#!/usr/bin/env python2
import pygame # this level was removed from Donkey Kong for its NES release
import pickle # a sea cucumber left in brine
import os # generic brand cheerios
import sys # a nickname that refers to a female sybling
import time # an illusion
game_path = './games'
brain_path = './brains'
blacklist = ['skeleton_game', 'skeleton_solver']
sys.path.append(game_path)
sys.path.append(brain_path)
sys.path.append('./deps/python-retro') # hack
class UtilType:
def __init__(self):
pass
@staticmethod
def ListModules(path):
files = os.listdir(path)
for i in files:
if '.' in i:
name, ext = i.rsplit('.', 1)
if ext == 'py' and name not in blacklist:
yield name
def ListGames(self):
return self.ListModules(game_path)
def ListBrains(self):
return self.ListModules(brain_path)
@staticmethod
def GetArgs(modname):
mod = __import__(modname)
validators = {}
if hasattr(mod, 'validargs'):
validators = mod.validargs
return mod.defaultargs.copy(), validators.copy()
util = UtilType()
class Driver:
def __init__(self, game, brain, game_args, brain_args):
self.game = game(**game_args)
self.brain = brain(self.game, **brain_args)
self.game_args = game_args
self.brain_args = brain_args
pygame.display.init()
def Run(self):
running = True
print('Driver: Started at', time.asctime())
while running:
# let the pathfinder take a step, get screens to show throughout
for surf in self.brain.Step():
# process events
for event in pygame.event.get():
# pass events on to the pathfinder in case it takes input etc.
self.brain.Event(event)
if event.type == pygame.QUIT:
running = False
# if relevant, draw the screen
if surf is not None:
pygame.display.get_surface().blit(surf, (0, 0))
pygame.display.flip()
pygame.display.flip()
print('Driver: Finished at', time.asctime())
def Save(self, output, screenshot=None):
result = {
'game': self.game.name,
'game_args': self.game_args,
'brain': self.brain.name,
'brain_args': self.brain_args,
'path': self.brain.Path(),
'state': self.game.Freeze()
}
pickle.dump(result, open(output, 'wb'))
if screenshot is not None:
pygame.image.save(pygame.display.get_surface(), screenshot)