forked from fogleman/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (61 loc) · 1.62 KB
/
main.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
from importlib import import_module
import os
import pg
import sys
def find_examples():
result = []
for filename in os.listdir('examples'):
name, ext = os.path.splitext(filename)
if ext == '.py' and name != '__init__':
result.append(name)
return result
EXAMPLES = find_examples()
def get_argument_example():
args = sys.argv[1:]
if len(args) != 1:
return None
arg = args[0]
if arg in EXAMPLES:
return arg
try:
return EXAMPLES[int(arg) - 1]
except Exception:
return None
def get_menu_example():
print 'Select an example to run:'
for index, name in enumerate(EXAMPLES):
print '%3d. %s' % (index + 1, name)
try:
return EXAMPLES[int(raw_input('> ')) - 1]
except Exception:
return None
def generate_screenshots(size):
try:
os.mkdir('screenshots')
except Exception:
pass
app = pg.App()
for name in EXAMPLES:
module = import_module('examples.%s' % name)
window = module.Window(size)
app.tick()
app.tick()
window.save_image('screenshots/%s.png' % name)
window.close()
app.run()
def main():
name = get_argument_example() or get_menu_example()
if name is None:
return
module = import_module('examples.%s' % name)
if hasattr(module, 'main'):
module.main()
else:
names = ['Window', 'Scene']
for name in names:
if hasattr(module, name):
pg.run(getattr(module, name))
return
if __name__ == '__main__':
# generate_screenshots((800, 600))
main()