Skip to content

Commit 62db0a0

Browse files
committed
Add method to visualiza game state
1 parent a81ab61 commit 62db0a0

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

game_map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Map:
22

3-
def __init__(self, game_map):
4-
self.static = game_map
3+
def __init__(self, map_data):
4+
self.static = map_data
55
self.active = []
66
self.mock = []
77
self.special = {}

mapworks/visualization.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,52 @@
33
import matplotlib.pyplot as plt
44
import matplotlib.cm as cm
55
import numpy as np
6+
import itertools
7+
8+
9+
def view_game_state(game_map, view_mock=False, grid=True):
10+
"""
11+
Method shows current game state including static, active and mock points.
12+
Active points should have cur_pos, Mock objects should have a list of
13+
points, static is a 2D np array.
14+
15+
Note: Mock objects have low opacity (alpha=0.3)
16+
17+
Args:
18+
game_map (Map object)
19+
view_mock (Boolean): mock objects are shown if true
20+
"""
21+
# show active units
22+
unit_points = [game_unit.cur_pos for game_unit in game_map.active]
23+
x, y = get_x_and_y_from_itr(unit_points)
24+
colors = np.arange(len(unit_points))
25+
plt.scatter(x, y, c=colors)
26+
27+
# show mock units if specified
28+
if view_mock:
29+
mock_objects = [game_mock.points for game_mock in game_map.mock]
30+
31+
# each mock object is colored different but points for each object
32+
# are colored same
33+
colors = [len(obj) for obj in mock_objects]
34+
colors = [[i]*value for i, value in enumerate(colors)]
35+
colors = list(itertools.chain.from_iterable(colors))
36+
colors = np.array(colors)
37+
38+
mock_objects = itertools.chain.from_iterable(mock_objects)
39+
x, y = get_x_and_y_from_itr(mock_objects)
40+
plt.scatter(x, y, c=colors, alpha=0.3)
41+
42+
# show map with static passable and impassable terrain
43+
map_data = game_map.static
44+
m, n = map_data.shape
45+
plt.imshow(map_data)
46+
plt.xticks(np.arange(0.5, n, 1.0), [])
47+
plt.yticks(np.arange(-0.5, m, 1.0), [])
48+
plt.connect('button_press_event', mouse_move)
49+
plt.grid(grid)
50+
plt.show()
51+
return
652

753

854
def view_map(map_data, stores=None, paths=None, points=None, grid=True):

0 commit comments

Comments
 (0)