【发布时间】:2017-02-12 16:19:20
【问题描述】:
我一直在尝试创建一个程序生成的地下城,如 this article 所示。但这对我来说有点难以理解这种算法的工作原理。因此,我一直使用this 作为至少了解房间布置的指南。
文章中使用的程序是用Java编写的,所以我对自己的“现实”做了一些改编,并尝试在Python 3.5中模拟相同的结果。
我的代码如下:
from random import randint
class Room:
"""docstring for Room"""
def __init__(self, x, y, w, h):
"""[summary]
[description]
Arguments:
x {int} -- bottom-left horizontal anchorpoint of the room
y {int} -- bottom-left vertical anchor point of the room
w {int} -- width of the room
h {int} -- height of the room
"""
self.x1 = x
self.x2 = x + w
self.y1 = y
self.y2 = y + h
self.w = w
self.h = h
self.center = ((self.x1 + self.x2)/2, (self.y1 + self.y2)/2)
def intersects(self, room):
"""[summary]
Verifies if the rooms overlap
Arguments:
room {Room} -- a room object
"""
return(self.x1 <= room.x2 and self.x2 >= room.x1 and \
self.y1 <= room.y2 and self.y2 >= room.y1)
def __str__(self):
room_info = ("Coords: (" + str(self.x1) + ", " + str(self.y1) +
") | (" + str(self.x2) + ", " + str(self.y2) + ")\n")
room_info += ("Center: " + str(self.center) + "\n")
return(room_info)
MIN_ROOM_SIZE = 10
MAX_ROOM_SIZE = 20
MAP_WIDTH = 400
MAP_HEIGHT = 200
MAX_NUMBER_ROOMS = 20
dungeon_map = [[None] * MAP_WIDTH for i in range(MAP_HEIGHT)]
# print(dungeon_map)
def crave_room(room):
"""[summary]
"saves" a room in the dungeon map by making everything inside it's limits 1
Arguments:
room {Room} -- the room to crave in the dungeon map
"""
for x in xrange(min(room.x1, room.x2), max(room.x1, room.x2) + 1):
for y in xrange(min(room.y1, room.y2), max(room.y1, room.y2) + 1):
print(x, y) # debug
dungeon_map[x][y] = 1
print("Done") # dungeon
def place_rooms():
rooms = []
for i in xrange(0, MAX_NUMBER_ROOMS):
w = MIN_ROOM_SIZE + randint(0, MAX_ROOM_SIZE - MIN_ROOM_SIZE + 1)
h = MIN_ROOM_SIZE + randint(0, MAX_ROOM_SIZE - MIN_ROOM_SIZE + 1)
x = randint(0, MAP_WIDTH - w) + 1
y = randint(0, MAP_HEIGHT - h) + 1
new_room = Room(x, y, w, h)
fail = False
for other_room in rooms:
if new_room.intersects(other_room):
fail = True
break
if not fail:
print(new_room)
crave_room(new_room) # WIP
new_center = new_room.center
# rooms.append(new_room)
if len(rooms) != 0:
prev_center = rooms[len(rooms) - 1].center
if(randint(0, 1) == 1):
h_corridor(prev_center[0], new_center[0], prev_center[1])
v_corridor(prev_center[1], new_center[1], prev_center[0])
else:
v_corridor(prev_center[1], new_center[1], prev_center[0])
h_corridor(prev_center[0], new_center[0], prev_center[1])
if not fail:
rooms.append(new_room)
for room in rooms:
print(room)
def h_corridor(x1, x2, y):
for x in xrange(min(x1, x2), max(x1, x2) + 1):
dungeon_map[x][y] = 1
def v_corridor(y1, y2, x):
for y in xrange(min(y1, y2), max(y1, y2) + 1):
dungeon_map[x][y] = 1
place_rooms()
但是每当我运行它时,我都会收到以下错误:
Traceback (most recent call last):
File "/home/user/dungeon.py", line 114, in <module>
place_rooms()
File "/home/user/dungeon.py", line 87, in place_rooms
crave_room(new_room)
File "/home/user/dungeon.py", line 65, in crave_room
dungeon_map[x][y] = 1
IndexError: list index out of range
根据我从代码中了解到的情况,crave_room 函数应该可以正常工作,因为我正在使用 min 和 max 函数。并且由于h_corridor 和v_corridor 函数的工作方式相似,它们提出了同样的问题。
我不确定问题是否正在发生,因为我使用矩阵代替原始文章中使用的画布。我怀疑是局部/全局变量问题,但我认为这不是问题所在。恐怕我犯了一个非常愚蠢的错误而没有看到它。
欢迎任何关于使用更好的数据结构的代码改进提示或建议,如果有人有,请在该主题上发表更清晰/更简单的文章,最好是关于 Python,我在这里看到了很多相关的帖子,但是我还是有点迷茫。
感谢您的帮助。 :D
【问题讨论】:
-
可能与您的问题无关,但
dungeon_map = [[None] * MAP_WIDTH] * MAP_HEIGHT稍后会引起麻烦。有关详细信息,请参阅Python list of lists, changes reflected across sublists unexpectedly -
大胆猜测:尝试使矩阵高/高/宽一列。
dungeon_map = [[None] * (MAP_WIDTH+1) for _ in range(MAP_HEIGHT+1)] -
只是另一个提示:使用 numpy,它是数组而不是列表列表,特别是如果您打算稍后对它们进行操作。
-
我只能说,你应该将 room.x 放在
dungeon_map大小的范围内的函数根本不起作用。您有一个包含 200 行的矩阵,而您的room.x的范围是 350 - 370。您的MAP_WIDTH值为 400(这就是问题所在) -
也许您最好使用一种数据结构,它可以让您将值分配给任何坐标,而无需提前指定空间边界。试试
import collections和dungeon_map = collections.defaultdict(dict)。
标签: python matrix indexoutofboundsexception python-3.5 procedural-generation