所以我正在研究一个我正在制作的小型Python游戏的渲染系统,我发现当我打印用于存储图块渲染的Render字典时,我发现每行的第四个图块都是实际渲染。我打算将它当前输出的内容输出到每四个图块到渲染距离内的每个图块,但遗憾的是它不起作用。我的代码和输出如下:
主:
import RenderGame
import CreateWorld
global Map
Rows = {}
#Start.start()
#MainMenu.listMenu_Main00()
Map = CreateWorld.createLandscape(5, 5)
#print Map
Render = RenderGame.render(Map, 0, 0, 2, 2)
print Render
#random.seed("z12")
#print random.randint(1, 5)
世界一代:
global Map
Map = {}
Map["Rows"] = {}
#The Process of Generating a Blank Map xAxis long and zAxis wide
def createLandscape(xAxis, zAxis):
#Sets the amount of createdRows to 0
createdRows = 0
#Calls createRow() the amount of times that the Map is Long
while createdRows < xAxis:
createRow(createdRows, zAxis)
createdRows += 1
return Map
#The Process of Generating a Blank Map Row zAxis Wide and with a Row Number of RowsCreated
def createRow(RowsCreated, zAxis):
#Sets the variable "createdTiles" to 0
createdTiles = 0
#Sets the variable "createdRows" to the parameter "RowsCreated"
createdRows = RowsCreated
#Creates a Row within the dictionary "Rows"
Map["Rows"]["Row %s" % createdRows] = {}
#Calls createTile() the amount of times that the Row is wide
while createdTiles < zAxis:
createTile(createdRows, createdTiles)
createdTiles += 1
#The Process of Generating a Blank Map Tile in the
def createTile(RowsCreated, TilesCreated):
#Sets the variable "createdRows" to the parameter "RowsCreated"
createdRows = RowsCreated
#Sets the variable "createdTiles" to the parameter "TilesCreated"
createdTiles = TilesCreated
Map["Rows"]["Row %s" % createdRows]["Tile %s" % createdTiles] = {}
Map["Rows"]["Row %s" % createdRows]["Tile %s" % createdTiles]["Type"] = "Null"
渲染器:
global Render
Render = {}
def render(Map, SelectedRow, SelectedTile, xDistance, zDistance):
#Sets the variable "createdRows" to the parameter "RowsCreated"
rowSelected = SelectedRow
tileSelected = SelectedTile
if checkSelection(Map, SelectedRow, tileSelected):
renderMap(Map, rowSelected, tileSelected, xDistance, zDistance)
return Render
#DisplayGame.displayRender(Render, xDistance, zDistance)
else:
print "Readjusting Selection"
rowSelected = 0
tileSelected = 0
def checkSelection(Map, SelectedRow, SelectedTile):
#Make sure the Map has Rows
if "Rows" in Map:
#Make sure the Rows has the Selected Row
if ("Row %s" % SelectedRow) in Map["Rows"]:
#Make sure the Selected Row has the Selected Tile
if ("Tile %s" % SelectedTile) in Map["Rows"]["Row %s" % SelectedRow]:
return True
#Give Error Message if I cluck up
else:
print " [DEBIG]: The Key that represents to Selected Tile cannot be found in the Dictionary that represents the Selected Row "
#Give Error Message if I cluck up
else:
print " [DEBIG]: The Key that represents to Selected Row cannot be found in the Dictionary \"Rows\" "
#Give Error Message if I cluck up
else:
print " [DEBIG]: The Key \"Rows\" cannot be found in the Dictionary \"Map\" "
def renderMap(Map, SelectedRow, SelectedTile, xDistance, zDistance):
rowRendersCreated = 0
while rowRendersCreated < 1 + (zDistance * 2):
createRowRender(Map, SelectedRow, SelectedTile, xDistance, zDistance, rowRendersCreated)
rowRendersCreated += 1
def createRowRender(Map, SelectedRow, SelectedTile, xDistance, zDistance, CreatedRowRenders):
rowRendersCreated = CreatedRowRenders
tileRendersCreated = 0
while tileRendersCreated < 1 + (xDistance * 2):
Render["Row %s" % rowRendersCreated] = {}
createTileRender(Map, SelectedRow, SelectedTile, xDistance, zDistance, rowRendersCreated, tileRendersCreated)
tileRendersCreated += 1
def createTileRender(Map, SelectedRow, SelectedTile, xDistance, zDistance, CreatedRowRenders, CreatedTileRenders):
rowRendersCreated = CreatedRowRenders
tileRendersCreated = CreatedTileRenders
Render["Row %s" % rowRendersCreated]["Tile %s" % tileRendersCreated] = ""
if Map["Rows"]["Row %s" % rowRendersCreated]["Tile %s" % tileRendersCreated]["Type"] == "Null":
Render["Row %s" % rowRendersCreated]["Tile %s" % tileRendersCreated] = "--------"
输出:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
{'Row 1': {'Tile 4': '--------'}, 'Row 0': {'Tile 4': '--------'}, 'Row 3': {'Tile 4': '--------'}, 'Row 2': {'Tile 4': '--------'}, 'Row 4': {'Tile 4': '--------'}}
>>>
任何人都知道为什么会这样?
答案 0 :(得分:1)
我还没有尝试为你的所有代码片段创建单独的文件,但我认为我可以发现问题而无需运行代码。在createRowRender
中,每次增加磁贴数量时,都会为每行重置字典:
while tileRendersCreated < 1 + (xDistance * 2):
Render["Row %s" % rowRendersCreated] = {}
createTileRender(Map, SelectedRow, SelectedTile, xDistance, zDistance, rowRendersCreated, tileRendersCreated)
tileRendersCreated += 1
我认为应该是这样的:
while tileRendersCreated < 1 + (xDistance * 2):
rowName = "Row %s" % rowRendersCreated
if not rowName in Render:
Render[rowName] = {}
createTileRender(Map, SelectedRow, SelectedTile, xDistance, zDistance, rowRendersCreated, tileRendersCreated)
tileRendersCreated += 1