-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
267 lines (256 loc) · 12.7 KB
/
draw.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
#!/usr/bin/env python3
"""
3次元描画
"""
import sys
from PIL import Image, ImageDraw
import const
class Draw3D:
""" 3次元描画クラス """
# 地図の参照順序
# 左端→左側→右端→右側→真ん中 の順
MAP_ORDER = list(range(const.VIEW)) \
+ [i for i in range(const.VIEW * 2, const.VIEW - 1, -1)]
def __init__(self):
""" コンストラクタ """
pass
def draw(self, obj_maze, x, y, direction):
"""
描画メソッド
引数:
obj_maze : 迷路オブジェクト
x, y : プレイヤーの位置
direction : プレイヤーの向き
"""
# 描画の初期化
img = Image.new('RGB', (const.WIDTH, const.HEIGHT), color=0)
draw = ImageDraw.Draw(img)
# 床、天井の描画
self.draw_updown(draw)
# 可視範囲の地図情報取得
map_view = self.get_map_visible(obj_maze, x, y, direction)
# 壁の描画
self.draw_wall(draw, map_view, direction)
# 枠の描画
draw.rectangle(
(0, 0) + (const.WIDTH - 1, const.HEIGHT - 1),
outline=(255, 255, 255))
# 戻り値
return img
def get_map_visible(self, obj_maze, x, y, direction):
"""
可視範囲の地図情報取得
「手前→奥」「左→右」で取得
範囲外は反対側の場所
"""
maze_wall = obj_maze.maze_wall
maze_door = obj_maze.maze_door
maze_contents = obj_maze.maze_contents
# 可視範囲の情報
view_wall = []
view_door = []
view_contents = []
# 見た方向の地図情報を取得
depth_range = None
depth_mod_b = None
lr_range = None
lr_mod_b = None
if direction == const.DIR_EAST:
# 東
depth_range = range(x, x + const.VIEW + 1)
lr_range = range(y - const.VIEW, y + const.VIEW + 1)
elif direction == const.DIR_WEST:
# 西
depth_range = range(x, x - const.VIEW - 1, -1)
lr_range = range(y + const.VIEW, y - const.VIEW - 1, -1)
elif direction == const.DIR_SOUTH:
# 南
depth_range = range(y, y + const.VIEW + 1)
lr_range = range(x + const.VIEW, x - const.VIEW - 1, -1)
elif direction == const.DIR_NORTH:
# 北
depth_range = range(y, y - const.VIEW - 1, -1)
lr_range = range(x - const.VIEW, x + const.VIEW + 1)
# 情報取得
for depth in depth_range:
row_wall = []
row_door = []
row_contents = []
for lr in lr_range:
i = 0
j = 0
if (direction == const.DIR_NORTH
or direction == const.DIR_SOUTH):
i = lr % len(maze_wall[0])
j = depth % len(maze_wall)
elif (direction == const.DIR_EAST
or direction == const.DIR_WEST):
i = depth % len(maze_wall[0])
j = lr % len(maze_wall)
row_wall.append(maze_wall[j][i])
row_door.append(maze_door[j][i])
row_contents.append(maze_contents[j][i])
view_wall.append(row_wall)
view_door.append(row_door)
view_contents.append(row_contents)
# 戻り値
map_view = [view_wall, view_door, view_contents]
return map_view
def draw_wall(self, draw, map_view, direction):
""" 壁の描画 """
view_wall, view_door, view_contents = map_view
# 壁の色
COLOR_WALL = (255, 255, 255)
# 扉の色
COLOR_DOOR = (255, 255, 255)
# イベントの色
COLOR_EVENT = (255, 255, 255)
# 壁の有無により描画
for depth in range(const.VIEW, -1, -1):
for i in self.MAP_ORDER:
# 3Dの場所(視点(中心)からの距離)
d3_x_left = (i - const.VIEW) * 2 - 1
d3_x_right = (i - const.VIEW) * 2 + 1
d3_y = 1
d3_z = depth + 1
d3_z_here = depth
# イベント床の描画
if view_contents[depth][i] & const.EVENT_FLOOR > 0:
d2_x, d2_y = self.transform(d3_x_left + 0.3, d3_y, d3_z_here + 0.15)
xy = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_left + 0.3, d3_y, d3_z - 0.15)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_right - 0.3, d3_y, d3_z - 0.15)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_right - 0.3, d3_y, d3_z_here + 0.15)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
draw.polygon(xy, fill=COLOR_EVENT, outline=COLOR_EVENT)
# イベント天井の描画
if view_contents[depth][i] & const.EVENT_CEILING > 0:
d2_x, d2_y = self.transform(d3_x_left + 0.3, d3_y, d3_z_here + 0.15)
xy = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_left + 0.3, d3_y, d3_z - 0.15)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right - 0.3, d3_y, d3_z - 0.15)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right - 0.3, d3_y, d3_z_here + 0.15)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
draw.polygon(xy, fill=COLOR_EVENT, outline=COLOR_EVENT)
# 前面の壁&扉の描画
cell_judge = const.MAZE_NORTH << direction
if (view_wall[depth][i] & cell_judge > 0
or view_door[depth][i] & cell_judge > 0):
d2_x_left, d2_y_left = self.transform(d3_x_left, d3_y, d3_z)
d2_x_right, d2_y_right = self.transform(d3_x_right, d3_y, d3_z)
xy = self.xy_wall(
d2_x_left, d2_y_left,
d2_x_right, d2_y_right)
draw.polygon(xy, fill=0, outline=COLOR_WALL)
if view_door[depth][i] & cell_judge > 0:
d2_x, d2_y = self.transform(d3_x_left + 0.4, d3_y, d3_z)
xy = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_left + 0.4, d3_y - 0.4, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right - 0.4, d3_y - 0.4, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right - 0.4, d3_y, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
draw.polygon(xy, outline=COLOR_DOOR)
# 左の壁&扉の描画
cell_judge = const.MAZE_NORTH << ((direction - 1) % 4)
if i <= const.VIEW \
and (view_wall[depth][i] & cell_judge > 0
or view_door[depth][i] & cell_judge > 0):
d2_x_left, d2_y_left = self.transform(d3_x_left, d3_y, d3_z_here)
d2_x_right, d2_y_right = self.transform(d3_x_left, d3_y, d3_z)
xy = self.xy_wall(
d2_x_left, d2_y_left,
d2_x_right, d2_y_right)
draw.polygon(xy, fill=0, outline=COLOR_WALL)
if view_door[depth][i] & cell_judge > 0:
d2_x, d2_y = self.transform(d3_x_left, d3_y, d3_z_here + 0.2)
xy1 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_left, d3_y - 0.4, d3_z_here + 0.2)
xy2 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_left, d3_y - 0.4, d3_z - 0.2)
xy3 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_left, d3_y, d3_z - 0.2)
xy4 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
draw.line(xy1 + xy2, fill=COLOR_DOOR)
draw.line(xy2 + xy3, fill=COLOR_DOOR)
draw.line(xy3 + xy4, fill=COLOR_DOOR)
# 右の壁&扉の描画
cell_judge = const.MAZE_NORTH << ((direction + 1) % 4)
if i >= const.VIEW \
and (view_wall[depth][i] & cell_judge > 0
or view_door[depth][i] & cell_judge > 0):
d2_x_left, d2_y_left = self.transform(d3_x_right, d3_y, d3_z)
d2_x_right, d2_y_right = self.transform(d3_x_right, d3_y, d3_z_here)
xy = self.xy_wall(
d2_x_left, d2_y_left,
d2_x_right, d2_y_right)
draw.polygon(xy, fill=0, outline=COLOR_WALL)
if view_door[depth][i] & cell_judge > 0:
d2_x, d2_y = self.transform(d3_x_right, d3_y, d3_z_here + 0.2)
xy1 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y - 0.4, d3_z_here + 0.2)
xy2 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y - 0.4, d3_z - 0.2)
xy3 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y, d3_z - 0.2)
xy4 = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
draw.line(xy1 + xy2, fill=COLOR_DOOR)
draw.line(xy2 + xy3, fill=COLOR_DOOR)
draw.line(xy3 + xy4, fill=COLOR_DOOR)
def draw_updown(self, draw):
""" 床、天井の描画 """
BORDER_COLOR = (255, 255, 255)
for depth in range(0, const.VIEW + 1):
for i in range(- const.VIEW, const.VIEW + 1):
# 3Dの場所(視点(中心)からの距離)
d3_x_left = i * 2 - 1
d3_x_right = i * 2 + 1
d3_y = 1
d3_z = depth + 1
d3_z_here = depth
# 床
d2_x, d2_y = self.transform(d3_x_left, d3_y, d3_z_here)
xy = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_left, d3_y, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y, d3_z_here)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 + d2_y)
draw.polygon(xy, outline=BORDER_COLOR)
# 天井
d2_x, d2_y = self.transform(d3_x_left, d3_y, d3_z_here)
xy = (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_left, d3_y, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y, d3_z)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
d2_x, d2_y = self.transform(d3_x_right, d3_y, d3_z_here)
xy += (const.WIDTH / 2 + d2_x, const.HEIGHT / 2 - d2_y)
draw.polygon(xy, outline=BORDER_COLOR)
def transform(self, d3_x, d3_y, d3_z):
""" 透視投影変換 """
# 3D座標から2D座標に変換
if d3_z == 0:
# 計算後、描画枠からはみ出るような値
d3_z = 0.1
d2_x = d3_x / d3_z * const.FOV * const.WALL
d2_y = d3_y / d3_z * const.FOV * const.WALL
return d2_x, d2_y
def xy_wall(self, d2_x_left, d2_y_left, d2_x_right, d2_y_right):
""" 壁の描画座標を算出 """
# 各座標を指定
# 左下
xy = (const.WIDTH / 2 + d2_x_left, const.HEIGHT / 2 + d2_y_left)
# 左上
xy += (const.WIDTH / 2 + d2_x_left, const.HEIGHT / 2 - d2_y_left)
# 右上
xy += (const.WIDTH / 2 + d2_x_right, const.HEIGHT / 2 - d2_y_right)
# 右下
xy += (const.WIDTH / 2 + d2_x_right, const.HEIGHT / 2 + d2_y_right)
return xy