此作业的要求参见:https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11577
语言:python
作业要求:
游戏名称 | 满分分值 | 功能点提示 |
---|---|---|
飞机大战 | 50 | 移动飞机、发射子弹、敌机移动、消灭敌机、被敌机撞击、存档读档 |
1.coding地址:https://e.coding.net/xucancan1/plane_war/plane_war.git , 主运行文件:main.py
2.项目PSP
类型 | 任务 | 开始时间 | 结束时间 | 中断时间(min) | delta时间 |
分析 | 分析各个部分之间的联系 | 2020.11.28 21:32 | 2020.11.29 00:23 | 0min | 171min |
编程 | 编写设计屏幕和英雄飞机类,实现英雄飞机能够左右移动 | 2020.11.30 21:28 | 2020.12.01 00:30 | 0min | 182min |
编程 | 编写英雄飞机的普通子弹和超级子弹 | 2020.12.01 21:30 | 2020.12.01 23:32 | 0min | 122min |
编程 | 编写各种敌机类 | 2020.12.02 21:30 | 2020.12.02 00:05 | 0min | 155min |
编程 | 编写英雄飞机击打敌机的函数 | 2020.12.03 21:22 | 2020.12.03 23:02 | 0min | 100min |
编程 | 编写设计游戏的计分和等级 | 2020.12.04 21:20 | 2020 .12.05 00:13 | 0min | 173min |
编程 | 调试 | 2020.12 .05 21:33 | 2020. 12.06 00:10 | 0min | 157min |
编程 | 编写显示最高分记录和英雄飞机的生命条数显示 | 2020.12.06 21:30 | 2020.12.07 00:12 | 0min | 162min |
编程 | 整体调试,和界面优化 | 2020.12.07 15:28 | 2020.12.07 22:10 | 32min | 370min |
写文档 | 写文档 | 2020.12.08 15:30 | 2020.12.08 16:23 | 0min | 53min |
3.项目WBS
4.主类的大概逻辑
5.主要关键技术
该项目引入了pygame这个包
主类main的代码:
import sys import pygame from settings import Settings import game_functions as gf from pygame.sprite import Group #引入动作物的组 from alien import Alien #导入ship类 from ship import Ship from game_stats import Gamestats from button import Button from scoreboard import Scoreboard from alien import Alien import random as r def run_game(): pygame.init() #创建所有设置对象 ai_settings =Settings() #创建存储游戏统计信息的实例,并创建记分牌 stats =Gamestats(ai_settings) #游戏的状态 #创建屏幕实例 screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_heigh)) sb = Scoreboard(ai_settings, screen,stats) #创建积分板的实例 #创建play按钮实例 play_button =Button(ai_settings,screen,"play") pygame.display.set_caption("alien invasion") #创建一个飞船 ship =Ship(ai_settings,screen) bullets = Group() aliens = pygame.sprite.RenderUpdates() if stats.game_active: #开始时只有游戏活动状态才能创建外星人军队 gf.creat_fleet(ai_settings, screen, ship, aliens) while True: gf.check_event(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: #判断飞船的命数是否用完了的标志 gf.check_event(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) #按下键,飞船不停的移动 ship.update() #子弹更新操作 gf.update_bullets(ai_settings,screen, stats, sb, ship, aliens, bullets) #更新外星人的操作 gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) #更新屏幕 gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,play_button) run_game()
英雄飞机类:
import pygame from settings import Settings from pygame.sprite import Sprite class Ship (Sprite): def __init__(self, ai_settings,screen): super(Ship,self).__init__() #初始化飞船并设置初始化位置 self.screen =screen #调用移动速度 self.ai_settings = ai_settings #加载飞船图像并获取其外接矩形 self.image =pygame.image.load("images/ship.png") self.rect =self.image.get_rect() self.screen_rect =screen.get_rect() #获取屏幕的对象 #将每艘飞船放在屏幕底部中央 self.rect.centerx =self.screen_rect.centerx self.rect.centery = self.screen_rect.centery self.rect.bottom = self.screen_rect.bottom #存储飞船的外形方框的中心 self.centerx = float(self.rect.centerx) self.centery = float(self.rect.centery) #移动标志 self.moveing_right = False #加一个变量,控制飞船右移 self.moveing_left = False #加一个变量,控制左移 self.moveing_up = False self.moveing_down = False #飞船移动 def update(self): if (self.moveing_right and self.rect.centerx <self.screen_rect.right): #判断一下是否跃出右边界,按住右键键,向右移动 self.centerx += self.ai_settings.ship_speed_factor if(self.moveing_left and self.rect.centerx >0): #判断一下是否跃出左边界,按住左键键,向左移动 self.centerx -= self.ai_settings.ship_speed_factor if self.moveing_up and self.rect.top > self.screen_rect.top: self.centery -= self.ai_settings.ship_speed_factor if self.moveing_down and self.rect.bottom < self.screen_rect.bottom: self.centery += self.ai_settings.ship_speed_factor # 根据self.centrx更新rect对象centerx self.rect.centerx = self.centerx self.rect.centery = self.centery #如果被撞了一次,就初始化飞船的置于中间 def center_ship(self): """让飞船在屏幕上居中""" self.centerx = self.screen_rect.centerx self.rect.centery = self.screen_rect.centery self.rect.bottom = self.screen_rect.bottom def blitme(self): #在屏幕上指定位置绘制飞船 self.screen.blit(self.image,self.rect)
敌机类:
import pygame from pygame.sprite import Sprite import random from settings import * class Alien(Sprite): #表示外星人 def __init__(self,ai_settings,screen): #初始化外星人的大小和位置 super(Alien , self).__init__() self.screen = screen self.ai_settings = ai_settings #加载外星人图像,创建一个该图像的外接矩形 self.image = pygame.image.load("./images/enemy/enemy0.png") self.rect = self.image.get_rect() #初始化外星人的位置,使他出现在屏幕左上角位置 self.rect.x =self.rect.width self.rect.y =self.rect.height self.plane_type = 0 self.speed = 2 self.interval=1000 #储存外星人的位置 self.x =float(self.rect.x) def set_plane_type(self, type=1): """ 设置飞机类型 type 0~6,目前的定义,可以添加 type 不同,图像,飞行速度不同 """ self.plane_type = type if self.plane_type == 0: self.image = pygame.image.load("./images/enemy/enemy0.png") # 飞机矩形 self.rect = self.image.get_rect() # 飞机速度 self.speed = 0.5 # 子弹间隔 self.interval = 1000 elif self.plane_type == 1: self.image = pygame.image.load("./images/enemy/enemy1.png") # 飞机矩形 self.rect = self.image.get_rect() # 飞机速度 self.speed = 0.5 # 子弹间隔 self.interval = 1500 elif self.plane_type == 2: self.image = pygame.image.load("./images/enemy/enemy2.png") # 飞机矩形 self.rect = self.image.get_rect() # 飞机速度 self.speed = 0.5 self.interval = 2000 elif self.plane_type == 3: self.image = pygame.image.load("./images/enemy/enemy3.png") # 飞机矩形 self.rect = self.image.get_rect() # 飞机速度 self.speed = 1 self.interval = 1500 elif self.plane_type == 4: self.image = pygame.image.load("./images/enemy/enemy4.png") # 飞机矩形 self.rect = self.image.get_rect() self.speed = 0.5 self.interval = 1500 elif self.plane_type == 5: self.image = pygame.image.load("./images/enemy/enemy5.png") # 飞机矩形 self.rect = self.image.get_rect() # 飞机速度 self.speed = 1 self.interval = 1500 elif self.plane_type == 6: self.image = pygame.image.load("./images/enemy/enemy6.png") # 飞机矩形 self.rect = self.image.get_rect() # 飞机速度 self.speed = 1 self.interval = 1500 # 默认 else: self.image = pygame.image.load("./images/enemy/enemy0.png") # 飞机矩形 self.rect = self.image.get_rect() self.speed = 0.5 # 子弹间隔 self.interval = 1000 # 设置飞机位置 def set_pos(self, x, y): self.rect.x = x self.rect.y = y #表示让外星人动起来 def update(self): self.rect.x += self.ai_settings.fleet_line_speed self.rect.y += self.speed+self.ai_settings.alien_speed_factor if not self.rect.colliderect(self.screen.get_rect()): self.kill() def set_motion_type(self, type=0): """ 飞机的飞行类型 type 0 """ self.motion_type = type #绘制外星人 def blitme(self): self.screen.blit(self.image,self.rect)
游戏计分和等级以及英雄机的显示类:
import pygame.font from pygame.sprite import Group from ship import Ship class Scoreboard(): #显示得分信息的类 def __init__(self,ai_settings,screen,stats): #初始化得分涉及的属性 self.screen = screen self.screen_rect = screen.get_rect() self.ai_settings =ai_settings self.stats = stats #显示得分信息时使用的字体设置 self.text_color =(255, 255, 255) self.font =pygame.font.SysFont('arial',40) #准备初始得分图像 self.prep_score() #最高得分 self.prep_high_score() #关卡等级 self.prep_level() #飞船的命数 self.prep_ships() #显示余下多少飞船 def prep_ships(self): self.ships =Group() #飞船命数形成组 for ship_number in range(self.stats.ships_left): ship =Ship(self.ai_settings,self.screen) ship.rect.x =1070 +ship_number*ship.rect.width ship.rect.y =10 self.ships.add(ship) #将等级转化成图像 def prep_level(self): level_image_str ="Level:"+str(self.stats.level) self.level_image =self.font.render(level_image_str, True, self.text_color,self.ai_settings.bg_color) self.level_rect =self.level_image.get_rect() self.level_rect.left =self.screen_rect.left self.level_rect.top =self.score_rect.bottom+15 #将最高得分转化成图像 def prep_high_score(self): high_score =self.stats.high_score high_score_str ="High score:"+str(high_score) self.high_score_image =self.font.render(high_score_str,True,self.text_color,self.ai_settings.bg_color) #将最高得分放在屏幕中间 self.high_score_rect =self.high_score_image.get_rect() self.high_score_rect.centerx =self.screen_rect.centerx self.high_score_rect.top = 10 #将得分转化成一副渲染的图像 def prep_score(self): score_str ="Score: "+str(self.stats.score) #将得分变成字符串,然后变成图像 self.score_image =self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color) #将得分放在屏幕右上角 self.score_rect =self.score_image.get_rect() self.score_rect.right = self.screen_rect.right -20 self.screen_rect.top =10 #与屏幕上端的距离 #在屏幕显示得分 def show_score(self): self.screen.blit(self.score_image,self.screen_rect) self.screen.blit(self.high_score_image, self.high_score_rect) self.screen.blit(self.level_image, self.level_rect) #绘制飞船 self.ships.draw(self.screen)
6.完成效果图: