This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviewer.py
340 lines (273 loc) · 10.4 KB
/
viewer.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Viewer application."""
import argparse
import asyncio
import json
import logging
import os
import random
import websockets
import pygame
from consts import RANKS, Tiles
from mapa import Map
from game import reduce_score
logging.basicConfig(level=logging.DEBUG)
logger_websockets = logging.getLogger("websockets")
logger_websockets.setLevel(logging.WARN)
logger = logging.getLogger("Map")
logger.setLevel(logging.DEBUG)
KEEPER = {
"up": (3 * 64, 4 * 64),
"left": (3 * 64, 6 * 64),
"down": (0, 4 * 64),
"right": (0, 6 * 64),
}
BOX = (7 * 64, 0)
BOX_ON_GOAL = (9 * 64, 0)
GOAL = (12 * 64, 5 * 64)
WALL = (8 * 64, 6 * 64)
PASSAGE = (12 * 64, 6 * 64)
CHAR_LENGTH = 64
CHAR_SIZE = CHAR_LENGTH, CHAR_LENGTH
SCALE = 1
COLORS = {
"white": (255, 255, 255),
"red": (255, 0, 0),
"pink": (255, 105, 180),
"blue": (135, 206, 235),
"orange": (255, 165, 0),
"yellow": (255, 255, 0),
"grey": (120, 120, 120),
}
SPRITES = None
SCREEN = None
async def messages_handler(websocket_path, queue):
"""Handles server side messages, putting them into a queue."""
async with websockets.connect(websocket_path) as websocket:
await websocket.send(json.dumps({"cmd": "join"}))
while True:
update = await websocket.recv()
queue.put_nowait(update)
class Artifact(pygame.sprite.Sprite):
"""Representation of moving pieces."""
def __init__(self, *args, **kw):
x, y = kw.pop("pos", ((kw.pop("x", 0), kw.pop("y", 0))))
new_pos = scale((x, y))
self.x, self.y = new_pos[0], new_pos[1]
self.image = pygame.Surface(CHAR_SIZE)
self.rect = pygame.Rect(new_pos + CHAR_SIZE)
self.update((x, y))
super().__init__(*args, **kw)
def update(self, pos=None):
"""Updates the sprite with a new position."""
if not pos:
pos = self.x, self.y
else:
pos = scale(pos)
self.rect = pygame.Rect(pos + CHAR_SIZE)
self.image.fill((0, 0, 230))
self.image.blit(SPRITES, (0, 0), (*PASSAGE, *scale((1, 1))))
self.image.blit(*self.sprite)
# self.image = pygame.transform.scale(self.image, scale((1, 1)))
self.x, self.y = pos
class Keeper(Artifact):
"""Handles Keeper Sprites."""
def __init__(self, *args, **kw):
self.direction = "left"
self.sprite = (SPRITES, (0, 0), (*KEEPER[self.direction], *scale((1, 1))))
super().__init__(*args, **kw)
def update(self, pos=None):
x, y = scale(pos)
if x > self.x:
self.direction = "right"
if x < self.x:
self.direction = "left"
if y > self.y:
self.direction = "down"
if y < self.y:
self.direction = "up"
self.sprite = (SPRITES, (0, 0), (*KEEPER[self.direction], *scale((1, 1))))
super().update(tuple(pos))
class Box(Artifact):
"""Handles Box Sprites."""
def __init__(self, *args, **kw):
self.sprite = (SPRITES, (0, 0), (*BOX, *scale((1, 1))))
if kw.pop("stored"):
self.sprite = (SPRITES, (0, 0), (*BOX_ON_GOAL, *scale((1, 1))))
super().__init__(*args, **kw)
def clear_callback(surf, rect):
"""Beneath everything there is a passage."""
surf.blit(SPRITES, (rect.x, rect.y), (*PASSAGE, rect.width, rect.height))
def scale(pos):
"""Scale positions according to gfx."""
x, y = pos
return int(x * CHAR_LENGTH / SCALE), int(y * CHAR_LENGTH / SCALE)
def draw_background(mapa):
"""Create background surface."""
background = pygame.Surface(scale(mapa.size))
for x in range(mapa.size[0]):
for y in range(mapa.size[1]):
wx, wy = scale((x, y))
background.blit(SPRITES, (wx, wy), (*PASSAGE, *scale((1, 1))))
if mapa.get_tile((x, y)) == Tiles.WALL:
background.blit(SPRITES, (wx, wy), (*WALL, *scale((1, 1))))
if mapa.get_tile((x, y)) in [Tiles.GOAL, Tiles.BOX_ON_GOAL, Tiles.MAN_ON_GOAL]:
background.blit(SPRITES, (wx, wy), (*GOAL, *scale((1, 1))))
return background
def draw_info(surface, text, pos, color=(0, 0, 0), background=None):
"""Creates text based surfaces for information display."""
myfont = pygame.font.Font(None, int(24 / SCALE))
textsurface = myfont.render(text, True, color, background)
x, y = pos
if x > surface.get_width():
pos = surface.get_width() - (textsurface.get_width() + 10), y
if y > surface.get_height():
pos = x, surface.get_height() - textsurface.get_height()
if background:
surface.blit(background, pos)
else:
erase = pygame.Surface(textsurface.get_size())
erase.fill(COLORS["grey"])
surface.blit(textsurface, pos)
return textsurface.get_width(), textsurface.get_height()
async def main_loop(queue):
"""Processes events from server and display's."""
global SPRITES, SCREEN
main_group = pygame.sprite.LayeredUpdates()
boxes_group = pygame.sprite.OrderedUpdates()
logging.info("Waiting for map information from server")
state = await queue.get() # first state message includes map information
logging.debug("Initial game status: %s", state)
newgame_json = json.loads(state)
GAME_SPEED = newgame_json["fps"]
try:
mapa = Map(newgame_json["map"])
except (KeyError, FileNotFoundError):
mapa = Map("levels/1.xsb") # Fallback to initial map
SCREEN = pygame.display.set_mode(scale(mapa.size))
SPRITES = pygame.image.load("data/sokoban.png").convert_alpha()
BACKGROUND = draw_background(mapa)
SCREEN.blit(BACKGROUND, (0, 0))
main_group.add(Keeper(pos=mapa.keeper))
state = {
"score": 0,
"player": "player1",
"keeper": mapa.keeper,
"boxes": mapa.boxes,
}
new_event = True
while True:
SCREEN.blit(BACKGROUND, (0, 0))
pygame.event.pump()
if pygame.key.get_pressed()[pygame.K_ESCAPE]:
asyncio.get_event_loop().stop()
main_group.clear(SCREEN, clear_callback)
boxes_group.clear(SCREEN, clear_callback)
if "score" in state and "player" in state:
text = f"m, p, s: {state['score']}"
draw_info(SCREEN, text.zfill(6), (5, 1))
text = str(state["player"]).rjust(32)
draw_info(SCREEN, text, (4000, 1))
if "level" in state:
w, _ = draw_info(SCREEN, "level: ", (SCREEN.get_width() / 2, 1))
draw_info(
SCREEN,
f"{state['level']}",
(SCREEN.get_width() / 2 + w, 1),
color=(200, 20, 20),
)
if "boxes" in state:
boxes_group.empty()
for box in state["boxes"]:
boxes_group.add(
Box(
pos=box,
stored=mapa.get_tile(box) in [Tiles.GOAL, Tiles.BOX_ON_GOAL],
)
)
boxes_group.draw(SCREEN)
main_group.draw(SCREEN)
# Highscores Board
if "highscores" in state and "player" in state:
if new_event:
highscores = state["highscores"]
highscores.append(
(f"<{state['player']}>", reduce_score(*state["score"]))
)
highscores = sorted(highscores, key=lambda s: s[1])
highscores = highscores[: len(RANKS)]
HIGHSCORES = pygame.Surface((256, 280))
HIGHSCORES.fill((30, 30, 30))
COLS = [20, 80, 150]
draw_info(HIGHSCORES, "THE 10 BEST PLAYERS", (20, 10), COLORS["white"])
for value, column in zip(["RANK", "SCORE", "NAME"], COLS):
draw_info(HIGHSCORES, value, (column, 30), COLORS["orange"])
for i, highscore in enumerate(highscores):
color = (
random.randrange(66, 222),
random.randrange(66, 222),
random.randrange(66, 222),
)
for value, column in zip(
[RANKS[i + 1], str(highscore[1]), highscore[0]], COLS
):
draw_info(HIGHSCORES, value, (column, 60 + i * 20), color)
SCREEN.blit(
HIGHSCORES,
(
(SCREEN.get_width() - HIGHSCORES.get_width()) / 2,
(SCREEN.get_height() - HIGHSCORES.get_height()) / 2,
),
)
if "keeper" in state:
main_group.update(state["keeper"])
pygame.display.flip()
try:
state = json.loads(queue.get_nowait())
new_event = True
if "map" in state:
logger.debug("New Level!")
# New level! lets clean everything up!
try:
mapa = Map(state["map"])
except FileNotFoundError:
logger.error(
"Can't find levels/%s.xsb, means we have a WINNER!",
state["level"],
)
continue
SCREEN = pygame.display.set_mode(scale(mapa.size))
BACKGROUND = draw_background(mapa)
SCREEN.blit(BACKGROUND, (0, 0))
boxes_group.empty()
main_group.empty()
main_group.add(Keeper(pos=mapa.keeper))
pygame.display.flip()
except asyncio.queues.QueueEmpty:
await asyncio.sleep(1.0 / GAME_SPEED)
new_event = False
continue
if __name__ == "__main__":
SERVER = os.environ.get("SERVER", "localhost")
PORT = os.environ.get("PORT", "8000")
parser = argparse.ArgumentParser()
parser.add_argument("--server", help="IP address of the server", default=SERVER)
parser.add_argument(
"--scale", help="reduce size of window by x times", type=int, default=1
)
parser.add_argument("--port", help="TCP port", type=int, default=PORT)
arguments = parser.parse_args()
SCALE = arguments.scale
LOOP = asyncio.get_event_loop()
pygame.font.init()
q = asyncio.Queue()
PROGRAM_ICON = pygame.image.load("data/icon.png")
pygame.display.set_icon(PROGRAM_ICON)
ws_path = f"ws://{arguments.server}:{arguments.port}/viewer"
try:
LOOP.run_until_complete(
asyncio.gather(messages_handler(ws_path, q), main_loop(q))
)
except RuntimeError as err:
logger.error(err)
finally:
LOOP.stop()