12-5 侧面射击:编写一个游戏,将一艘飞船放置在屏幕左边,并允许玩家上下移动飞船。在玩家按空格键时,让飞船发射一颗在屏幕中央向右穿行的子弹,并在子弹离开屏幕而消失后将其删除。
这个题和书上的程序类似,但自己写的时候还是遇到了许多问题,磕磕碰碰实现了。
主程序:game
import pygame
from settings import Settings
import func as f
from ship import Ship
from bullet import Bullet
from pygame.sprite import Group
def run_game():
pygame.init()
settings = Settings()
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
pygame.display.set_caption("Side Shoot")
ship = Ship(screen)
bullets = Group()
while True:
f.check_keys(settings, screen, ship, bullets)
ship.update()
bullets.update()
#删除已消失的子弹
for bullet in bullets.copy():
if bullet.rect.left > screen.get_rect().right:
bullets.remove(bullet)
f.update_screen(settings, screen, ship, bullets)
run_game()
Settings类:
import pygame
class Settings():
"""所有设置的类"""
def __init__(self):
#设置屏幕的属性
self.screen_width = 1200
self.screen_height = 600
self.bg_color = (230, 230, 230)
#设置子弹属性
self.bullet_speed_factor = 1
self.bullet_width = 5
self.bullet_height = 5
self.bullet_color = 0, 0, 0
Ship类:
import pygame
class Ship():
def __init__(self, screen):
"""设置飞船的属性"""
self.screen = screen#EEEE:忘记写
self.image = pygame.image.load('ship.bmp')#读取飞船图片
self.image = pygame.transform.rotate(self.image, -90)#将飞船顺时针旋转90°
self.rect = self.image.get_rect()#获取飞船图片外接矩形
self.screen_rect = screen.get_rect()#获取屏幕的外接矩形
#设置飞船的位置,将飞船放置在屏幕左侧中间
self.rect.centery = self.screen_rect.centery
self.rect.left = self.screen_rect.left
#初始化飞船上下移动属性
self.moving_up = False
self.moving_down = False
def update(self):
"""更新飞船位置"""
if self.moving_up and self.rect.top > 0:
self.rect.centery -= 1
elif self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.rect.centery += 1
def blitme(self):
"""绘制飞船"""
self.screen.blit(self.image, self.rect)
Bullet类:
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""一个对子弹进行管理的类"""
def __init__(self, settings, screen, ship):
"""在飞船出创建一个子弹对象"""
#继承父类的属性
super().__init__()
self.screen = screen
#在(0, 0)处创建一个子弹的矩形,再设置其准确位置
self.rect = pygame.Rect(0, 0, settings.bullet_width, settings.bullet_height)
self.rect.centery = ship.rect.centery
self.rect.right = ship.rect.right
#存储用小数表示的子弹位置
self.x = float(self.rect.x)
self.color = settings.bullet_color
self.speed_factor = settings.bullet_speed_factor
def update(self):
"""更新子弹位置"""
self.x += self.speed_factor
self.rect.x = self.x
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen, self.color, self.rect)
函数模块func:
import pygame
import sys
from ship import Ship
from bullet import Bullet
def check_keys(settings, screen, ship, bullets):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keys_down(event, settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_keys_up(event, ship)
def check_keys_down(event,settings, screen, ship, bullets):
if event.key == pygame.K_UP:
ship.moving_up = True
if event.key == pygame.K_DOWN:
ship.moving_down = True
elif event.key == pygame.K_SPACE:
#创建一颗子弹,并加入到Group编组中
new_bullet = Bullet(settings, screen, ship)
bullets.add(new_bullet)
def check_keys_up(event,ship):
if event.key == pygame.K_UP:
ship.moving_up = False
if event.key == pygame.K_DOWN:
ship.moving_down = False
def update_screen(settings, screen, ship, bullets):
screen.fill(settings.bg_color)
ship.blitme()
for bullet in bullets.sprites():
bullet.draw_bullet()
pygame.display.flip()
效果图:
这子弹模型让我想起了小时候掌机上一个像弹乒乓球的游戏,呵呵!
问题与总结:
1.初始化Ship类时,忘记写self.screen = screen
2.当按住↑键时,飞船直接到了屏幕顶部,经检查是在check_keys_down()中,moving_up拼写出错
3.对编组bullets中的bullet使用for循环时,应写成for bullet in bullet.sprites():
4.各处形参传递顺序应一致,本次因为没注意一直出现 AttributeError,值得注意