2017-06-20 206 views
0

我試圖在沒有pygame和curses的Linux終端上製作遊戲。我做了一個叫做readlevels()的函數,在第36和39行,出現了一個錯誤。該計劃的一部分是確保所有級別都適合終端。我甚至用print來確保變量是正確的。任何人都知道爲什麼它不起作用?我使用python 2.7,關卡存儲在一個.txt文件中,但我懷疑這會有所幫助。如果x> y不工作,其中x大於y python 2.7

import sys, os, time 
def readlevels(): 
     #Open level file 
     levelfile = 'levels.txt' #file with all the levels 
     try: 
       leveltxt = open(levelfile, 'r') 
     except IOError: 
       return str('The file ' + levelfile + ' is not in this directory. Please look for a file with the levels and name it' +levelfile + ' before continuing the game. This file is needed to store all of the levels$ 
     #Read level file 
     levels = [] 
     currentlevel = [] 
     for line in leveltxt.readlines(): 
       if ';' in line: #lines with ';' will be ignored 
         pass 
       elif '~' in line: #lines with '~' will seperate levels (also show level number) 
         levels.append(currentlevel) 
         currentlevel = [] 
       else: #all other lines are part of the level 
         currentlevel.append(line) 
     #Make Necessary adjustments in the list 'levels' and make the cleaner list 'levels2' 
     levels = levels[1:] #gets rid of the [] at the beginning of 'levels' (bug fix) 
     levels2 = [] 
     for level in levels: 
       level2 = [] #not to be confused with 'levels2' which has an 's'. This replaces all items in 'levels' while 'levels2' replaces the entire list 
       for line in level: 
         level2.append(line.rstrip()) 
       levels2.append(level2) 
     #Overwrite 'levels' with 'levels2' so it does not look weird for the rest of the program 
     levels = levels2 
     #Checks to see if all levels will fit on the terminal 
     terminalsize = os.popen('stty size', 'r').read().split() #terminalsize[0] is rows in terminal, terminalsize[1] is letters per row 
     print terminalsize 
     levelnum = 1 #level number 
     for level in levels: #level is now each level again 
       print len(level), terminalsize[0] #this was to check if len(level) and terminalsize[0] was the number I wanted 
       if len(level) > terminalsize[0]: #if level has more rows than the terminal 
         return str('Level ' + levelnum + ' is too big for the terminal. Easily fixable by increasing the size of the terminal or decreasing the font size, or deleting/changing the level') 
       for line in level: 
         if len(line) > terminalsize[1]: #if level is too wide for the terminal 
           return str('Level ' + levelnum + ' is too big for the terminal. Easily fixable by increasing the size of the terminal, decreasing the font size, or deleting/changing the level') 
       levelnum += 1 
     #Finish 
     return levels 
print readlevels() 

下面的代碼是levels.txt,但我懷疑它幫助。

-Blocks --------- P 
-Enemies -------- A, B, C, D, E 
-Exit Block ----- S 
-Start Block ---- S 




~1 
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP 
P             P 
P             P 
P             P 
P             P 
P             P 
P        EEEE     P 
P        E E     P 
P        E E     P 
P        EEEE     P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P             P 
P       S      P 
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP                

~2 
- TO BE CONTD 

「Readlevels」正常返回的所有級別的列表,以及列表中的每個電平是在該級別的所有行的另一個列表。

+0

什麼是第36行?什麼是第39行? –

+0

它檢查該級別是否適合終端的部分。我在問題的頂部說了這個問題。第36行是'if len(level)> terminalsize [0]:',第39行是'如果len(line)> terminalsize [1]:' – SnootierBaBoon

回答

1

看起來你正在比較整數與字符串。嘗試

if len(level) > int(terminalsize[0]):

代替。

+0

謝謝,我不知道終止符是一個字符串,因爲當我使用'print'來檢查數字是否正確,它不會顯示引號。 – SnootierBaBoon

相關問題