# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import time
import random
class Base():
def __init__(self,x,y,screen_temp,image_temp):
self.x = x
self.y = y
self.screen = screen_temp
self.image = image_temp
class BasePlane(Base):
def __init__(self,x,y,screen_temp,image_temp):
Base.__init__(self,x,y,screen_temp,image_temp)
self.bomb_list = []#存储爆炸图片
self.hit = 0#1表示被攻击,发生爆炸
self.image_num = 0
self.image_idx = 0
self.bullet_list = []
def display(self):
bullet_list1 = []#用于存储此次显示越界的子弹
for bullet in self.bullet_list:
bullet.display()
bullet.move()
if bullet.judge():#如果子弹越界
bullet_list1.append(bullet)
for bullet1 in bullet_list1:
self.bullet_list.remove(bullet1)
class BaseBullet(Base):
def display(self):
self.screen.blit(self.image,(self.x,self.y))
class HeroPlane(BasePlane):
def __init__(self,screen_temp):
BasePlane.__init__(self,210,700,screen_temp, pygame.image.load("./feiji/hero1.png"))
self.__create_image()
def move_left(self):
self.x -=20
def move_right(self):
self.x +=20
def fire(self):
self.bullet_list.append(HeroBullet(self.x, self.y, self.screen))
def display(self):
if self.hit == 1:#如果被攻击就爆炸
self.screen.blit(self.bomb_list[self.image_idx],(self.x,self.y))
self.image_num +=1
if self.image_num % 15 ==0:
self.image_idx +=1
self.image_num = 0
if self.image_idx>3:
self.image_idx = 0
time.sleep(1)
exit()
if self.hit == 0:
self.screen.blit(self.image,(self.x,self.y))
BasePlane.display(self)
def __create_image(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 behit(self,enemy_bullet_list):
for bullet in enemy_bullet_list:
if self.x <= bullet.x <= self.x+80 and self.y <= bullet.y <= self.y+152:
self.hit = 1
class EnemyPlane(BasePlane):
def __init__(self,screen_temp):
BasePlane.__init__(self,0,0,screen_temp, pygame.image.load("./feiji/enemy0.png"))
self.direction = 0
self.enemy_num = 0
self.__create_image()
def move(self):
if self.direction == 0:
self.x +=5
elif self.direction == 1:
self.x -=5
if self.x>480-50:
self.direction = 1
elif self.x <= 0:
self.direction = 0
if self.y > 850-40:
self.y = 0
else:
self.y +=3
def display(self):
if self.hit == 1:#如果被攻击就爆炸
self.direction = 2
self.screen.blit(self.bomb_list[self.image_idx],(self.x,self.y))
self.image_num +=1
if self.image_num % 3 ==0:
self.image_idx +=1
self.image_num = 0
if self.image_idx>3:
self.x = random.randint(0,480)
self.y = 0
self.hit = 0
self.direction = 0
self.image_idx = 0
self.enemy_num +=1
if self.hit == 0:
self.screen.blit(self.image,(self.x,self.y))
BasePlane.display(self)
def fire(self):
if random.randint(1,100) == 2 or random.randint(1,100) ==34:
self.bullet_list.append(EnemyBullet(self.x, self.y, self.screen))
def __create_image(self):
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down1.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down2.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down3.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down4.png"))
def behit(self,hero_bullet_list):
for bullet in hero_bullet_list:
if self.x <= bullet.x <= self.x+50 and self.y <= bullet.y <= self.y+40:
self.hit = 1
class BossPlane(BasePlane):
def __init__(self,screen_temp):
BasePlane.__init__(self,153,0,screen_temp, pygame.image.load("./feiji/enemy2.png"))
self.__create_image()
def move(self):
pass
def display(self):
if self.hit == 20:#如果被攻击就爆炸
self.screen.blit(self.bomb_list[self.image_idx],(self.x,self.y))
self.image_num +=1
if self.image_num % 20 ==0:
self.image_idx +=1
self.image_num = 0
if self.image_idx>3:
time.sleep(1)
exit()
else:
self.screen.blit(pygame.image.load("./feiji/enemy2_hit.png"),(self.x,self.y))
self.screen.blit(self.image,(self.x,self.y))
BasePlane.display(self)
def fire(self):
if random.randint(1,30) == 25:
x=random.randint(0,480)
self.bullet_list.append(EnemyBullet(x, self.y, self.screen))
def __create_image(self):
self.bomb_list.append(pygame.image.load("./feiji/enemy2_down1.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy2_down2.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy2_down3.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy2_down4.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy2_down5.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy2_down6.png"))
def behit(self,hero_bullet_list):
for bullet in hero_bullet_list:
if self.x <= bullet.x <= self.x+165 and self.y <= bullet.y <= self.y+246 and self.hit != 20:
self.hit += 1
hero_bullet_list.remove(bullet)
class HeroBullet(BaseBullet):
def __init__(self,x,y,screen_temp):
BaseBullet.__init__(self,x+40,y-20,screen_temp, pygame.image.load("./feiji/bullet.png"))
def move(self):
self.y -=20
def judge(self):
if self.y <= 0:
return True
else:
return False
class EnemyBullet(BaseBullet):
def __init__(self,x,y,screen_temp):
BaseBullet.__init__(self,x+25,y+40,screen_temp, pygame.image.load("./feiji/bullet1.png"))
def move(self):
self.y += 5
def judge(self):
if self.y>850:
return True
else:
return False
class BossBullet(BaseBullet):
def __init__(self,x,y,screen_temp):
BaseBullet.__init__(self,x,y+250,screen_temp, pygame.image.load("./feiji/bullet2.png"))
def move(self):
self.y += 10
def judge(self):
if self.y>850:
return True
else:
return False
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
#print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
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_SPACE :
# print('space')
hero_temp.fire()
elif event.key == K_b:
hero_temp.hit = 1
def main():
#1.创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2.创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3.创建一个飞机对象
hero = HeroPlane(screen)
enemy = EnemyPlane(screen)
boss =BossPlane(screen)
while True:
screen.blit(background,(0,0))
hero.display()
if enemy.enemy_num == 10:
boss.display()
boss.fire()
boss.behit(hero.bullet_list)
else:
enemy.display()
enemy.move()
enemy.fire()
enemy.behit(hero.bullet_list)
hero.behit(enemy.bullet_list)
pygame.display.update()
key_control(hero)
time.sleep(0.01)
if __name__ == "__main__":
main()
#后期陆续实现双人打飞机,联网打飞机
|
|