python版飞机大战代码分享
利用pygame实现了简易版飞机大战。源代码如下:
#-*-coding:utf-8-*-
importpygame
importsys
frompygame.localsimport*
frompygame.fontimport*
importtime
importrandom
classHero(object):
#玩家英雄类
def__init__(self,screen_temp):
self.x=210
self.y=700
self.life=21
#self.life=100
self.image=pygame.image.load("./feiji/hero1.png")
self.screen=screen_temp
self.bullet_list=[]#用来存储子弹对象的引用
#爆炸效果用的如下属性
self.hit=False#表示是否要爆炸
self.bomb_list=[]#用来存储爆炸时需要的图片
self.__create_images()#调用这个方法向bomb_list中添加图片
self.image_num=0#用来记录whileTrue的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index=0#用来记录当前要显示的爆炸效果的图片的序号
def__create_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"))
defdisplay(self):
#显示玩家的飞机
#如果被击中,就显示爆炸效果,否则显示普通的飞机效果
ifself.hit==True:
self.screen.blit(self.bomb_list[self.image_index],(self.x,self.y))#(self.x,self.y)是指当前英雄的位置
#blit方法(一个对象,左上角位置)
self.image_num+=1
print(self.image_num)
ifself.image_num==7:
self.image_num=0
self.image_index+=1
print(self.image_index)#这里子弹打住英雄时没有被清除掉,所以打一下,就死了
ifself.image_index>3:
time.sleep(1)
exit()#调用exit让游戏退出
#self.image_index=0
else:
ifself.x<0:#控制英雄,不让它跑出界面
self.x=0
elifself.x>382:
self.x=382
ifself.y<0:
self.y=0
elifself.y>750:
self.y=750
self.screen.blit(self.image,(self.x,self.y))#z这里是只要没有被打中,就一直是刚开始的样子
#不管玩家飞机是否被击中,都要显示发射出去的子弹
forbulletinself.bullet_list:
bullet.display()
bullet.move()
defmove(self,move_x,move_y):
self.x+=move_x
self.y+=move_y
deffire(self):
#通过创建一个子弹对象,完成发射子弹
bullet=Bullet(self.screen,self.x,self.y)#创建一个子弹对象
self.bullet_list.append(bullet)
defbomb(self):
self.hit=True
defjudge(self):
globallife
iflife<=0:
self.bomb()
classBullet(object):
#玩家子弹类
def__init__(self,screen_temp,x_temp,y_temp):
self.x=x_temp+40
self.y=y_temp-20
self.image=pygame.image.load("./feiji/bullet.png")
self.screen=screen_temp
defdisplay(self):
self.screen.blit(self.image,(self.x,self.y))
defmove(self):
self.y-=10
classBullet_Enemy(object):
#敌机子弹类
def__init__(self,screen_temp,x_temp,y_temp):
self.x=x_temp+25
self.y=y_temp+30
self.image=pygame.image.load("./feiji/bullet1.png")
self.screen=screen_temp
defdisplay(self):
self.screen.blit(self.image,(self.x,self.y))
defmove(self,hero):
self.y+=10
globallife
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
#ifself.yinrange(hero.y,hero.y+40)andself.xinrange(hero.x,hero.x+40):
life-=10
#self.bullet_list.remove()
print("---judge_enemy---")
returnTrue
iflife<=0:
hero.bomb()
returnFalse
classBullet_Boss(object):
#boss子弹类1
def__init__(self,screen_temp,x_temp,y_temp):
self.x=x_temp+80
self.y=y_temp+230
self.image=pygame.image.load("./feiji/bullet2.png")
self.screen=screen_temp
defdisplay(self):
self.screen.blit(self.image,(self.x,self.y))
defmove(self,hero):
self.y+=6
self.x+=2
globallife
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
#ifself.yinrange(hero.y,hero.y+40)andself.xinrange(hero.x,hero.x+40):
life-=20
#self.bullet_list.remove()
print("---judge_boss---")
returnTrue
iflife<=0:
hero.bomb()
returnFalse
classBullet_Boss1(object):
#boss子弹类2
def__init__(self,screen_temp,x_temp,y_temp):
self.x=x_temp+80
self.y=y_temp+230
self.image=pygame.image.load("./feiji/bullet2.png")
self.screen=screen_temp
defdisplay(self):
self.screen.blit(self.image,(self.x,self.y))
defmove(self,hero):
self.y+=6
self.x-=2
globallife
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
#ifself.yinrange(hero.y,hero.y+40)andself.xinrange(hero.x,hero.x+40):
life-=20
#self.bullet_list.remove()
print("---judge_boss---")
returnTrue
iflife<=0:
hero.bomb()
returnFalse
classBullet_Boss2(object):
#boss子弹类3
def__init__(self,screen_temp,x_temp,y_temp):
self.x=x_temp+80
self.y=y_temp+230
self.image=pygame.image.load("./feiji/bullet2.png")
self.screen=screen_temp
defdisplay(self):
self.screen.blit(self.image,(self.x,self.y))
defmove(self,hero):
self.y+=6
globallife
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
#ifself.yinrange(hero.y,hero.y+40)andself.xinrange(hero.x,hero.x+40):
life-=20
#self.bullet_list.remove()
print("---judge_boss---")
returnTrue
iflife<=0:
hero.bomb()
returnFalse
classBase(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)
self.alive=True
defdisplay(self):
ifself.alive==True:
self.screen.blit(self.image,(self.x,self.y))
defmove(self):
self.y+=5
classbomb_bullet(Base):
#炸弹类
def__init__(self,screen_temp):
Base.__init__(self,screen_temp,random.randint(45,400),0,"./feiji/bomb.png")
defjudge(self,hero):
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
self.alive=False
hero.bomb()
ifself.y>=850:
#self.alive=False
self.y=0
self.x=random.randint(45,400)
#print("bomb.y=%d"%self.y)
classsupply(Base):
#补给类
def__init__(self,screen_temp):
Base.__init__(self,screen_temp,random.randint(45,400),-300,"./feiji/bomb-1.gif")
defjudge(self,hero):
globallife
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
self.alive=False
life+=10
ifself.y>=1500:
self.y=0
self.x=random.randint(45,400)
self.alive=True
classclear_bullet(Base):
def__init__(self,screen_temp):
Base.__init__(self,screen_temp,random.randint(45,400),0,"./feiji/bomb-2.gif")
self.alive=False
defjudge(self,hero,enemies):
globalq
q+=1
#self.move()
ifq==20:
#self.move()
self.alive=True
q=0
if(hero.y<=self.yandself.y<=hero.y+40)and(hero.x<=self.xandself.x<=hero.x+100):
self.alive=False
forenemyinenemies:
enemy.hit==True
classEnemyPlane(object):
#敌机类
def__init__(self,screen_temp):
self.x=random.randint(15,480)
self.y=0
self.image=pygame.image.load("./feiji/enemy0.png")
self.screen=screen_temp
self.bullet_list=[]#用来存储子弹对象的引用
#self.direction="right"#用来设置这个飞机默认的移动方向
self.hit=False
self.bomb_list=[]
self.__create_images()
self.image_num=0
self.image_index=0
#利用产生的随机数,随机确定飞机初始移动方向
self.k=random.randint(1,20)
ifself.k<=10:
self.direction="right"
elifself.k>10:
self.direction="left"
defdisplay(self,hero):
#显示敌人的飞机
ifnotself.hit:
self.screen.blit(self.image,(self.x,self.y))
else:
self.screen.blit(self.bomb_list[self.image_index],(self.x,self.y))
self.image_num+=1
ifself.image_num==3andself.image_index<3:
self.image_num=0
self.image_index+=1
#print(self.image_index)
#ifself.image_index>2:
#time.sleep(0.1)
forbulletinself.bullet_list:
bullet.display()
if(bullet.move(hero)):
self.bullet_list.remove(bullet)
defmove(self):
#利用随机数来控制飞机移动距离,以及移动范围
d1=random.uniform(1,3)
d2=random.uniform(0.2,3)
p1=random.uniform(50,100)
p2=random.uniform(-200,0)
ifself.direction=="right":
self.x+=d1
elifself.direction=="left":
self.x-=d1
ifself.x>480-p1:
#480-50
self.direction="left"
elifself.x480:#删除越界的玩家子弹
hero.bullet_list.remove(bullet1)
defjudge3(hero,boss):
#判断boss的炸毁
globalgoal,g,goal0
forbullet3inhero.bullet_list:
ifbullet3.yinrange(int(boss.y),int(boss.y+60))andbullet3.xinrange(int(boss.x),int(boss.x+100)):
hero.bullet_list.remove(bullet3)
g+=1
boss.image=boss.bomb_list[g]
print("g=%d"%g)
ifg>=6:
boss.y,g,goal=0,0,0
boss.bomb()
goal0+=10
defclear_enemy(enemies):
#清除敌机群类中被炸毁的敌机
globalgoal,goal0
forenemyinenemies.enemy_list:
ifenemy.hit==Trueandenemy.image_index==3:
enemies.enemy_list.remove(enemy)
enemies.num-=1
goal+=1
goal0+=5
print("goal=%d"%goal)
ifenemy.y>=850:
enemies.enemy_list.remove(enemy)
enemies.num-=1
defjudge_num(enemies):
#判断频幕上敌人的数量,如果为零,继续添加敌人
n=random.randint(1,5)
iflen(enemies.enemy_list)==0:
enemies.add_enemy(n)
defshow_text(screen_temp):
#在屏幕上显示文字
text="GOAL:"+str(goal0)+"Life:"+str(life)
font_size=50
pos=(0,0)
color=(0,255,0)
cur_font=pygame.font.SysFont("宋体",font_size)
text_fmt=cur_font.render(text,1,color)
screen_temp.blit(text_fmt,pos)
defcreat_bomb(screen_temp):
bomb=bomb_bullet(screen_temp)
bomb_list=[]
bomb_list.apend(bomb)
#定义的全局变量
goal=0#玩家得分
goal0=0
g=0#击中boss的次数
life=100#生命值
s=0#判断大boss是否发射子弹
q=0
defmain():
#主函数执行
#获取事件,比如按键等
bb=False
move_x=0
move_y=0
pygame.init()
screen=pygame.display.set_mode((480,852),0,32)
#210,400
background=pygame.image.load("./feiji/background.png")
pygame.display.set_caption("飞机大战")
atlas=pygame.image.load("./feiji/NewAtlas.png")
#创建玩家飞机
hero=Hero(screen)
#创建敌机群
enemis=EnemyPlanes(screen)
enemis.add_enemy(5)
#创建boss对象
boss=Boss(screen)
#创建炸弹对象
bomb=bomb_bullet(screen)
#创建补给对象
supply0=supply(screen)
clear=clear_bullet(screen)
left_key,right_key,up_key,down_key,done=0,0,0,0,0
#mark=0#用来判断boss发射子弹
whileTrue:
ifdone:
ifdone%8==0:
done=1
hero.fire()
else:
done+=1
foreventinpygame.event.get():
#判断是否是点击了退出按钮
ifevent.type==QUIT:
print("exit")
exit()
#判断是否是按下了键
ifevent.type==KEYDOWN:
#down
#检测按键是否是a或者left
ifevent.key==K_aorevent.key==K_LEFT:
#print('left')
move_x=-5
left_key+=1
#检测按键是否是d或者right
elifevent.key==K_dorevent.key==K_RIGHT:
#print('right')
move_x=5
right_key+=1
elifevent.key==K_worevent.key==K_UP:
move_y=-5
up_key+=1
elifevent.key==K_sorevent.key==K_DOWN:
move_y=5
down_key+=1
#检测按键是否是空格键
elifevent.key==K_SPACE:
#print('space')
hero.fire()
done=1
#enemis.fire()
elifevent.key==K_b:
print('b')
hero.bomb()
ifevent.type==KEYUP:
ifevent.key==K_aorevent.key==K_LEFT:
left_key-=1
ifright_key==0:
move_x=0
else:
move_x=5
ifevent.key==K_dorevent.key==K_RIGHT:
right_key-=1
ifleft_key==0:
move_x=0
else:
move_x=-5
ifevent.key==K_worevent.key==K_UP:
up_key-=1
ifdown_key==0:
move_y=0
else:
move_y=5
ifevent.key==K_sorevent.key==K_DOWN:
down_key-=1
ifup_key==0:
move_y=0
else:
move_y=-5
ifevent.key==K_SPACE:
done=0
screen.blit(background,(0,0))
hero.move(move_x,move_y)
hero.display()
hero.judge()
enemis.display(hero)
enemis.move()
enemis.fire()
bomb.display()
bomb.judge(hero)
bomb.move()
supply0.display()
supply0.judge(hero)
supply0.move()
#clear.display()
#clear.judge(hero,enemis)
#clear.move()
foriinrange(enemis.num):
judge1(hero,enemis.enemy_list[i])
#enemis.enemy_list[i].judge(hero)
clear_enemy(enemis)
judge_num(enemis)
show_text(screen)
ifgoal>=15:
boss.display(hero)
boss.move()
#mark+=1
#ifmark==8:
boss.fire()
#mark=0
#boss.judge
judge3(hero,boss)
pygame.display.update()
if__name__=="__main__":
main()
方法是利用面向对象的思想,写了基本的敌机类、英雄类、武器类等,利用继承关系产生多架敌机。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。