手把手讲解超详细python入门游戏项目‘打外星飞船’(四)
在经过创立屏幕、飞船移动和设置子弹,我们这里开始设置外形人的创建和移动。我们这里主要的任务是:创建一众外星人让它们充满屏幕,让他们向下和两边移动,这里继承了上面四个文件外我们还多键一个文件来管理外星人的类,也就是五个文件。这里后三个文件没有太大的变化,我们主要调整了前两个文件
主文件
import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
from alien import Alien
class AlienInvasion:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._creat_fleet()
def run_game(self):
while True:
self._check_events()
self.ship.update()
self._update_bullets()
#这里多加了外星人的运动函数
self._update_aliens()
self._update_screen()
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self, event):
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_SPACE:
self._fire_bullet()
def _check_keyup_events(self, event):
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
if len(self.bullets) < self.settings.bullets_allowed:
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _update_bullets(self):
self.bullets.update()
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
def _update_aliens(self):
"""
判断外星人有没有到边,到了就调整位置
"""
self._check_fleet_edges()
self.aliens.update()
def _create_fleet(self):
"""用来创建外星人"""
# 计算出一行可以容纳多少外星人
alien = Alien(self)
alien_width, alien_height = alien.rect.size
available_space_x = self.settings.screen_width - (2 * alien_width)
number_aliens_x = available_space_x // (2 * alien_width)
#计算出一列可以容纳多少外星人
ship_height = self.ship.rect.height
available_space_y = (self.settings.screen_height -
(3 * alien_height) - ship_height)
number_rows = available_space_y // (2 * alien_height)
# 这里用了一个嵌套循环,外部是列,内部是行,创建多个外形人
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
self._create_alien(alien_number, row_number)
def _create_alien(self, alien_number, row_number):
"""创建一个外星人,放在一行里面"""
alien = Alien(self)
alien_width, alien_height = alien.rect.size
#总宽度是所有外星人的宽度加间隔,间隔也取外星人的宽度乘上总行数
alien.x = alien_width + 2 * alien_width * alien_number
#要创建的外星人的数
alien.rect.x = alien.x
#总高度是所有外星人的高加间隔,间隔也取外星人的宽度,乘上总列数
alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
self.aliens.add(alien)
def _check_fleet_edges(self):
"""这里判断外星人到底屏幕底部没有"""
for alien in self.aliens.sprites():
#遍历每个外星人
if alien.check_edges():
self._change_fleet_direction()
#如果到了就退出循环
break
def _change_fleet_direction(self):
"""将外星人下移加左右飘"""
for alien in self.aliens.sprites():
alien.rect.y += self.settings.fleet_drop_speed
self.settings.fleet_direction *= -1
def _update_screen(self):
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
外星人控制
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""管理的外星人的类"""
def __init__(self, ai_game):
"""初始化外星人并设置他们初始位置"""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.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 check_edges(self):
#判断是否超过屏幕
"""看看飞船有没有超过屏幕,如果屏幕大于右边的屏幕的数后者小于零是超越屏幕的左边"""
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right or self.rect.left <= 0:
return True
def update(self):
"""这里用乘积的办法,每次就是用所在的x坐标减去方向*1or-1"""
self.x += (self.settings.alien_speed *
self.settings.fleet_direction)
self.rect.x = self.x
子弹管理类
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
def __init__(self, ai_game):
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color = self.settings.bullet_color
self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
self.settings.bullet_height)
self.rect.midtop = ai_game.ship.rect.midtop
self.y = float(self.rect.y)
def update(self):
# Update the decimal position of the bullet.
self.y -= self.settings.bullet_speed
# Update the rect position.
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect)
总设置参数
class Settings:
"""这个类用于储存一些参数"""
def __init__(self):
# 屏幕参数
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
# 飞船参数
self.ship_speed = 1.5
# 子弹参数
self.bullet_speed = 1.0
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60, 60, 60)
self.bullets_allowed = 3
# 外星人左右移动的速度1,向下移动的速度10
self.alien_speed = 1.0
self.fleet_drop_speed = 10
# 这里其实是在设置每次外形人飞船的位置,1为右移一位,-1为左移
self.fleet_direction = 1
飞船设置
import pygame
class Ship:
"""飞船参数"""
def __init__(self, ai_game):
self.screen = ai_game.screen
self.settings = ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.rect.midbottom = self.screen_rect.midbottom
self.x = float(self.rect.x)
self.moving_right = False
self.moving_left = False
def update(self):
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
self.rect.x = self.x
def blitme(self):
self.screen.blit(self.image, self.rect)