0
#!/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()
我想製作一個簡單的基於文本的遊戲,你在'#'的網格周圍移動'@' 並嘗試找到退出。我已經改變了代碼,使我更容易使電網更大或更小而不添加或刪除的代碼大塊,它不斷給我這個輸出:Python for loop breaks基於文本的遊戲
fail
# # # # #
@ # # # #
@ # # # #
@ # # # #
@ # # # #
11 52
,我想不出它出什麼問題了!只有一個'@'應該出現:( 我只是一個新手在python所以如果你有任何提示,以改善此請,請不要猶豫,併發布它們! 在此先感謝,
你的代碼總體上有太多的空白;你能刪除過多的空行並減少縮進嗎?每層4個空格更具可讀性。在代碼中似乎也有一些縮進錯誤。 –
@MartijnPieters我已經做到了,但仍然失敗。 – Glollum
我沒有說這可以解決您的問題。 :-P我只想讓人們更容易幫助你。 –