import pygame
from pygame.locals import *
import time
import random
# 全局变量 表示爆炸
bomb_flag = 0 # 0 表示没有爆炸,1表示爆炸
# 敌机和英雄飞机的父类
class Plane(object):
def __init__(self,screen,image_path,x,y):
self.screen = screen
self.x = x
self.y = y
self.image = pygame.image.load(image_path)
# 子弹列表
self.bullet_list = []
def display(self):
# 显示英雄和敌机飞机
self.screen.blit(self.image,(self.x,self.y))
# 装越界的子弹
bullet_list_remove = []
# 显示子弹
for bullet in self.bullet_list:
# 显示和移动子弹
bullet.display()
bullet.move()
# 判断子弹是否越界
if bullet.judge():
bullet_list_remove.append(bullet)
# 删除越界的子弹
for i in bullet_list_remove:
self.bullet_list.remove(i)
class HeroPlane(Plane):
def __init__(self,screen):
image_path = 'feiji/hero1.png'
super(HeroPlane, self).__init__(screen,image_path,190,500)
# 添加爆炸效果
self.bomb_image_list = []
self.__get_bomb_image() # 加载爆炸图片
self.isbomb = False # false没有爆炸,True爆炸
self.image_num = 0 # 显示过的图片数
self.image_index = 0 # 显示图片的下标变化
def display(self):
if self.isbomb:
bomb_image = self.bomb_image_list[self.image_index]
self.screen.blit(bomb_image,(self.x,self.y))
self.image_num += 1
if self.image_num ==(self.image_length + 1):
self.image_num = 0
self.image_index += 1
if self.image_index > (self.image_length-1):
self.image_index = 5
time.sleep(2)
exit()
else: