首页 > 解决方案 > AttributeError:“pygame.Surface”对象没有属性“事件”

问题描述

我正在用 python 创建我的第一个游戏,并且正在逐步完成它。比我收到此错误消息:

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

我的代码:

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)

标签: pythonpygame

解决方案


因为模块被引用显示Surface对象pygame的变量所遮蔽。您必须重命名保存与 Pygame 显示关联的Surface对象的变量:pygame

pygame = pygame.display.set_mode([screen_width,screen_width])

pygame_surf = pygame.display.set_mode([screen_width,screen_width])

注意,当pygame.event.get()被调用时,pygame被理解为Surface对象pygame,并且Surface对象没有属性event


推荐阅读