这段代码存在严重问题。让我们从头开始设计。这将有助于学习如何设计和构建类和数据结构。
首先,你应该围绕一个Map
类来组织你的代码,然后将房间表示为网格。你不应该考虑“房间1”,“房间2”等(这很难在地图上跟踪),而是应该根据坐标来考虑房间。
现在,有一些可能被忽略的功能,包括玩家只看到他去过的房间,玩家保持在地图中心,以及对角线路径。如果你想要它们,可以在基本功能正常工作后再加入它们。现在,我们的目标是实现类似于这样的东西:
[ ]-[u] [ ] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ]-[ ]
也就是说,我们将其表示为一个网格,其中有些房间相互连接,而其他房间则没有。让我们给每个房间都分配一个坐标对,就像这样:
0 1 2 3
0 [ ]-[u] [ ] [ ]
|
1 [ ]-[ ]-[ ] [ ]
|
2 [ ]-[ ]-[ ] [ ]
|
3 [ ]-[ ]-[ ]-[ ]
让 x 沿着顶部,y 沿着侧面。左上角为 (0, 0),带有
[u]
的格子为 (0, 1)。
现在,我们的
Map
类有哪些组成部分?
地图高度:整数
地图宽度:整数
player_x、player_y:玩家坐标
可能的路径:一组可以在房间之间移动的坐标对。以上地图可以表示为:
[((0, 0), (1, 0)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((1, 1), (2, 1)),
((1, 0), (1, 2)), ((0, 2), (1, 2)), ((1, 2), (2, 2)), ((0, 2), (0, 3)),
((0, 3), (1, 3)), ((1, 3), (2, 3)), ((2, 3), (3, 3))]
请注意,我按照较大的元组先进行排序(稍后会很重要)。
现在我们有了设计,让我们来编写Map
类!
我可以想到四种方法:print_map
、move
和初始化器。初始化很简单:只需设置我们上面列出的四个属性:
class Map:
def __init__(self, height, width, player_x, player_y, paths):
self.height = height
self.width = width
self.x = player_x
self.y = player_y
self.paths = paths
现在,move
非常简单。给定方向n/e/s/w:
def move(self, direction):
if direction == "n":
if ((self.x, self.y - 1), (self.x, self.y)) not in self.paths:
print "Cannot go north"
else:
self.y -= 1
"north"的move
函数只是检查我们所在房间上方是否有一条路径。
现在最有趣的部分:打印地图。您可以通过循环行(从0到self.height
)和列(从0到self.width
)来完成此操作。(注意:在这种情况下,您不能使用print
,因为它会自动在字符串之后添加换行符或空格。相反,我们使用sys.stdout.write。)
def print_map(self):
for y in range(0, self.height):
for x in range(0, self.width):
if self.x == x and self.y == y:
sys.stdout.write("[u]")
else:
sys.stdout.write("[ ]")
if ((x, y), (x + 1, y)) in self.paths:
sys.stdout.write("-")
else:
sys.stdout.write(" ")
print
for x in range(0, self.width):
sys.stdout.write(" ")
if ((x, y), (x, y + 1)) in self.paths:
sys.stdout.write("| ")
else:
sys.stdout.write(" ")
print
现在,让我们把所有的东西都放在一起并试一下。这是代码:
import sys
class Map:
def __init__(self, height, width, player_x, player_y, paths):
self.height = height
self.width = width
self.x = player_x
self.y = player_y
self.paths = paths
def move(self, direction):
if direction == "n":
if ((self.x, self.y - 1), (self.x, self.y)) not in self.paths:
print "Cannot go north"
else:
self.y -= 1
if direction == "s":
if ((self.x, self.y), (self.x, self.y + 1)) not in self.paths:
print "Cannot go south"
else:
self.y += 1
if direction == "e":
if ((self.x, self.y), (self.x + 1, self.y)) not in self.paths:
print "Cannot go east"
else:
self.x += 1
if direction == "w":
if ((self.x - 1, self.y), (self.x, self.y)) not in self.paths:
print "Cannot go west"
else:
self.x -= 1
def print_map(self):
for y in range(0, self.height):
for x in range(0, self.width):
if self.x == x and self.y == y:
sys.stdout.write("[u]")
else:
sys.stdout.write("[ ]")
if ((x, y), (x + 1, y)) in self.paths:
sys.stdout.write("-")
else:
sys.stdout.write(" ")
print
for x in range(0, self.width):
sys.stdout.write(" ")
if ((x, y), (x, y + 1)) in self.paths:
sys.stdout.write("| ")
else:
sys.stdout.write(" ")
print
paths = [((0, 0), (1, 0)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((1, 1),
(2, 1)), ((1, 1), (1, 2)), ((0, 2), (1, 2)), ((1, 2), (2, 2)),
((0, 2), (0, 3)), ((0, 3), (1, 3)), ((1, 3), (2, 3)), ((2, 3),
(3, 3))]
m = Map(4, 4, 0, 0, paths)
while True:
m.print_map()
direction = raw_input("What direction do you want to move? [n/e/s/w] ")
m.move(direction)
请注意,我在底部添加了一个部分,创建了一个地图并允许玩家在地图上移动。以下是运行时的效果:
Davids-MacBook-Air:test dgrtwo$ python Map.py
[u]-[ ] [ ] [ ]
|
[ ] [ ]-[ ] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ]-[ ]
What direction do you want to move? [n/e/s/w] e
[ ]-[u] [ ] [ ]
|
[ ] [ ]-[ ] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ]-[ ]
What direction do you want to move? [n/e/s/w] s
[ ]-[ ] [ ] [ ]
|
[ ] [u]-[ ] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ]-[ ]
What direction do you want to move? [n/e/s/w] w
Cannot go west
[ ]-[ ] [ ] [ ]
|
[ ] [u]-[ ] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ]-[ ]
What direction do you want to move? [n/e/s/w] e
[ ]-[ ] [ ] [ ]
|
[ ] [ ]-[u] [ ]
|
[ ]-[ ]-[ ] [ ]
|
[ ]-[ ]-[ ]-[ ]
这段代码有很多可以改进的地方(特别是move
方法很重复),但这是一个不错的开始。尝试将地图设置为20x20,你会发现它会扩展得很好。
附:我应该指出,print_map
可以用更短的形式重写,例如:
def print_map(self):
for y in range(0, self.height):
print "".join(["[%s]%s" %
("u" if self.x == x and self.y == y else " ",
"-" if ((x, y), (x + 1, y)) in self.paths else " ")
for x in range(0, self.width)])
print " " + " ".join(["|" if ((x, y), (x, y + 1)) in self.paths
else " " for x in range(0, self.width)])
但这要更加复杂一些。