微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError: 'pygame.Surface' 对象没有属性 'event'

如何解决AttributeError: 'pygame.Surface' 对象没有属性 'event'

我正在用 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)

解决方法

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

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

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

请注意,当调用 pygame.event.get() 时,pygame 被理解为 Surface 对象 pygameSurface 对象不是有一个属性 event

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。