-
Notifications
You must be signed in to change notification settings - Fork 3
/
gameexample.py
90 lines (77 loc) · 3.37 KB
/
gameexample.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
import sys
from curtsies.fmtfuncs import red, bold, green, on_blue, yellow, on_red
from curtsies.window import Window
from curtsies.terminal import Terminal
from curtsies.fsarray import FSArray
class Entity(object):
def __init__(self, display, x, y, speed=1):
self.display = display
self.x, self.y = x, y
self.speed = speed
def towards(self, entity):
dx = entity.x - self.x
dy = entity.y - self.y
desired_x = dx/abs(dx) * self.speed if dx else 0
desired_y = dy/abs(dy) * self.speed if dy else 0
return desired_x, desired_y
class World(object):
def __init__(self, width, height):
self.width = width
self.height = height
n = 10
self.player = Entity(on_blue(green(bold('5'))), width // 2, height // 2 - 2, speed=5)
self.npcs = [Entity(on_blue(red('X')), i * width // (n * 2), j * height // (n * 2)) for i in range(1, 2*n, 2) for j in range(1, 2*n, 2)]
self.turn = 0
entities = property(lambda self: self.npcs + [self.player])
def move_entity(self, entity, dx, dy):
entity.x = max(0, min(self.width-1, entity.x + dx))
entity.y = max(0, min(self.height-1, entity.y + dy))
def process_event(self, c):
if c == "":
sys.exit()
elif c in ('KEY_UP', 'KEY_LEFT', 'KEY_DOWN', 'KEY_RIGHT'):
self.move_entity(self.player, *{'KEY_UP':(0,self.player.speed),
'KEY_LEFT':(-self.player.speed, 0),
'KEY_DOWN':(0,-self.player.speed),
'KEY_RIGHT':(self.player.speed, 0)}[c])
else:
self.msg = Window.array_from_text_rc("try w, a, s, d, or ctrl-D", self.height, self.width)
return self.tick()
def tick(self):
for npc in self.npcs:
self.move_entity(npc, *npc.towards(self.player))
for entity1 in self.entities:
for entity2 in self.entities:
if entity1 is entity2: continue
if (entity1.x, entity1.y) == (entity2.x, entity2.y):
if entity1 is self.player:
return 'you lost on turn %d' % self.turn
entity1.speed = 0
entity2.speed = 0
entity1.display = on_red(bold(yellow('o')))
entity2.display = on_red(bold(yellow('o')))
if all(npc.speed == 0 for npc in self.npcs):
return 'you won on turn %d' % self.turn
self.turn += 1
if self.turn % 20 == 0:
self.player.speed = max(0, self.player.speed - 1)
self.player.display = on_blue(green(bold(str(self.player.speed))))
def get_array(self):
a = FSArray(self.height, self.width, bg='blue')
for entity in self.entities:
a[self.height - 1 - entity.y, entity.x] = entity.display
return a
def main():
with Terminal(sys.stdin, sys.stdout) as tc:
with Window(tc) as t:
rows, columns = t.tc.get_screen_size()
world = World(width=columns, height=rows) # note the row/column x/y swap!
while True:
t.render_to_terminal(world.get_array())
c = t.tc.get_event()
msg = world.process_event(c)
if msg:
break
print(msg)
if __name__ == '__main__':
main()