我正在尝试用python构建基于文本的开放世界游戏
world = [["=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","=","="],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "],
]
for i in world:
for j in i:
print(j, end="")
print("")
此代码成功打印了世界地图,尽管速度有些慢。有没有办法提高性能?最终,我想要一张巨大的地图,玩家可以在其中移动,与敌人,NPC等互动。有什么方法可以使渲染速度更快,使其优于.1 FPS?
我正在使用Python 3.7 IDLE。
谢谢!
答案 0 :(得分:1)
使用.join()
快速将列表串在一起。
print('\n'.join([''.join(row) for row in world]))
在IDLE上,print
有很多开销。您最好在打印字符串之前先对其进行编译,以获得最佳性能。
答案 1 :(得分:1)
String在python中已经是只读的迭代器。所以你可以做类似的事情
world = [
" X ",
" XXX ",
" X ",
]
for row in world:
print(row)
但是您实际上应该尽量减少对print
的调用,这是慢。也许您只能使用一个三重引号的字符串?
world = """\
X
XXX
X """
print(world)
使用这样的字符串,您可以通过计算(i,j)
元素之前的字符数来访问它。每行有board_width + 1
个字符(不要忘记计算换行符),并且在元素j
之前有(i,j)
行。另外,在0
处建立索引,与i
在同一行上有(i,j)
个元素,但在(i,j)
的左侧,因此(i,j)
元素为{ {1}}。
如果您希望保留双精度数组,因为它是 mutable ,请考虑在打印之前将其合并为单个字符串。为此,请使用world[(row_width + 1) * j + i]
在一次调用中合并每一行。
map
edit1 :您可以像TrebuchetMS一样理解列表,但是实际上它是在python3中构建列表的,map将创建一个迭代器,在调用{之前不需要存储所有已转换的行{1}},而不是即时计算转换后的行列表,而无需将其存储在列表中。
edit2 :要部分渲染世界,只需在构建字符串之前使用列表的范围选择即可。例如,world [j1:j2]仅包含从索引displayedWorld = "\n".join(map("".join, world))
(包括)到索引"\n".map
(不包括)的世界行。因此,如果要显示从j1
到尺寸为j2
的矩形,请执行以下操作:
(i,j)
这使用了一个新的成语lambda,它只是一个未命名的函数。 λ(w,h)
等效于方法displayedWorld = "\n".join(map(lambda row: "".join(row[i:i+w]), world[j:j+h]))
。因此,lambda row: "".join(row)
仅合并行中从"".join
到lambda row: "".join(row[i:i+w])
的情况。请注意,我们也仅选择了世界上从i
到i+w
的行。
答案 2 :(得分:0)
如果您使用的是Linux / osx,则可以签出curses
。使用该库,您可以单独绘制像素,而不必一直重新绘制像素。
涉及到巨大的地图:仅绘制地图上当前可见的部分。