AttributeError:'int'对象没有属性'KEYDOWN'

时间:2013-08-22 02:10:19

标签: python events pygame keydown

我对python比较陌生,而且我一直在搞乱PyGame ......对我来说,这段代码看起来不错:

import pygame

class Game(object):
    def main(self, screen):
        image = pygame.image.load("images\player.png")
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = false
                if event.type.KEYDOWN and event.key == pygame.K.ESCAPE:
                    running = false

            screen.fill((200,200,200))
            screen.blit(image, (320, 240))
            pygame.display.flip()

if __name__ == "__main__":
    pygame.init()
    screen=pygame.display.set_mode((640,480))
    Game().main(screen)

然而,当我运行它时,我收到此错误:

File "C:\Users\jwquick\Documents\Programming\Python\Pygame Test\main.py", line
 10, in main
    if event.type.KEYDOWN and event.key == pygame.K.ESCAPE:
AttributeError: 'int' object has no attribute 'KEYDOWN'

2 个答案:

答案 0 :(得分:1)

from pygame.locals import *

# ...

for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            print("Quit")

答案 1 :(得分:0)

尝试:

if event.type==KEYDOWN and event.key == pygame.K.ESCAPE: