我一直在嘗試創建程序生成的地下城,如this article所示。但是這對我來說理解這種算法的工作有點難。相反,我一直在使用this作爲指導,至少了解房間的位置。本文中使用的程序是用Java編寫的,因此我對「現實」做了一些修改,並嘗試在Python 3.5中模擬相同的結果。IndexError:列表索引超出範圍
我的代碼如下:
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 = [無] * MAP_WIDTH] * MAP_HEIGHT'會在稍後造成麻煩。有關更多信息,請參閱[列表的Python列表,意外地在子列表中反映的更改](http://stackoverflow.com/q/240178/953482) – Kevin
瘋狂猜測:嘗試使矩陣一行/列更高/更寬。'dungeon_map = [[None] *(MAP_WIDTH + 1)for _ in range(MAP_HEIGHT + 1)]' – Kevin
謝謝,我沒有意識到這一點。我會更新代碼。 – inblank