[英]Need to check what character at a certain position in a list is
我需要查看列表中特定行和列的字符。 我正在创建一个游戏,需要防止玩家穿过墙壁。
到目前为止,这是我的代码。
import copy
def print_house(h,sr,sc):
th = copy.deepcopy(h)
th[sr][sc] = "@"
for i in th:
print(''.join(str(x) for x in i), end='')
def build_house():
#print("Please enter the house file: ")
house_file = "house.txt"
housefp = open(house_file, "r")
global myhouse
myhouse = []
line = housefp.readline()
while line:
myhouse.append(list(line))
line = housefp.readline()
return myhouse
def check_north(h,sr,sc):
if sc-1 == "*":
return False
else:
return True
def check_south(h,sr,sc):
if sr+1 == "*":
return False
else:
return True
def check_east(h,sr,sc):
if sc+1 == "*":
return False
else:
return True
def check_west(h,sr,sc):
if sc-1 == "*":
return False
else:
return True
def main():
global house, startrow, startcol
house = build_house()
startrow, startcol = 1,3
num_treasures = 2
tcount = 0
while tcount < num_treasures:
print_house(myhouse, startrow, startcol)
print("You can go N,S,W,E")
command = input("Please enter where to do using the w,a,s,d keys or q to quit:")
if command == "w" and check_north(myhouse, startrow, startcol) == True:
trow = startrow-1
tcol = startcol
elif command == "a" and check_west(myhouse, startrow, startcol) == True:
trow = startrow
tcol = startcol-1
elif command == "s" and check_south(myhouse, startrow, startcol) == True:
trow = startrow+1
tcol = startcol
elif command == "d" and check_east(myhouse, startrow, startcol) == True:
trow = startrow
tcol = startcol+1
elif command == "q":
return
else:
print("Sorry, you can't go through walls")
startrow,startcol = trow,tcol
main()
它还在这里使用带有 map 的文本文件。 文本文件名为 house.txt
***************
*** 0 ********************************
*** 6 *
*********** ************************** *
* * * *
********5*************** * t *
* * ******
* t 1 *
************************
如果我没看错,您尝试使用if sc-1 == "*":
检查 char 但您应该以与set
char 相同的方式get
char - 使用th[sr][sc]
。
所以你需要
if th[sr][sc-1] == "*":
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.