KEVIN___durant 2022-02-23 20:04 采纳率: 55.6%
浏览 16

python中有关类的属性的问题,请帮我看看哪里导致编译错误

#主程序
import sys

import pygame
from settings import Settings
from ship import Ship

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(ai_settings.screen_width,ai_settings.screen_height)  
    pygame.display.set_caption("Alien Invasion")
    
    """buiding a ship"""         
    ship = Ship(screen)        

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        screen.fill(ai_settings.bg_color)
        ship.blitme()
        pygame.display.flip()

run_game()


#设置类,说我类中没有相应属性
class Settings():
    def _init_(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)

运行报错原因:

Traceback (most recent call last):
  File "E:\Alien_invasion\alien_invasion.py",  in <module>
    run_game()
  File "E:\Alien_invasion\alien_invasion.py",in run_game
    screen = pygame.display.set_mode(ai_settings.screen_width,ai_settings.screen_height)
AttributeError: 'Settings' object has no attribute 'screen_width'

我明明定义了了属性'screen_width',为什么还报错呢?

  • 写回答

1条回答 默认 最新

  • 星浩前进中 2022-02-23 20:48
    关注
    
    ai_settings.screen_width,ai_settings.screen_height    
    这里改成下面的代码试下
    ai_settings.self.screen_width,ai_settings.self.screen_height
    
    评论

报告相同问题?

问题事件

  • 创建了问题 2月23日

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题