AttributeError: 'pygame.Surface' 对象没有属性 'event' [英] AttributeError: 'pygame.Surface' object has no attribute 'event'

查看:189
本文介绍了AttributeError: 'pygame.Surface' 对象没有属性 'event'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

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

我的代码:

导入pygamepygame.init()屏幕宽度 = 800屏幕高度 = 600pygame = pygame.display.set_mode([screen_width,screen_width])游戏结束 = 错误虽然不是游戏结束:对于 pygame.event.get() 中的事件:打印(事件)

解决方案

因为模块 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.

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)

解决方案

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.

这篇关于AttributeError: 'pygame.Surface' 对象没有属性 'event'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆