-
Notifications
You must be signed in to change notification settings - Fork 7
/
life-gui.py
executable file
·93 lines (75 loc) · 3.1 KB
/
life-gui.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
#!/usr/bin/env python
import life
import numpy as np
from sdl2 import *
import sdl2.ext
import argparse
import pyopencl as cl
import sys
life = life.life(interactive=True)
rulesets = [ 'conway', 'conway_fading', 'quad', 'quad_fading', 'quad2', 'rule101' ]
def get_ruleset(l, s):
return (getattr(l, s + '_init'),
getattr(l, s + '_steps'),
getattr(l, s + '_render'),
getattr(l, s + '_uninit'))
def next_ruleset(l, s, d):
ruleset = rulesets[(rulesets.index(s) + d) % len(rulesets)]
return ruleset, get_ruleset(l, ruleset)
parser = argparse.ArgumentParser(description='The Game of Life!')
parser.add_argument('--variant', metavar='RULES', type=str, choices=rulesets, default='conway',
help='The variant of the game to run')
parser.add_argument('--width', metavar='INT', type=int, default=800,
help='Width of the world')
parser.add_argument('--height', metavar='INT', type=int, default=600,
help='Height of the world')
parser.add_argument('--steps', metavar='INT', type=int, default=1,
help='Number of simulation steps to perform per frame')
args = parser.parse_args()
height = args.height
width = args.width
steps=args.steps
SDL_Init(SDL_INIT_EVERYTHING)
window = SDL_CreateWindow(b"Game of Life",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_SHOWN)
def reWindow(window):
window_surface = SDL_GetWindowSurface(window)
frame_py = np.ndarray(shape=(height, width), dtype=np.uint32, order='C')
surface = SDL_CreateRGBSurfaceFrom(frame_py.ctypes.data, width, height, 32, width*4,
0xFF0000, 0xFF00, 0xFF, 0x00000000)
return (window_surface, frame_py, surface)
(window_surface, frame_py, surface) = reWindow(window)
initworld = np.random.choice([True, False], size=(height, width))
ruleset = args.variant
life_init, life_steps, life_render, life_uninit = get_ruleset(life, ruleset)
life_state = life_init(initworld)
def render():
frame = life_render(life_state)
frame.get(ary=frame_py)
SDL_BlitSurface(surface, None, window_surface, None)
SDL_UpdateWindowSurface(window)
def switch_rules(d):
global ruleset, life_state, life_init, life_steps, life_render, life_uninit
bools = life_uninit(life_state)
ruleset, (life_init, life_steps, life_render, life_uninit) = next_ruleset(life, ruleset, d)
life_state = life_init(bools)
running=True
while running:
life_state = life_steps(steps, life_state)
render()
events = sdl2.ext.get_events()
for event in events:
if event.type == SDL_QUIT:
running=False
if event.type == SDL_KEYDOWN:
key = event.key.keysym.sym
if key == SDLK_RIGHT:
switch_rules(1)
if key == SDLK_LEFT:
switch_rules(-1)
if key == SDLK_ESCAPE:
running=False
if key == SDLK_r:
life_state = life_init(np.random.choice([True, False],
size=(height, width)))