pygame.QUIT未定义

时间:2019-07-16 12:31:53

标签: python pygame

以下是我的代码:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.event.quit()

我正在使用PyCharm,它突出显示QUIT并说Cannot find reference 'QUIT' in '__init__.py'。该程序无法正常运行。

编辑:下面的代码。 K_LEFT或其他键也显示相同的错误

import pygame
def move(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.event.quit()
        keys = pygame.key.get_pressed()
        for key in keys:
            if keys[pygame.K_LEFT]:
                self.dirnx = -1
                self.dirny = 0
                self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
            elif keys[pygame.K_RIGHT]:
                self.dirnx = 1
                self.dirny = 0
                self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
            elif keys[pygame.K_UP]:
                self.dirnx = 0
                self.dirny = -1
                self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]
            elif keys[pygame.K_DOWN]:
                self.dirnx = 0
                self.dirny = 1
                self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

错误:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:/Users/HasanIsgandarli/PycharmProjects/untitled/Snake.py", line 151, in <module>
    main()
  File "C:/Users/HasanIsgandarli/PycharmProjects/untitled/Snake.py", line 147, in main
    s.move()
  File "C:/Users/HasanIsgandarli/PycharmProjects/untitled/Snake.py", line 56, in move
    pygame.event.quit()
AttributeError: module 'pygame.event' has no attribute 'quit'

Process finished with exit code 1```

1 个答案:

答案 0 :(得分:2)

将其更改为

if event.type == pygame.QUIT: pygame.display.quit() pygame.quit()