alien_invasion:
import pygame
import sys
from time import sleep
from settings import Settings
from game_stats import GameStats
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
#self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))#利用settings类进行设置游戏窗口
pygame.display.set_caption("Alien Invasion") #set_caption设置窗口的标题
self.stats = GameStats(self)
self.ship = Ship(self)
self.bullet = pygame.sprite.Group()
self.alien = pygame.sprite.Group()
self._create_fleet()
def run_game(self):
while True: #控制游戏的循环
self._check_events()
if self.stats.game_active:
self.ship.update()
self._update_bullets()
self._update_aliens()
self._update_screen()
def _update_aliens(self):
self._check_fleet_edges()
self.alien.update()
if pygame.sprite.spritecollideany(self.ship,self.alien):
self._ship_hit()
self._check_aliens_bottom()
def _ship_hit(self):
if self.stats.ship_left > 0:
self.stats.ship_left -= 1
self.alien.empty()
self.bullet.empty()
self._create_fleet()
self.ship.center_ship()
sleep(0.5)
else:
self.stats.game_active = False
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.alien.add(alien)
def _check_fleet_edges(self):
for alien in self.alien.sprites():
if alien.check_edges():
self._change_fleet_direction()
break
def _change_fleet_direction(self):
for alien in self.alien.sprites():
alien.rect.y += self.settings.fleet_drop_speed
self.settings.fleet_direction *= -1
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_aliens_bottom(self):
screen_rect = self.screen.get_rect()
for alien in self.alien.sprites():
if alien.rect.bottom >= screen_rect.bottom:
self._ship_hit()
break
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: #游戏中按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.bullet) < self.settings.bullet_allowed:
new_bullet = Bullet(self)
self.bullet.add(new_bullet)
def _update_bullets(self):
self.bullet.update()
for bullet in self.bullet.copy():
if bullet.rect.bottom <= 0:
self.bullet.remove(bullet)
self._check_bullet_alien_collisions()
def _check_bullet_alien_collisions(self):
collections = pygame.sprite.groupcollide(self.bullet,self.alien,True,True)
if not self.alien:
self.bullet.empty()
self._create_fleet()
def _update_screen(self):
self.screen.fill(self.settings.bg_color) # 每次循环时都重绘屏幕
self.ship.blitme()
for bullet in self.bullet.sprites():
bullet.draw_bullet()
self.alien.draw(self.screen)
pygame.display.flip() #更新surface对象
if __name__ == '__main__':
ai = AlienInvasion()
ai.run_game()
game_stats:
class GameStats:
def __init__(self,ai_game):
self.settings = ai_game.settings
self.reset_stats()
self.game_active = True
def reset_stats(self):
self.ship_left = self.settings.ship_limit
alien:
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
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):
self.x += (self.settings.alien_speed * self.settings.fleet_direction)
self.rect.x = self.x
settings:
class Settings:
def __init__(self):
self.screen_width = 1200 #游戏屏幕的设置
self.screen_height = 700
self.bg_color = (230,230,230)
self.ship_speed = 1.5
self.ship_limit = 3
self.bullet_speed = 1.5 #子弹的设置
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60,60,60)
self.bullet_allowed = 3
self.alien_speed = 1.0 #设置外星人的移动
self.fleet_drop_speed = 10
self.fleet_direction = 1 #direction为1表示右移,-1表示左移
bullet:
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):
self.y -= self.settings.bullet_speed
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen,self.color,self.rect)
ship:
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.centerx = 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.centerx += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.centerx -= self.settings.ship_speed
self.rect.centerx = self.centerx
def blitme(self):
self.screen.blit(self.image,self.rect)#在指定位置绘制飞船
def center_ship(self):
self.rect.midbottom = self.screen_rect.midbottom
self.centerx = float(self.rect.centerx)
|