鱼C论坛

 找回密码
 立即注册
查看: 3616|回复: 8

游戏代码问题 按照书上敲得 但就是报错

[复制链接]
发表于 2019-11-13 20:43:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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")
    #创建一艘飞船
    ship = Ship(screen)


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



class Settings:
    def _int_(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)



Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
============ RESTART: C:\Users\asus\Desktop\外星人入侵\alien_invasion.py ============
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\asus\Desktop\外星人入侵\alien_invasion.py", line 31, in <module>
    run_game()
  File "C:\Users\asus\Desktop\外星人入侵\alien_invasion.py", line 12, in run_game
    (ai_settings.screen_width,ai_settings.screen_height))
AttributeError: 'Settings' object has no attribute 'screen_width'
>>>



我按照书上的的代码打的为什么一直有问题


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-13 21:12:41 | 显示全部楼层
class Settings:
    def _int_(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)


应该是 __init__,不是 _int_。

  1. import sys
  2. import pygame
  3. from settings import Settings
  4. from ship import Ship

  5. def run_game():
  6.     pygame.init()
  7.     ai_settings = Settings()
  8.     screen = pygame.display.set_mode(
  9.         (ai_settings.screen_width,ai_settings.screen_height))
  10.    
  11.     pygame.display.set_caption("Alien Invasion")
  12.     #创建一艘飞船
  13.     ship = Ship(screen)


  14.     while True:
  15.         for event in pygame.event.get():
  16.             if event.type==pygame.QUIT:
  17.                 sys.exit()
  18.         screen.fill(be_color)
  19.         screen.fill(ai_settings.bg_color)
  20.         ship.blite()
  21.         pygame.display.flip()
  22.         
  23. run_game()



  24. class Settings:
  25.     def __init__(self):
  26.         self.screen_width = 1200
  27.         self.screen_height = 800
  28.         self.bg_color = (230,230,230)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-13 21:35:06 | 显示全部楼层
zltzlt 发表于 2019-11-13 21:12
class Settings:
    def _int_(self):
        self.screen_width = 1200

还是报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 21:37:01 | 显示全部楼层

Ship 类呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-13 21:50:29 | 显示全部楼层

ship没问题 还是同样的错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 22:29:56 | 显示全部楼层
cxcxcxcx 发表于 2019-11-13 21:50
ship没问题 还是同样的错误

改了 Settings 类吗?

  1. class Settings:
  2.     def __init__(self):
  3.         self.screen_width = 1200
  4.         self.screen_height = 800
  5.         self.bg_color = (230,230,230)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 22:31:36 | 显示全部楼层
要不别用import setting了,直接继承settings....哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 22:31:47 | 显示全部楼层
要不别用import setting了,直接继承settings....哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-14 07:28:05 | 显示全部楼层
zltzlt 发表于 2019-11-13 22:29
改了 Settings 类吗?

改了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-20 18:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表