[英]How do people make a Level in a game with a list of strings in python
i would like to know because i am making games in pygame and would like to use this method because it looks neat and tidy. 我想知道,因为我正在pygame中制作游戏,并且想使用此方法,因为它看起来很整洁。
EXE 可执行程序
level = "WWWWWWWW" \
"WSSSSSSW" \
"WSSSSSSW" \
"WSSSSSSW" \
"WSSSSSSW" \
"WWWWWWWW"
Here the s's are empty space and the w's are blocks 这里s是空白空间,w是块
If you use that method, you will need to define the dimensions of the level - LEVELWIDTH and LEVELHEIGHT, in this case 8 and 6, then you can find the block at x, y with this code: 如果使用该方法,则需要定义级别的尺寸-LEVELWIDTH和LEVELHEIGHT,在这种情况下为8和6,然后可以使用以下代码在x,y处找到块:
LEVELWIDTH = 8
LEVELHEIGHT = 6
def GetBlock(x, y):
return level[x * LEVELWIDTH + y]
remembering that the indexes x and y both start at zero and end with the last item at the index length - 1 because of Python's method of indexing. 请记住,由于Python的索引方法,索引x和y都从零开始,以索引长度的最后一项结束-1。
Here is a different method I have used in situations like this: 这是我在这种情况下使用的另一种方法:
level = ['WWWWWWWW',
'WSSSSSSW',
'WSSSSSSW',
'WSSSSSSW',
'WSSSSSSW',
'WWWWWWWW']
with this method all you need to do to find what block is at (x, y) is: 使用此方法,您需要找到在(x,y)处的块是:
level[x][y]
but remember if you want to be able to edit the level, you need to create a new string for each row you edit, because you cant assign items to a string. 但是请记住,如果您希望能够编辑关卡,则需要为编辑的每一行创建一个新的字符串,因为您不能将项目分配给字符串。 Eg: To set the block at x, y with the string new: 例如:使用新字符串将块设置在x,y处:
level[y] = level[x][:y] + new + level[x][y+1:]
See https://docs.python.org/3/tutorial/introduction.html#strings for more information on indexing the method I have shown is based off of the examples in this book http://inventwithpython.com/pygame/ and a good example of levels in use in the book is in chapter 9 - Star Pusher 请参阅https://docs.python.org/3/tutorial/introduction.html#strings,以获取更多关于索引该方法的信息,该方法基于本书http://inventwithpython.com/pygame/中的示例,并且第9章-Star Pusher是本书中使用的关卡的一个很好的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.