2017-06-20 124 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

相关问题