python外星人学习报错求助
本帖最后由 Antion 于 2019-11-25 17:32 编辑python外星人学习过程中报错,卡主了找了很久没有找到问题所在和解决办法 求大神帮助,BBS不太会排版 辛苦大神帮帮找找原因
报错内容
C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/fly/alien_invasion.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:/fly/alien_invasion.py", line 32, in <module>
run_game()
File "D:/fly/alien_invasion.py", line 23, in run_game
gf.create_fleet(ai_settings, screen, aliens)
File "D:\fly\game_functions.py", line 77, in create_fleet
available_space_x = ai_settings.screen_width - 2 * alien_width
AttributeError: 'Settings' object has no attribute 'screen_width'
Process finished with exit code 1
下面是各个文件
alinen_invasion.pyimport pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
"""1"""
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.sreen_width, ai_settings.sreen_height))
# 翻译::display 陈列,列出 set 设置 mode 模式↓caption 标题
pygame.display.set_caption("Alien Invasion")
ship = Ship(ai_settings, screen)
# 创建一个用于存储子弹的编组和飞船族
bullets = Group()
aliens = Group()
# alien
gf.create_fleet(ai_settings, screen, aliens)
# 循环
while True:
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings, screen, ship, aliens, bullets)
run_game()
game_function.py
import sys
# 游戏功能
import pygame
from bullet import Bullet
from alien import Alien
import settings
def check_keydown_events(event, ai_settings, screen, ship, bullets):
"""down"""
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)
elif event.key == pygame.K_q:
sys.exit()
def fire_bullet(ai_settings, screen, ship, bullets):
# 创建一颗子弹,并将其加入到编组bullets中
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
def check_keyup_events(event, ship):
"""up"""
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings, screen, ship, bullets):
"""XIANGYING"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, ship, bullets)
# 松开的键如果是k-right船类右移标记为假
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
def update_bullets(bullets):
bullets.update()
# 删除已消失的子弹
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
def update_screen(ai_settings, screen, ship, aliens, bullets):
"""更新窗口属性设置,窗口,船的图像,并刷新到屏幕上"""
screen.fill(ai_settings.bg_color)
# 在飞船和外星人后面重绘所有子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
aliens.draw(screen)
pygame.display.flip()
def create_fleet(ai_settings, screen, aliens):
"""创建外星人群"""
# 创建一个外星人 并计算一行能放多少个
# 外星人间距为外星人宽度
alien = Alien(ai_settings, screen)
alien_width = alien.rect.width
available_space_x = ai_settings.screen_width - 2 * alien_width
number_aliens_x = int(available_space_x / (2 * alien_width))
# 创建第一行外星人
for alien_number in range(number_aliens_x):
# 创建一个外星人并将其加入当前行
alien = Alien(ai_settings, screen)
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect_.x = alien.x
aliens.add(alien)
alien.py
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""表示单个外星人的类"""
# 由于没有缩减 类下面的函数没有正确ctrl+shift+alt+L
def __init__(self, ai_settings, screen):
super(Alien, self).__init__()
self.screen = screen
self.ai_settings = ai_settings
# 加载外星人图像,并设置其rect属性
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# 每个外星人最初都在屏幕左上角?
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# 存储外星人的准确位置
self.x = float(self.rect.x)
def blitme(self):
"""在指定位置绘制外星人"""
self.screen.blit(self.image, self.rect)
ship.py
import pygame
class Ship():
"""船:ship screen窗口"""
def __init__(self, ai_settings, screen):
"""初始化飞船并设置其初始化"""
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect =screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.center = float(self.rect.centerx)
self.moving_right = False
self.moving_left = False
def update(self):
"""根据标志调整飞机位置"""
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
self.rect.centerx = self.center
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""一个对飞船发射子弹进行管理的类"""
def __init__(self, ai_settings, screen, ship):
"""在船位置创建一个子弹对象"""
super(Bullet, self).__init__()
self.screen = screen
# 在(0,0)处创建一个表示子弹的矩形,在设置正确的位置
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
# 存储用小数表示的子弹位置
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
"""向上移动"""
# 更新表示子弹位置的小数值
self.y -= self.speed_factor
# 更新表示子弹的rect的位置
self.rect.y = self.y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen, self.color, self.rect)
steeings.py
class Settings():
"""存储alien invasion的所有设置的类"""
def __init__(self):
"""初始化游戏的设置"""
self.sreen_width = 1200
self.sreen_height = 800
self.bg_color = (230, 230, 230)
self.ship_speed_factor = 1.5
# 子弹设置
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = 60, 60, 60
self.bullets_allowed = 3
sreen 和screen的区别? 本帖最后由 人生苦短丶 于 2019-11-25 18:11 编辑
错误的原因就是ai_setting没有screen_width 这个属性,然后你回顾一下setting.py 仔细查看一下你写的代码,你就知道为什么了。 人生苦短丶 发表于 2019-11-25 18:10
错误的原因就是ai_setting没有screen_width 这个属性,然后你回顾一下setting.py 仔细查看一下你写的代 ...
通过提示往这方便找了,没找到原因才求助的 class Settings():
"""存储alien invasion的所有设置的类"""
def __init__(self):
"""初始化游戏的设置"""
self.sreen_width = 1200 //sreen_width改成screen_width
self.sreen_height = 800 //sreen_height 改成screen_height 5楼说的对,sreen改成screen 如果以前能运行就把available_space_x = ai_settings.screen_width - 2 * alien_width里的screen改成sreen我也在学比你进度稍微快一点 Antion 发表于 2019-11-25 18:49
通过提示往这方便找了,没找到原因才求助的
是不是我提示的太隐晦了........原因就是你单词写错了.....哈哈哈
页:
[1]