#-*- coding:utf-8 -*- #导入相应的模块 import pygame import time from pygame.locals import * from sys import exit import random #定义Base类,保存飞机的基本显示功能 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) #定义BasePlane,保存飞机显示功能 class BasePlane(Base): def __init__(self, screen_temp, x, y, image_name): Base.__init__(self, screen_temp, x, y, image_name) self.bullet_list = [] def display(self): self.screen.blit(self.image, (self.x, self.y)) #for循环遍历每一颗发出的子弹 for bullet in self.bullet_list: bullet.display() bullet.move() #判断子弹越界 if bullet.judge(): self.bullet_list.remove(bullet) #定义Hero飞机类 class HeroPlane(BasePlane): def __init__(self, screen_temp): BasePlane.__init__(self, screen_temp, 210, 700, './feiji/hero1.png') #定义爆炸基本条件 self.hit = False self.bomb_list = [] self.__crate_images() self.image_num = 0 self.image_index = 0 #定义爆炸图片方法,用列表存储爆炸过程的四张图片 def __crate_images(self): self.bomb_list.append(pygame.image.load('./feiji/hero_blowup_n1.png')) self.bomb_list.append(pygame.image.load('./feiji/hero_blowup_n2.png')) self.bomb_list.append(pygame.image.load('./feiji/hero_blowup_n3.png')) self.bomb_list.append(pygame.image.load('./feiji/hero_blowup_n4.png')) #定义显示爆炸 def display(self): if self.hit == True: self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y)) self.image_num += 1 if self.image_num > 7: self.image_num = 0 self.image_index +=1 if self.image_index > 3: time.sleep(1) exit() self.image_index = 0 else: self.screen.blit(self.image, (self.x, self.y)) #使用super方法调用父类 super(HeroPlane, self).display() #控制飞机上下左右移动 def move_left(self): self.x -= 20 def move_right(self): self.x += 20 def move_up(self): self.y -= 10 def move_down(self): self.y += 10 def fire(self): """通过创建一个子弹对象,完成发射子弹""" print("-----1----") bullet = Bullet(self.screen, self.x, self.y)#创建一个子弹对象 self.bullet_list.append(bullet) #定义爆炸 def bomb(self): self.hit = True #def hit(self): #定义敌机类 class EnemyPlane(BasePlane): def __init__(self, screen_temp): BasePlane.__init__(self, screen_temp, 0, 0, './feiji/enemy0.png') self.direction = 'right' #控制敌机左右移动 def move(self): if self.direction == 'right': self.x += 3 elif self.direction == 'left': self.x -= 3 #敌机边界判定 if self.x > 480-50: self.direction = 'left' elif self.x < 0: self.direction = 'right' #敌机开火 def fire(self): #取0-100之间的一个数 random_num = random.randint(0,100) #如果取的数等于13,就发出一颗子弹 if random_num == 13: self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) #定义子弹基类 class BaseBullet(Base): def display(self): self.screen.blit(self.image, (self.x, self.y)) #定义子弹类 class Bullet(object): def __init__(self, screen_temp, x, y): self.x = x + 40 self.y = y - 20 self.image = pygame.image.load("./feiji/bullet.png") self.screen = screen_temp def display(self): self.screen.blit(self.image, (self.x, self.y)) #子弹移动 def move(self): self.y -= 20 #子弹边界判定,出界删除 def judge(self): if self.y < 0: return True else: return False #定义敌机子弹 class EnemyBullet(BaseBullet): def __init__(self, screen_temp, x, y): BaseBullet.__init__(self, screen_temp, x+26, y+40, './feiji/bullet1.png') #子弹移动 def move(self): self.y += 5 #子弹边界判定 def judge(self): if self.y > 852 : return True else: return False #定义按键控制函数 def KeyControl(hero_temp): #用get抓取所有event项,for循环遍历 for event in pygame.event.get(): if event.type == QUIT: exit() #如果键被按下 elif event.type == KEYDOWN: #如果键为xxx就xxx,这里控制飞机移动 if event.key == K_a or event.key == K_LEFT: print('left') hero_temp.move_left() #检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print('right') hero_temp.move_right() elif event.key == K_w or event.key == K_UP: print('up') hero_temp.move_up() elif event.key == K_s or event.key == K_DOWN: print('down') hero_temp.move_down() #检测按键是否是空格键 elif event.key == K_SPACE: print('space') hero_temp.fire() elif event.key == K_b: hero_temp.bomb() #定义主函数 def main(): #定义一个窗口 screen = pygame.display.set_mode((480, 852), 0, 32) #背景图 background = pygame.image.load('./feiji/background.png').convert() #标题 pygame.display.set_caption("飞机大战") #飞机显示 hero = HeroPlane(screen) #敌机显示 enemy = EnemyPlane(screen) #程序一直不断循环显示 while True: screen.blit(background, (0, 0)) hero.display() enemy.display() enemy.move() enemy.fire() pygame.display.update() KeyControl(hero) time.sleep(0.01) if __name__ == '__main__': main()实现爆炸效果,基类提取完成,使用了super方法优化代码
飞机大战(1)
猜你喜欢
转载自blog.csdn.net/qq_41805514/article/details/80260351
今日推荐
周排行