Pygame列表错误,对象中的对象

时间:2019-03-24 18:15:31

标签: python pygame

我正在运行pygame并尝试使自己熟悉该模块。每当我运行此代码时,总会出现以下问题:

levelObj = levels[levelNum]  
     

IndexError:列表索引超出范围

我输入的代码显示了levelNum出现的唯一实例

def runLevel(levels, levelNum):
    global currentImage
    levelObj = levels[levelNum]
    mapObj = decorateMap(levelObj['mapObj'], levelObj['startState']['player'])
    gameStateObj = copy.deepcopy(levelObj['startState'])
    mapNeedsRedraw = True
    levelSurf = BASICFONT.render('Level %s of %s' % (levelNum + 1, len(levels)), 1, TEXTCOLOR)
    levelRect = levelSurf.get_rect()
    levelRect.bottomleft = (20, WINHEIGHT - 35)
    mapWidth = len(mapObj) *  TILEWIDTH
    mapHeight = (len(mapObj[0]) - 1) * TILEFLOORHEIGHT + TILEHEIGHT
    MAX_CAM_X_PAN = abs(HALF_WINHEIGHT - int(mapHeight / 2)) + TILEWIDTH
    MAX_CAM_Y_PAN = abs(HALF_WINWIDTH - int(mapWidth / 2)) + TILEHEIGHT

然后将其余所有代码放在一起

def readLevelsFile(filename):
    assert os.path.exists(filename), 'Cannot find the level file: %s' % (filename)

    mapFile = open(filename, 'r')
    content = mapFile.readlines() + ['\r\n']
    mapFile.close()

    levels= []
    levelNum = 0
    mapTextLines = []
    mapObj = []
    for lineNum in range (len(content)):
        line = content[lineNum].rstrip('\r\n')

        if ';' in line:
            line = line[:line.find(';')]

        if line != ' ':
            mapTextLines.append(line)

        elif line == ' ' and len(mapTextLines) > 0:
            maxWidth = -1
            for i in range (len(mapTextLines)):
                if len(mapTextLines[i]) > maxWidth:
                    maxWidth = len(mapTextLines[i])


            for i in range (len(mapTextLines)):
                mapTextLines[i] += ' ' * (maxWidth - len(mapTextLines[i]))

            for x in range (len(mapTextLines[0])):
                mapObj.append([])
            for y in range (len(mapTextLines)):
                for x in range (maxWidth):
                    mapObj[x].append(mapTextLines[y][x])

            startx = None
            starty = None
            goals = []
            stars = []
            for x in range (maxWidth):
                for y in range (len(mapObj[x])):
                    if mapObj[x][y] in ('@', '+'):
                        startx = x
                        starty= y

                    if mapObj[x][y] in ('.', '+', '*'):
                        goals.append(( x, y))

                    if mapObj[x][y] in ('$', '*'):
                        stars.append(( x, y))

            assert startx != None and starty != None, 'Level %s (around line %s) in %s is missing a '@' or '+' to mark the start pont.' % (levelNum + 1, lineNum, filename)

            assert len(goals) > 0, 'Level %s (around line &s) in %s must have at least one goal. ' % (levelNum+1, lineNum, filename)

            assert len(stars) >= len(goals), 'Level %s (around line %s) in %s is impossible to solve. It has %s goals but only %s stars.' % (levelNum+1, lineNum, filename, len(goals), len(stars))


            gameStateObj = {'player': (startx, starty), 
                            'stepCounter': 0,
                            'stars': stars}

            levelObj = {'width': maxWidth,
                        'height': len(mapObj),
                        'mapObj': mapObj,
                        'goals': goals,
                        'startState': gameStateObj}

            levels.append(levelObj)

            mapTextLines = []
            mapObj = []
            gameStateObj = {}
            levelNum += 1
    return levels

0 个答案:

没有答案