seven20_22 2021-11-28 22:58 采纳率: 100%
浏览 92
已结题

在编写外星人入侵时,遇到AttributeError: __enter__错误,不知道什么原因

问题遇到的现象和发生背景

在编写外星人小游戏时遇到AttributeError: __enter__错误,不适用类函数就时正常的,但是已将with true放在类中就报错.AttributeError: enter,不知道是什么原因


```python
import sys

import pygame

from settings import Settings

class AlienInvasion:
    """管理游戏资源和行为的类"""
    def __init__(self):
        """初始化游戏并创建游戏资源"""
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

    def run_game(self):
        """开始游戏主循环"""
        with True:
            """监视键盘和鼠标"""
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
        """让最近绘制的屏幕可见"""
        self.screen.fill(self.settings.bg_color)
        pygame.display.flip()
if __name__ == '__main__':
    """创建游戏实例并运行游戏."""
    ai = AlienInvasion()
    ai.run_game()


```python
pygame 2.1.0 (SDL 2.0.16, Python 3.10.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:\练习册\随手练习\外星人入侵游戏\alinen_invasion.py", line 31, in <module>
    ai.run_game()
  File "D:\练习册\随手练习\外星人入侵游戏\alinen_invasion.py", line 20, in run_game
    with True:
AttributeError: __enter__

  • 写回答

1条回答 默认 最新

  • 关注

    你的with 应该是while

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月7日
  • 已采纳回答 11月29日
  • 创建了问题 11月28日

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊