2013-07-25 41 views
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所以如果你有任何提示,以改善此請,請不要猶豫,併發布它們! 在此先感謝,

+0

你的代碼總體上有太多的空白;你能刪除過多的空行並減少縮進嗎?每層4個空格更具可讀性。在代碼中似乎也有一些縮進錯誤。 –

+0

@MartijnPieters我已經做到了,但仍然失敗。 – Glollum

+0

我沒有說這可以解決您的問題。 :-P我只想讓人們更容易幫助你。 –

回答

0

我認爲出現「故障」,因爲它會發生在每次char_loc是不是和55

if (char_loc >= 11 and char_loc <= 15): 
if (char_loc >= 21 and char_loc <= 25): 
if (char_loc >= 31 and char_loc <= 35): 
if (char_loc >= 41 and char_loc <= 45): 
if (char_loc >= 51 and char_loc <= 55): 
else: 

我想什麼你想在這裏做的之間的51時間使用ELIF,這隻會火如果前面的檢查不要't trigger。

if (char_loc >= 11 and char_loc <= 15): 
elif (char_loc >= 21 and char_loc <= 25): 
elif (char_loc >= 31 and char_loc <= 35): 
elif (char_loc >= 41 and char_loc <= 45): 
elif (char_loc >= 51 and char_loc <= 55): 
else: 

關於t他多個@符號,我認爲這可能起一部分作用。目前您有:

if char_loc == k: 
    x0.insert(i, '@') 
else: 
    i += 1 
    k += 1 

我想你希望做的是:

if char_loc == k: 
    x0.insert(i, '@') 

i += 1 
k += 1 

既然你想K改變每一次循環迭代。我會建議

最後一兩件事,因爲你必須:

i =0; k = 21 
i =0; k = 31 
i =0; k = 41 
i =0; k = 51 

你可能會想添加

i =0; k = 11 

到了第一位。

希望有所幫助。