forked from fisadev/zombsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
292 lines (237 loc) · 9.96 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# coding: utf-8
from __future__ import print_function
import os
import sys
from serial import Serial
import time
from termcolor import colored
from core import World
from things import Box, Wall, Zombie, ObjetiveLocation, Player
def get_creator(module_name):
'''Get the create() function from a module.'''
module = __import__(module_name, fromlist=['create', ])
create_function = getattr(module, 'create')
return create_function
def create_player(name, rules_name, objetives):
creator = get_creator('players.' + name)
return creator(rules_name, objetives)
def create_rules(name, game):
creator = get_creator('rules.' + name)
return creator(game)
class Rules(object):
'''Rules to decide when a game ends, and when it's won.'''
def __init__(self, game):
self.game = game
def players_alive(self):
'''Are there any alive players?'''
for player in self.game.players:
if player.life > 0:
return True
return False
def game_ended(self):
'''Has the game ended?'''
return not self.players_alive()
def game_won(self):
'''Was the game won?'''
if self.players_alive():
# never should happen, but ilustrative
return True, u'you won a game that never ends (?!)'
else:
return False, u'everybody is dead :('
class Map(object):
'''A map for a world.'''
def __init__(self, size, things, player_spawns=None, zombie_spawns=None,
objetives=None):
self.size = size
self.things = things
self.player_spawns = player_spawns
self.zombie_spawns = zombie_spawns
self.objetives = objetives
@classmethod
def from_file(cls, file_path):
'''Import data from a utf-8 map file.'''
zombie_spawns = []
player_spawns = []
objetives = []
things = []
max_row = 0
max_col = 0
# read the file
encoding = 'utf-8'
if sys.version_info > (3,):
with open(file_path, encoding=encoding) as map_file:
lines = map_file.read().split('\n')
else:
with open(file_path) as map_file:
lines = map_file.read().decode(encoding).split('\n')
# for each char, create the corresponding object
for row_index, line in enumerate(lines):
max_row = row_index
for col_index, char in enumerate(line):
if char:
max_col = max(col_index, max_col)
position = (col_index, row_index)
if char in (Box.ICON, 'b', 'B'):
things.append(Box(position))
elif char in (Wall.ICON, 'w', 'W'):
things.append(Wall(position))
elif char.lower() == 'p':
player_spawns.append(position)
elif char.lower() == 'z':
zombie_spawns.append(position)
elif char.lower() == 'o':
objetives.append(position)
things.append(ObjetiveLocation(position))
return Map((max_col, max_row),
things,
player_spawns,
zombie_spawns,
objetives)
class Game(object):
'''An instance of game controls the flow of the game.
This includes player and zombies spawning, game main loop, deciding when
to stop, importing map data, drawing each update, etc.
'''
def __init__(self, rules_name, player_names, map_, initial_zombies=0,
minimum_zombies=0, docker_isolator=False, debug=False,
isolator_port=8000, use_basic_icons=False, use_arduino=False,
arduino_device='/dev/ttyACM0', arduino_bauds=9600):
self.players = []
self.rules_name = rules_name
self.rules = get_creator('rules.' + rules_name)(self)
self.map = map_
self.minimum_zombies = minimum_zombies
self.docker_isolator = docker_isolator
self.isolator_port = isolator_port
self.debug = debug
self.use_basic_icons = use_basic_icons
self.use_arduino = use_arduino
self.arduino_device = arduino_device
self.arduino_bauds = arduino_bauds
self.world = World(self.map.size, debug=debug)
for thing in self.map.things:
self.world.spawn_thing(thing)
if docker_isolator:
from isolation.players_client import create_player_client
self.players = [create_player_client(name, rules_name,
self.map.objetives,
self.isolator_port)
for name in player_names]
else:
self.players = [create_player(name, rules_name,
self.map.objetives)
for name in player_names]
self.spawn_players()
self.spawn_zombies(initial_zombies)
if self.use_arduino:
self.arduino_serial = Serial(self.arduino_device,
self.arduino_bauds)
def spawn_players(self):
'''Spawn players using the provided player create functinons.'''
self.world.spawn_in_random(self.players, self.map.player_spawns)
def spawn_zombies(self, count):
'''Spawn N zombies in the world.'''
zombies = [Zombie() for i in range(count)]
self.world.spawn_in_random(zombies,
self.map.zombie_spawns,
fail_if_cant=False)
def position_draw(self, position):
'''Get the string to draw for a given position of the world.'''
# decorations first, then things over them
thing = self.world.things.get(position) or \
self.world.decoration.get(position)
if thing is not None:
if self.use_basic_icons:
icon = thing.icon_basic
else:
icon = thing.icon
return colored(icon, thing.color)
else:
return u' '
def play(self, frames_per_second=2.0):
'''Game main loop, ending in a game result with description.'''
while True:
self.world.step()
# mantain the flow of zombies if necessary
zombies = [thing for thing in self.world.things.values()
if isinstance(thing, Zombie)]
if len(zombies) < self.minimum_zombies:
self.spawn_zombies(self.minimum_zombies - len(zombies))
self.draw()
if self.debug:
if sys.version_info > (3,):
input()
else:
raw_input()
else:
time.sleep(1.0 / frames_per_second)
if self.rules.game_ended():
won, description = self.rules.game_won()
if self.use_arduino:
if won:
self.arduino('g', True) # "gwin!!"
else:
self.arduino('l', True) # lose
print('')
if won:
print(colored(u'WIN! ', 'green'))
else:
print(colored(u'GAME OVER ', 'red'))
print(description)
return won, description
def arduino(self, data, add_end_chars=False):
'''Send an order to the arduino screen.'''
if add_end_chars:
data = data + chr(1) * 2
self.arduino_serial.write(data)
def draw(self):
'''Draw the world.'''
screen = ''
# print the world
screen += '\n'.join(u''.join(self.position_draw((x, y))
for x in range(self.world.size[0]))
for y in range(self.world.size[1]))
# game stats
screen += '\nticks: %i deaths: %i' % (self.world.t, self.world.deaths)
# print player stats
players = sorted(self.players, key=lambda x: x.name)
for player in players:
try:
weapon_name = player.weapon.name
except:
weapon_name = u'unarmed'
if player.life > 0:
# a small "health bar" with unicode chars, from 0 to 10 chars
life_chars_count = int((10.0 / player.MAX_LIFE) * player.life)
life_chars = life_chars_count * u'\u2588'
no_life_chars = (10 - life_chars_count) * u'\u2591'
life_bar = u'\u2665 %s%s' % (life_chars, no_life_chars)
else:
life_bar = u'\u2620 [dead]'
player_stats = u'%s %s <%i %s %s>: %s' % (life_bar,
player.name,
player.life,
str(player.position),
weapon_name,
player.status or u'-')
screen += '\n' + colored(player_stats, player.color)
# print events (of last step) for debugging
if self.debug:
screen += u'\n'
screen += u'\n'.join([colored(u'%s: %s' % (thing.name, event),
thing.color)
for t, thing, event in self.world.events
if t == self.world.t])
os.system('clear')
print(screen)
# if using arduino screen, send data
if self.use_arduino:
for thing in self.world.things.values():
if isinstance(thing, Player):
icon = 'p'
else:
icon = thing.__class__.__name__[0].lower()
self.arduino(icon + \
chr(thing.position[0]) + \
chr(thing.position[1]))
self.arduino('r', True)