#!/usr/bin/env python
import random
import time
import os
class vars:
running = 1
def win ():
print("You escaped!")
vars.running = 0
time.sleep(4)
return 0
def main ():
char_loc = 11 #The characters current co-ordinates in XY format
pos_char_loc = 11
ex_y = random.randint(1, 5)
ex_x = random.randint(1, 5) * 10
ex_loc = ex_x + ex_y
while vars.running == 1:
os.system('CLS')
x0 = ["#"] * 5
x1 = ["#"] * 5
x2 = ["#"] * 5
x3 = ["#"] * 5
x4 = ["#"] * 5
if (char_loc >= 11 and char_loc <= 55):
if (char_loc >= 11 and char_loc <= 15):
i = 0; k = 11
for x in range(0, 4):
if char_loc == k:
x0.insert(i, '@')
else:
i += 1
k += 1
if (char_loc >= 21 and char_loc <= 25):
i =0; k = 21
for loop1 in range(0, 4):
if char_loc == k:
x1.insert(i, '@')
else:
i += 1
k += 1
if (char_loc >= 31 and char_loc <= 35):
i =0; k = 31
for loop2 in range(0, 4):
if char_loc == k:
x2.insert(i, '@')
else:
i += 1
k += 1
if (char_loc >= 41 and char_loc <= 45):
i =0; k = 41
for loop3 in range(0, 4):
if char_loc == k:
x3.insert(i, '@')
else:
i += 1
k += 1
if (char_loc >= 51 and char_loc <= 55):
i =0; k = 51
for loop5 in range(0, 4):
if char_loc == k:
x4.insert(i, '@')
else:
i += 1
k += 1
else:
print("fail")
print( x0[4],x1[4],x2[4],x3[4],x4[4])
print( x0[3],x1[3],x2[3],x3[3],x4[3])
print( x0[2],x1[2],x2[2],x3[2],x4[2])
print( x0[1],x1[1],x2[1],x3[1],x4[1])
print( x0[0],x1[0],x2[0],x3[0],x4[0])
print(char_loc, ex_loc)
if char_loc == ex_loc:
win()
move = input()
if move == "w" and (char_loc != 15 and char_loc != 25 and char_loc != 35 and char_loc != 45 and char_loc !=55):
char_loc += 1
print("up")
elif move == "s" and (char_loc != 11 and char_loc != 21 and char_loc != 31 and char_loc != 41 and char_loc != 51):
char_loc -= 1
print("down")
elif move == "a" and (char_loc != 11 and char_loc != 12 and char_loc != 13 and char_loc != 14 and char_loc != 15):
char_loc -= 10
print("left")
elif move == "d" and (char_loc != 51 and char_loc != 52 and char_loc != 53 and char_loc != 54 and char_loc != 55):
char_loc += 10
print("right")
else: print("You can't move there!")
if __name__ == '__main__': main()
我正在尝试制作一个简单的基于文本的游戏,您可以在其中围绕“#”网格移动“@”并尝试找到出口。我已经更改了代码,使我更容易在不添加或删除大量代码的情况下使网格更大或更小,并且它不断给我这个输出:
fail
# # # # #
@ # # # #
@ # # # #
@ # # # #
@ # # # #
11 52
我无法弄清楚它有什么问题!只有一个'@'应该出现:(我只是python的新手,所以如果你有任何改进的建议,请不要犹豫,发布它们!提前致谢,