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

社会主义新天地 提交于 2021-02-05 08:01:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!