# -*- coding:utf-8 -*-
import pygame
import time
import random
import sys
from pygame.locals import *
#全局变量
#窗口
window_screen = None
#hero
hero = None
#得分
hit_score = 0
#是否暂停
is_pause = False
#图片变量
#暂停图片
pause_image = None
#hero_fire_music
hero_fire_music = None
#number照片
number_image = []
#score_hp照片
score_hp_image = None
#单三管炮弹照片
one_or_three_barral = []
#三管炮弹子弹余量照片
bullet_3_stock = None
#max_score
max_score_image = None
#boss_hp
boss_HP_image = None
#line
line_image = None
#背景
background = None
#重新开始
restart = None
#退出游戏
exit_game = None
#操作说明
description = None
#关于飞机
##飞机HP
HP_list = [1, 20, 100, 20]#enemy0, enemy1, enemy2, hero
#飞机大小
plane_size = [{"width":51, "height":39}, {"width":69, "height":89}, {"width":165, "height":246}, {"width":100, "height":124}]
#各种飞机爆炸效果计数更换图片
plane_bomb_time = [5, 8, 14, 8]#enemy0, enemy1, enemy2, hero
#飞机最大子弹数
plane_maximum_bullet = [2, 5, 7, 8]#enemy0, enemy1, enemy2, hero
#血量补给
blood_supply = None
#子弹补给
bullet_supply = None
#关于子弹
#敌机子弹类型
bullet_type = ["bullet1.png", "bullet-1.gif", "bullet2.png", "bullet.png"]
#子弹伤害值
bullet_damage_value = [1, 1, 3, 1]#enemy0, enemy1, enemy2, hero
#补给
supply_image = ["bomb-1.gif", "bomb-2.gif"]
#补给的大小
supply_size = [{"width":58, "height":88}, {"width":60, "height":103}]
#敌机引用列表
enemy0_list = []#enemy0存在飞机列表
enemy0_maximum = 8#enemy0飞机存在最大值
enemy1_list = []#enemy1存在飞机列表
enemy1_maximum = 1
enemy2_list = []#enemy2存在飞机列表
enemy2_maximum = 1
class Base(object):
"""所有类的基类"""
def __init__(self, screen_temp, x, y, image_name):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load(image_name)
class BasePlane(Base):
"""飞机基类"""
def __init__(self, plane_type, screen_temp, x, y, image_name, picture_num, HP_temp):
Base.__init__(self, screen_temp, x, y, image_name)#plane_type飞机类型
self.bullet_list = [] #存储发射出去的子弹的引用
self.plane_type = plane_type #飞机类型标示, 3是hero
#爆炸效果用的如下属性
self.hitted = False #表示是否要爆炸
self.bomb_picture_list = [] #用来存储爆炸时需要的图片
self.bomb_picture_num = picture_num #飞机爆炸效果的图片数量
self.picture_count = 0#用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号
self.HP = HP_temp #飞机hp
self.fire_bullet_count = 0#飞机已发射子弹计数
def display(self):
"""显示玩家的飞机"""
global hit_score
global HP_list
global plane_bomb_time#飞机爆炸效果计数
#如果被击中,就显示爆炸效果,否则显示普通的飞机效果
if self.hitted == True and self.image_index < self.bomb_picture_num and self.HP <= 0:
self.screen.blit(self.bomb_picture_list[self.image_index], (self.x, self.y))
if self.plane_type != 3 and self.image_index == 0 and self.picture_count == 0:
if self.plane_type == 0:#击毁enemy0得分+HP
if hit_score < 650:#初始血量为1
hit_score += HP_list[self.plane_type]
else:#初始血量为2
hit_score += HP_list[self.plane_type]/2
elif self.plane_type == 1:#击毁enemy1得分+HP/2
hit_score += HP_list[self.plane_type]/2
else:#击毁enemy2得分+HP/4
hit_score += HP_list[self.plane_type]/4
self.picture_count += 1
if self.picture_count == plane_bomb_time[self.plane_type]: #根据飞机类型不同,爆炸效果持续的时间不同
self.picture_count = 0
self.image_index += 1
elif self.image_index < self.bomb_picture_num:
self.screen.blit(self.image, (self.x, self.y)) #显示原图
if self.hitted == True and not self.bullet_list and self.image_index >= self.bomb_picture_num and self.HP <= 0:
del_plane(self) #删除被击中敌机的对象
#敌机飞出window后删除
if self.y > 860:
del_plane(self)
#删除越界子弹
del_outWindow_bullet(self)
#创建出爆炸效果的图片的引用
def crate_images(self, bomb_picture_name):
for i in range(1, self.bomb_picture_num + 1):
self.bomb_picture_list.append(pygame.image.load("./feiji/" + bomb_picture_name + str(i) + ".png"))
#判断是否被击中
def isHitted(self, plane, width, height):# widht和height表示范围
if plane.bullet_list and self.HP:
for bullet in plane.bullet_list:
if bullet.x > self.x+0.05*width and bullet.x < self.x+0.95*width and bullet.y+0.1*height > self.y and bullet.y < self.y + 0.8*height:
self.HP -= bullet.damage_value#hero的HP减去子弹的伤害值
if self.plane_type == 3:
show_score_HP()
plane.bullet_list.remove(bullet) #删除击中的子弹
self.hitted = True
if plane.plane_type == 3 and plane.barrel_2 and plane.barrel_3:
for bullet in plane.barrel_2:#判断炮管3是否击中
if bullet.x > self.x+0.05*width and bullet.x < self.x+0.95*width and bullet.y+0.1*height > self.y and bullet.y < self.y + 0.8*height:
self.HP -= bullet.damage_value#hero的HP减去子弹的伤害值
plane.barrel_2.remove(bullet) #删除击中的子弹
self.hitted = True
for bullet in plane.barrel_3:#判断炮管3是否击中
if bullet.x > self.x+0.05*width and bullet.x < self.x+0.95*width and bullet.y+0.1*height > self.y and bullet.y < self.y + 0.8*height:
self.HP -= bullet.damage_value#hero的HP减去子弹的伤害值
plane.barrel_3.remove(bullet) #删除击中的子弹
self.hitted = True
#飞机开火
def fire(self, bullet_maximun):
if self.HP > 0:
random_num = random.randint(1, 60)
if (random_num == 10 or random_num == 45) and len(self.bullet_list) < bullet_maximun:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y, self))
self.fire_bullet_count += 1
class HeroPlane(BasePlane):
global supply_size
def __init__(self, screen_temp):
BasePlane.__init__(self, 3, screen_temp, 210, 728, "./feiji/hero1.png", 4, HP_list[3]) #super().__init__()
BasePlane.crate_images(self, "hero_blowup_n")
self.key_down_list = [] #用来存储键盘上下左右移动键
self.space_key_list = []#保存space键
self.is_three_bullet = False
self.barrel_2 = []#2号炮管(左)
self.barrel_3 = []#3号炮管(右)
self.three_bullet_stock = 50#三管齐发子弹初始值为50
#单键移动方向
def move_left(self):
self.x -= 7
def move_right(self):
self.x += 7
def move_up(self):
self.y -= 6
def move_down(self):
self.y += 6
#双键移动方向
def move_left_and_up(self):
self.x -= 5
self.y -= 6
def move_right_and_up(self):
self.x += 5
self.y -= 6
def move_lef_and_down(self):
self.x -= 5
self.y += 6
def move_right_and_down(self):
self.x += 5