Pygame AttributeError没有属性'显示'

时间:2014-06-11 00:40:33

标签: python pygame tiling pygame-surface

我试图通过制作基于2D平铺的平台游戏来学习Python和Pygame。现在我被困在基于音乐的""部分。这是我的代码:

import pygame, sys
from pygame.locals import *

#Just defining some variables
windowWidth = 640
windowHeight = 480
mapWidth = windowWidth // 32
mapHeight = windowHeight // 32
tilesize = 32
speed = [1, 1] #Array/List declaration
black = (0,0,0) #Tuple declaration

#intended to create a 2d list of subsurfaces    
def create_map():
    floor = pygame.image.load("rect_gray0.png")
    map = []
    for x in range(mapWidth):
        line = []
        map.append(line)
        for y in range(mapHeight):
            line.append(floor.subsurface((0,0,tilesize,tilesize)))

    return map


if __name__ == '__main__':

    pygame.init()
    print("Initializing")

    screen = pygame.display.set_mode((windowWidth, windowHeight))
    map = create_map()

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(black)

        for x in range(mapWidth):
            for y in range(mapHeight):
                #for each subsurface in the map, blit it to the screen.
                tile = map[x][y]
                screen.blit(tile, (x*tilesize, y*tilesize))

        screen.display.flip() 

当我运行代码时,它给了我这个错误:

Traceback (most recent call last):
  File "C:\Users\dementeddr\workspace\TheWaterIsRising\src\default\RisingMain.py", line 59, in <module>
    screen.display.flip() 
AttributeError: 'pygame.Surface' object has no attribute 'display'

我已经用Google搜索过,我发现了很多其他属性错误,但没有关于“显示”的信息。属性。我做错了什么?

1 个答案:

答案 0 :(得分:1)

错误消息告诉您需要知道的一切:

Traceback (most recent call last):
  File "C:\Users\dementeddr\workspace\TheWaterIsRising\src\default\RisingMain.py", line 59, in <module>
    screen.display.flip() 

上面的部分显示了问题发生的确切代码行screen.display.flip()

AttributeError: 'pygame.Surface' object has no attribute 'display'

screen属于pygame.Surface类型,它没有display属性,因此出了问题。查看http://www.pygame.org/docs/tut/intro/intro.html等教程会显示您应该调用pygame.display.flip()。尝试替换该行并查看它是否运行。

祝你好运:)