问题
I am creating my first game in python and was doing it part by part. Than I got this error message:
AttributeError: 'pygame.Surface' object has no attribute 'event'
My code:
import pygame
pygame.init()
screen_width = 800
screen_height = 600
pygame = pygame.display.set_mode([screen_width,screen_width])
gameover = False
while not gameover:
for event in pygame.event.get():
print(event)
回答1:
Because the module pygame is shadowed by the variable pygame that refers to the display Surface object. You have to rename the variable that holds the Surface object which is associated to the Pygame display:
pygame = pygame.display.set_mode([screen_width,screen_width])
pygame_surf = pygame.display.set_mode([screen_width,screen_width])
Note that when pygame.event.get() is called, pygame is understood as the Surface object pygame and a Surface object does not have an attribute event.
来源:https://stackoverflow.com/questions/65569598/attributeerror-pygame-surface-object-has-no-attribute-event