Python实现飞机大战(1版本)
2019-03-13 21:05
471 查看
飞机大战:
import pygame from pygame.locals import * import random import time #heroPlane子弹发射 class HeroBullet(): def __init__(self,x,y,windows): self.x=x self.y=y self.windows=windows self.pic=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\bullet.png') def draw(self): self.windows.blit(self.pic,(self.x,self.y)) self.move() def move(self): self.y-=5 #敌方子弹发射 class enemyBullet(): def __init__(self,x,y,windows): self.x=x self.y=y self.windows=windows self.pic = pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\bullet1.png') def draw(self): self.windows.blit(self.pic,(self.x,self.y)) self.move() def move(self): self.y+=5 windows=pygame.display.set_mode((480,650),0,32)#创建窗口 bg=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\background.png')#导入背景图片 pygame.display.set_caption('灰机大战')#窗口命名 icon=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\icon72x72.png')#导入窗口标题图片 pygame.display.set_icon(icon) heroPlane1=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero1.png')#导入自己飞机图片 enemyPlane=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1.png')#导入敌方飞机图片 heroPlane2=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero2.png')#导入自己飞机喷气图片 enemyBombList=['D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down1.png', 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down2.png', 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down3.png', 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down4.png']#导入敌方飞机爆炸图片 heroBombList=['D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n1.png', 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n2.png', 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n3.png', 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero_blowup_n4.png']#导入我方飞机爆炸图片 heroIndexShift=0#飞机交替尾气效果 direct='左' #先设置个运动方向 pygame.key.set_repeat(20,30) heroPlaneX=190#飞机初始位置 heroPlaneY=526 enemyPlaneX=205.5#敌机初始位置 enemyPlaneY=0 BiuList=[]#定义空列表放我方子弹 enemyBiuList=[]#定义空列表放敌方子弹 enemy_isBomb=False #敌军爆炸 enemy_BombIndex=0 hero_isBomb=False #我方飞机 hero_BombIndex=0 while True: windows.blit(bg,(0,0)) #设置heroplane尾气运动效果,两张图片交替出现 if heroIndexShift==0: windows.blit(heroPlane1,(heroPlaneX,heroPlaneY)) heroIndexShift+=1 else: windows.blit(heroPlane2,(heroPlaneX, heroPlaneY)) heroIndexShift=0 #控制敌机自己运动 if direct=='左': enemyPlaneX-=3 if enemyPlaneX<=0: direct='右' elif direct=='右': enemyPlaneX+=3 if enemyPlaneX>=411: direct='左' #敌机爆炸 if enemy_isBomb==False: windows.blit(enemyPlane,(enemyPlaneX,enemyPlaneY)) else: if enemy_BombIndex==len(enemyBombList): exit(0) pic=pygame.image.load(enemyBombList[enemy_BombIndex]) windows.blit(pic,(enemyPlaneX,enemyPlaneY)) enemy_BombIndex=enemy_BombIndex+1 time.sleep(0.5) #我方飞机爆炸 if hero_isBomb==False: windows.blit(heroPlane1,(heroPlaneX,heroPlaneY)) else: if hero_BombIndex==len(heroBombList): exit(0) pic=pygame.image.load(heroBombList[hero_BombIndex]) windows.blit(pic,(heroPlaneX,heroPlaneY)) hero_BombIndex=hero_BombIndex+1 time.sleep(0.5) # 画出子弹 for biu in BiuList: biu.draw() BiuList.remove(biu) if biu.y < 0 else '' for biu in enemyBiuList: biu.draw() enemyBiuList.remove(biu) if biu.y>650 else '' #飞机事件 for event in pygame.event.get(): if event.type==QUIT: print('关闭了') exit(0) if event.type==KEYDOWN:#按下键盘,上下左右控制飞机运动 if event.key==K_LEFT: heroPlaneX = heroPlaneX-5 if heroPlaneX>=5 else 0 elif event.key==K_RIGHT: heroPlaneX = heroPlaneX+5 if heroPlaneX<=375 else 380 elif event.key==K_UP: heroPlaneY = heroPlaneY-5 if heroPlaneY>=5 else 0 elif event.key==K_DOWN: heroPlaneY = heroPlaneY+5 if heroPlaneY<=516 else 521 elif event.key==K_SPACE:#子弹运动坐标跟随飞机头 oneBiu=HeroBullet(heroPlaneX+50-11,heroPlaneY-22,windows) BiuList.append(oneBiu) #随机生成子弹 x=random.randint(0,100) if x==5 or x==75: oneBiu=enemyBullet(enemyPlaneX+69//2-9//2,enemyPlaneY+89,windows) enemyBiuList.append(oneBiu) #敌机爆炸条件 dijiRect=Rect(enemyPlaneX,enemyPlaneY,69,89) for biu in BiuList: biuRect=Rect(biu.x,biu.y,22,22) if biuRect.colliderect(dijiRect): enemy_isBomb=True BiuList.remove(biu) #我方飞机爆炸条件 heroRect=Rect(heroPlaneX,heroPlaneY,100,124) for biu in enemyBiuList: biuRect=Rect(biu.x,biu.y,9,21) if biuRect.colliderect(heroRect): hero_isBomb=True enemyBiuList.remove(biu) pygame.display.update()
我方子弹:bullet.png
敌方子弹:bullet-1.png
背景图片:background.png
窗口图片:icon72x72.png
敌方飞机:enemy1.png
敌方爆炸飞机:enemy1_down1.png,enemy1_down2.png,enemy1_down3.png,enemy1_down4.png
我方飞机:hero1.png
我方飞机尾气动画效果:hero2.png
我方飞机爆炸效果:hero_blowup_n1.png,hero_blowup_n2.png,hero_blowup_n3.png,hero_blowup_n4.png
相关文章推荐
- Python实现飞机大战(封装后版本)
- 【Python】Python解释器的几种实现版本
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- DES算法 Python版本实现
- DJANGO+PYTHON+SELENIUM实现版本发布后,调用自动化脚本来验证发布发布环境
- Python实现韩文分解(基于python 3.4版本)
- Mac下使用Pyenv实现python多版本共存与切换
- python3.5实现决策树c4.5(离散值非递归版本)
- Python实现批量执行ArcGIS版本的协调和提交
- 想了解python的urllib2是怎么实现的,可以看pypy版本便于理解
- 如何在Anaconda中实现多版本python Spyder共存
- AES加解密多版本(GO、JAVA、Python)实现
- linux 下实现python多版本安装实践
- 用python实现自动备份svn版本库
- 用树莓派实现RGB LED的颜色控制——Python版本
- 实现python版本的按任意键继续/退出
- 神经网络/自编码器的实现(向量化Python版本实现)
- [Python]解释器的几种实现版本
- [三个版本]自定义int()函数(Python实现)
- 在Anaconda中实现多版本Python共存