<<python 编程 入门到实践>>一书例子:外星人入侵。
飞船左右移动做完,到子弹部分,总是提示:
AttributeError: 'pygame.Surface' object has no attribute 'bullet_width'
查不出错误所在。。。
代码:
alien_invasion.py
import sys
import pygame
from setting import Settings
from ship import Ship
from bullet import Bullet
from pygame.sprite import Group
import game_functions as gf
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("外星人入侵")
ship = Ship(ai_settings, screen)
# make a group for loading the buttlers
bullets = Group()
while True:
gf.check_events(ai_settings, screen, ship, bullets)
bullets.update()
gf.update_screen(ai_settings, screen, ship, bullets)
ship.update()
bullets.update()
run_game()
setting.py
class Settings():
"""存储所有设置"""
def __init__(self):
"""初始化设置"""
# 屏幕设置
self.screen_width = 1200
self.screen_height = 600
self.bg_color = (230,230,230)
self.ship_speed = 1.5
# 子弹设置
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height =15
self.bullet_color = 60,60,60
bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""a class who manage the bullets to ship"""
def __init__(self, ai_settings, screen, ship):
"""在飞船位置创建一个子弹对象"""
super().__init__()
self.screen = screen
# make a rect in (0,0),then move to correct location
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
# load the location by float
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self,):
"""move up bullets"""
# update float for the location of the bullet
self.y -= self.speed_factor
self.rect.y = self.y
def draw_bullet(self):
"""draw bullets on screen"""
pygame.draw.rect(self.screen, self.color, self.rect)
game_funcations.py
import sys
import pygame
from bullet import Bullet
def update_screen(ai_settings, screen, ship, bullets):
"""更新屏幕"""
screen.fill(ai_settings.bg_color)
ship.blitme()
# 让最新绘制的屏幕可见
pygame.display.flip()
# draw bullets
for bullet in bullets.sprites():
bullet.draw_bullet()
def check_keyup(event, ship ):
if event.key == pygame.K_RIGHT:
# 向右移动飞船
ship.moving_right = False
elif event.key == pygame.K_LEFT:
# 向左移动飞船
ship.moving_left = False
def check_keydown(event, ai_settings, screen, ship, bullets):
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:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
def check_events(ship, ai_settings,screen,bullets):
"""响应按键和鼠标"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_keyup(event, ship)
ship.py
import pygame
class Ship():
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.moving_right = False
self.moving_left = False
# 浮点数位置
self.center = float(self.rect.centerx)
def update(self):
"""根据移动标识调整飞船位置"""
if self.moving_right == True and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed
if self.moving_left == True and self.rect.left > self.screen_rect.left:
self.center -= self.ai_settings.ship_speed
self.rect.centerx =self.center
def blitme(self):
"""指定位置绘制飞船"""
self.screen.blit(self.image,self.rect)
gf.check_events(ai_settings, screen, ship, bullets
和def check_events(ship, ai_settings,screen,bullets):
实参顺序写错了