-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
95 lines (85 loc) · 3.16 KB
/
player.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
#!/usr/bin/env python3
"""
プレイヤー
"""
import sys
from PIL import Image, ImageDraw
import const
class Player:
""" プレイヤークラス """
name = ''
floor = 1
x = 0
y = 0
direction = const.DIR_SOUTH
# オートマッピング用(階層ごとに配列で用意)
mapping = None
def __init__(self, name, obj_maze=None):
""" コンストラクタ """
self.name = name
if obj_maze is not None:
map_floor = []
for j in range(obj_maze.height):
row = []
for i in range(obj_maze.width):
row.append(False)
map_floor.append(row)
self.mapping = [map_floor]
def auto_mapping(self):
""" オートマッピング """
# 次の階層
if len(self.mapping) < self.floor:
map_floor = []
for j in range(len(self.mapping[0])):
row = []
for i in range(len(self.mapping[0][0])):
row.append(False)
map_floor.append(row)
self.mapping.append(map_floor)
# 通った場所に印を付ける
self.mapping[self.floor - 1][self.y][self.x] = True
def move_forward(self, cell_wall):
""" 前方へ移動 """
# ※外枠判定はこのメソッド外で処理すること
if self.direction == const.DIR_NORTH:
if cell_wall & const.MAZE_NORTH == const.MAZE_NONE:
self.y -= 1
elif self.direction == const.DIR_SOUTH:
if cell_wall & const.MAZE_SOUTH == const.MAZE_NONE:
self.y += 1
elif self.direction == const.DIR_WEST:
if cell_wall & const.MAZE_WEST == const.MAZE_NONE:
self.x -= 1
elif self.direction == const.DIR_EAST:
if cell_wall & const.MAZE_EAST == const.MAZE_NONE:
self.x += 1
def turn_left(self):
""" 左に向く """
if self.direction == const.DIR_NORTH:
self.direction = const.DIR_WEST
elif self.direction == const.DIR_EAST:
self.direction = const.DIR_NORTH
elif self.direction == const.DIR_SOUTH:
self.direction = const.DIR_EAST
elif self.direction == const.DIR_WEST:
self.direction = const.DIR_SOUTH
def turn_right(self):
""" 右に向く """
if self.direction == const.DIR_NORTH:
self.direction = const.DIR_EAST
elif self.direction == const.DIR_EAST:
self.direction = const.DIR_SOUTH
elif self.direction == const.DIR_SOUTH:
self.direction = const.DIR_WEST
elif self.direction == const.DIR_WEST:
self.direction = const.DIR_NORTH
def turn_around(self):
""" 振り向く """
if self.direction == const.DIR_NORTH:
self.direction = const.DIR_SOUTH
elif self.direction == const.DIR_EAST:
self.direction = const.DIR_WEST
elif self.direction == const.DIR_SOUTH:
self.direction = const.DIR_NORTH
elif self.direction == const.DIR_WEST:
self.direction = const.DIR_EAST