标题:照着书上打的,错误在哪里?
只看楼主
zcl2988
Rank: 2
等 级:论坛游民
帖 子:3
专家分:14
注 册:2020-2-21
结帖率:0
已结贴  问题点数:20 回复次数:1 
照着书上打的,错误在哪里?
import pygame
import sys
from settings import Settings
def run_game():
    #初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings= Settings()
    print (ai_settings.screen_width)
    ai_settings.screen_height=80
    #ai_settings.screen_width,  ai_settings.screen_height
    screen = pygame.display.set_mode((ai_settings.screen_width,  ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    #设置背景色
    bg_color = (30,100,230)
    #开始游戏的主循环
    while True:
        #监视键盘和鼠标
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        #每次循环是都绘制屏幕
        #screen.fill(ai_settings.bg_color)
        #让最近绘制的屏幕可见
        pygame.display.flip()
run_game()
#####################################################
class Settings():
    """存储《外星人入侵》的所有设置的类"""
    def _init_(self):
        """初始化游戏的设置"""
        #屏幕设置
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)
        print (screen_width)
        print (1221)
print(Settings.screen_width)
#############################################################################
pygame 1.9.6
Hello from the pygame community. https://www.
Traceback (most recent call last):
  File "E:/python/game_fly/alien_invasion.py", line 23, in <module>
    run_game()
  File "E:/python/game_fly/alien_invasion.py", line 9, in run_game
    screen = pygame.display.set_mode((ai_settings.screen_width,  ai_settings.screen_height))
AttributeError: 'Settings' object has no attribute 'screen_width'
>>>
======================================================================= RESTART: E:/python/game_fly/alien_invasion.py ======================================================================
pygame 1.9.6
Hello from the pygame community. https://www.

======================================================================= RESTART: E:/python/game_fly/alien_invasion.py ======================================================================
pygame 1.9.6
Hello from the pygame community. https://www.
Traceback (most recent call last):
  File "E:/python/game_fly/alien_invasion.py", line 25, in <module>
    run_game()
  File "E:/python/game_fly/alien_invasion.py", line 8, in run_game
    print (ai_settings.screen_width)
AttributeError: 'Settings' object has no attribute 'screen_width'
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
122121
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
Traceback (most recent call last):
  File "E:/python/game_fly/settings.py", line 11, in <module>
    print(screen_width)
NameError: name 'screen_width' is not defined
>>>
========================================================================== RESTART: E:/python/game_fly/settings.py =========================================================================
Traceback (most recent call last):
  File "E:/python/game_fly/settings.py", line 11, in <module>
    print(Settings.screen_width)
AttributeError: type object 'Settings' has no attribute 'screen_width'
>>>
搜索更多相关主题的帖子: print File line pygame python 
2020-02-21 19:24
迷小弟
Rank: 2
来 自:内蒙古鄂尔多斯东胜
等 级:等待验证会员
帖 子:12
专家分:20
注 册:2020-2-21
得分:20 
你的版本可能和书不一样,还有你好像没打完程序。

时光逝之者之转世,python有成者之迷弟;愿为python之有成者之向往,不可技未成变傲娇
2020-02-21 19:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-499333-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057681 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved