【发布时间】:2012-02-03 16:46:08
【问题描述】:
我正在使用 Python 2.7 libtcod 制作 Roguelike,目前正在尝试获取一个函数来根据给定的楼层号、x1、x2、y1 和 y2 坐标集创建一个单独的房间。预期的行为是我应该得到一个以墙壁为界的房间,否则将充满空虚。例如,makeRoom(0, 1, 1, 10, 10) 应该给出:
----------
|********|
|********|
|********|
|********|
|********|
|********|
|********|
|********|
----------
然而,我得到了一个和地牢地板一样宽的房间,只考虑高度,而侧面的墙壁永远不会到达。所以相反,我得到的安排更像这样:
---------------------------
***************************
***************************
***************************
***************************
***************************
***************************
***************************
***************************
---------------------------
更改地牢地板的总可用宽度会导致房间缩小或水平扩大,无论我要求它具有什么尺寸。无论我如何调整范围、调整它们的位置、评论或取消评论,侧壁都不会出现。简而言之,无论我尝试什么,都只遵循高度和顶部/底部。
base.py(主游戏循环):
import sys
import libtcodpy as libtcod
import entity
import dungeon
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20
libtcod.console_set_custom_font('../dejavu10x10_gs_tc.png', libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'MadNet Zero', False)
libtcod.console_set_foreground_color(0, libtcod.white)
# The buggy functions
saturnSeven = dungeon.Dungeon(5)
saturnSeven.makeRoom(0, 1, 1, 10, 10)
player = entity.Player(25,25,3,4,0)
libtcod.console_print_left(0, player.X, player.Y, libtcod.BKGND_NONE, '@')
libtcod.console_flush()
while not libtcod.console_is_window_closed():
libtcod.console_set_foreground_color(0, libtcod.white)
#clear the player's old position
libtcod.console_print_left(0, player.X, player.Y, libtcod.BKGND_NONE, ' ')
#print terrain
for y in range(len(saturnSeven.floor[player.Z][0])):
for x in range(len(saturnSeven.floor[player.Z])):
symbol = saturnSeven.getTerrainSymbol(x,y,player.Z)
libtcod.console_print_left(0, x, y, libtcod.BKGND_NONE, symbol)
libtcod.console_print_left(0, player.X, player.Y, libtcod.BKGND_NONE, player.char)
libtcod.console_flush()
dungeon.py(完成工作):
# Define terrain types as a pseudo-enum
wall, empty, solid = range(3)
class Cell(object):
def __init__(self, terrain, items):
self.terrain = terrain
self.items = items
def isPassable(self):
if self.terrain != wall:
return True
else:
return False
class Dungeon(object):
def __init__(self, numfloors):
self.MAXFLOORS = numfloors
self.floor = [None]*numfloors
for i in range(numfloors):
self.floor[i] = makeFloor(20,20)
def makeRoom(self, floorNum, Xmin, Ymin, Xmax, Ymax):
#inner area
for x in range(Xmin+1, Xmax):
for y in range(Ymin+1, Ymax):
self.floor[floorNum][x][y] = Cell(empty, None)
#top/bottom wall
for x in range(Xmin,Xmax+1):
self.floor[floorNum][x][Ymin] = Cell(wall, None)
self.floor[floorNum][x][Ymax] = Cell(wall, None)
#left/right wall
for y in range(Ymin, Ymax+1):
self.floor[floorNum][Xmin][y] = Cell(wall, None)
self.floor[floorNum][Xmax][y] = Cell(wall, None)
def getTerrainSymbol(self, X, Y, Z):
if self.floor[Z][X][Y] == None:
return ' '
if self.floor[Z][X][Y].terrain == wall:
return '#'
if self.floor[Z][X][Y].terrain == solid:
return ' '
if self.floor[Z][X][Y].terrain == empty:
return '.'
def makeFloor(Xmax, Ymax):
return [[Cell(solid, None)]*Xmax]*Ymax
我已经消除了图形的问题;地牢本身肯定有问题,而不是它在屏幕上的显示方式。不过,我找不到任何理由让算法以这种方式出错。
编辑:给出上图的代码的原始版本在构成墙壁的部分下方有“内部区域”部分;像我一样把它放在上面,结果整个房间都是由墙壁组成的,但仍然太宽了。据我所知,他们应该给出相同的结果;他们没有。
【问题讨论】:
-
您是否尝试在每个创建房间的块之前和之后查看 self.floor?
-
我让循环给了我应该写单个墙段的坐标,我依次注释/取消注释函数的每个部分以查看不同的结果。即使我把它缩小到一排,那一排仍然会尽可能地宽,而不是像设定的那样宽。
标签: python python-2.7 roguelike