python - 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)
解决方案
因为模块被引用显示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
。
推荐阅读
- c# - 我有 json 数据并想在 Asp.net 中的 .cshtm 视图上显示
- html - 传单控件未垂直居中于 mac
- asynchronous - 测试 Mocha/supertest/expect MEAN-stack HTTP Request 4 秒延迟完成
- mqtt - mqtt nodejs模块的蚊子持久性问题
- orientation - 如何在用户定义的设置中设置支持的界面方向?
- symfony - Symfony,在控制器中强制注销
- python - 解析投注网站的 HTML (ValueError: No tables found)
- node.js - 同一网址上的 CORS 问题
- javascript - 如何在footable表中显示未找到记录错误消息?
- python - cifar10 数据集上的 CNN 模型似乎没有经过训练