worldArray = [["." for i in range(5)] for i in range(5)]
这产生了一个地图,我可以使用我的游戏。它应该看起来像:
[['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.']]
假设'.'
代表一个草瓷砖。如果我想将特定数量的索引替换为'~'
,而不是表示一个water tile,那么最简单的方法是什么?我想让地图看起来有点像:
[['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['~', '~', '.', '.', '.'],
['~', '~', '~', '.', '.']]
我知道我可以手动遍历并更改每个特定的索引来显示'~'
图块,但是我使用的真正的游戏内地图是40x40,这会使单独替换每个索引的工作变得有点乏味和多余。我想能够定义我要更换的磁贴,即第4行,第1-2列;第5行,第1-3列。我怎么能这样做呢?你知道吗
你可以写一个助手函数
你可以这样使用它:
这将导致:
我将定义一个函数,返回是否使其
~
而不是.
。你知道吗您可以根据自己的喜好更改
regular()
函数。你知道吗切片表示法非常适合:
相关问题 更多 >
编程相关推荐