pygame.退出未定义

2024-10-05 10:17:38 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我的代码:

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

我用的是PyCharm,它突出显示了QUIT并说Cannot find reference 'QUIT' in '__init__.py'。这个程序没有像预期的那样工作。你知道吗

编辑:代码如下。KU 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```

Tags: inpyposselfeventforgetkeys

热门问题