-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
132 lines (95 loc) Β· 2.76 KB
/
game.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
import random
import os
from snail import *
# Reactive entities
class Player(Entity):
x = Reactive()
y = Reactive()
def __init__(self, x=None, y=None, char='[]'):
super().__init__()
self.char = char
self.x = x
self.y = y
def down(self):
self.y = min(now(self.y) + 1, config.height)
def up(self):
self.y = max(now(self.y) - 1, 0)
def right(self):
self.x = min(now(self.x) + 1, config.width)
def left(self):
self.x = max(now(self.x) - 1, 0)
class Ball(Entity):
vx = Reactive()
vy = Reactive()
x = Reactive()
y = Reactive()
def __init__(self, char='ZZ'):
super().__init__()
self.char = char
def reverse_x(self):
self.vx = -1 * now(self.vx)
def reverse_y(self):
self.vy = -1 * now(self.vy)
# Some pure behavior helpers
def constrain_x(x):
return max(min(round(x), config.width), 0)
def constrain_y(y):
return max(min(round(y), config.height), 0)
def print_grid(state):
grid = [[char for _ in range(config.width+1)] for _ in range(config.height+1)]
for ball in state.balls:
x, y = constrain_x(now(ball.x)), constrain_y(now(ball.y))
grid[y][x] = ball.char
x, y = constrain_x(now(state.player.x)), constrain_y(now(state.player.y))
grid[y][x] = player.char
for row in grid:
print(''.join(row))
print('\n' * (term_height - config.height - 3))
def quit(done):
if done:
raise KeyboardInterrupt
# Setup code
sz = os.get_terminal_size()
term_height = sz.lines
term_width = sz.columns
char = '.`'
class Config:
width = 40
height = 30
class State:
player = None
balls = []
state = State()
config = Config()
behaviors = [
keyboard,
lift(state) >> lift(print_grid),
(keyboard == 'esc') >> quit
]
player = Player(x=config.width // 2, y=config.height // 2, char='π§π')
player.reactors = [
reactor(keyboard == 'down', player.down),
reactor(keyboard == 'up', player.up),
reactor(keyboard == 'left', player.left),
reactor(keyboard == 'right', player.right)
]
state.player = player
for _ in range(2000):
ball = Ball()
ball.vx = random.randint(5, 15)
ball.x = integral(ball.vx) + random.randint(0, config.width)
ball.vy = random.randint(5, 15)
ball.y = integral(ball.vy) + random.randint(0, config.height)
ball.reactors = [
reactor(ball.x >= config.width, ball.reverse_x),
reactor(ball.x < 0, ball.reverse_x),
reactor(ball.y >= config.height, ball.reverse_y),
reactor(ball.y < 0, ball.reverse_y),
]
state.balls.append(ball)
# Main reactive loop
engine = Engine(behaviors, state=state, tick_rate=0.03) # roughly 30fps
try:
engine.run()
except KeyboardInterrupt:
print('done')