Python实现飞机大战(封装后版本)
2019-03-13 21:07
435 查看
图片见上一版本
import pygame,random,time,os from pygame.locals import * def getPic(path): return os.path.join('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img',path) class Base(): def __init__(self, x, y, windows): self.x = x self.y = y self.windows = windows class BaseBullet(Base): def draw(self): self.windows.blit(self.pic, (self.x, self.y)) self.move() class HeroBullet(BaseBullet): def __init__(self,x,y,windows): super().__init__(x,y,windows) self.pic=pygame.image.load(getPic('bullet.png')) def move(self): self.y-=5 class enemyBullet(BaseBullet): def __init__(self,x,y,windows): super().__init__(x, y, windows) self.pic = pygame.image.load(getPic('bullet1.png')) def move(self): self.y+=5 class BasePlane(Base): def __init__(self, x, y, windows,normalImageList,bombImageIndex): super().__init__(x,y,windows) self.normalImageList = normalImageList self.normalIndex = 0 self.bombImageList = bombImageIndex self.bombImageIndex = 0 self.isBomb = False self.biuList = [] def draw(self): if self.isBomb == False: pic = pygame.image.load(getPic(self.normalImageList[self.normalIndex])) self.windows.blit(pic, (self.x, self.y)) self.normalIndex = (self.normalIndex + 1) % len(self.normalImageList) else: if self.bombImageIndex == len(self.bombImageList): time.sleep(0.8) exit(0) pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex])) self.windows.blit(pic, (self.x, self.y)) self.bombImageIndex = (self.bombImageIndex + 1) time.sleep(0.6) class EnemyPlane(BasePlane): def __init__(self, x, y, windows): super().__init__(x,y,windows,['enemy1.png'],['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png']) self.direct = '左' def draw(self): super().draw() self.move() self.fire() for biu in self.biuList: biu.draw() self.biuList.remove(biu) if biu.y > 650 else '' def move(self): if self.direct == '左': self.x -= 3 if self.x <= 0: self.direct = '右' elif self.direct == '右': self.x += 3 if self.x >= 411: self.direct = '左' def fire(self): x = random.randint(0, 100) if x == 5 or x == 75: oneBiu = enemyBullet(self.x + 69 // 2 - 9 // 2, self.y + 89, windows) self.biuList.append(oneBiu) def pzjc(self, bList): eRect = Rect(self.x, self.y, 69, 89) for biu in bList: bRect = Rect(biu.x, biu.y, 22, 22) if bRect.colliderect(eRect): self.isBomb = True class HeroPlane(BasePlane): def __init__(self,x,y,windows): super().__init__(x,y,windows,['hero1.png','hero2.png'],['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']) def draw(self): super().draw() for biu in self.biuList: biu.draw() self.biuList.remove(biu) if biu.y<0 else'' def dealEvent(self,eventList): for event in eventList: if event.type==QUIT: exit(0) if event.type==KEYDOWN: if event.key==K_LEFT: self.x = self.x - 5 if self.x >= 5 else 0 elif event.key==K_RIGHT: self.x = self.x + 5 if self.x <= 375 else 380 elif event.key == K_UP: self.y = self.y - 5 if self.y >= 5 else 0 elif event.key == K_DOWN: self.y = self.y + 5 if self.y <= 516 else 521 elif event.key==K_SPACE: oneBiu=HeroBullet(self.x+39,self.y-22,windows) self.biuList.append(oneBiu) def pzjc(self,bList): eRect=Rect(self.x,self.y,100,124) for biu in bList: bRect=Rect(biu.x,biu.y,9,21) if bRect.colliderect(eRect): self.isBomb=True windows=pygame.display.set_mode((480,650),0,32) pygame.display.set_caption('灰机大战') icon=pygame.image.load(getPic('icon72x72.png')) pygame.key.set_repeat(20,30) pygame.display.set_icon(icon) bg=pygame.image.load(getPic('background.png')) heroPlane=HeroPlane(480//2-100//2,650-124,windows) enemyPlane=EnemyPlane(205.5,0,windows) while True: windows.blit(bg,(0,0)) heroPlane.draw() enemyPlane.draw() enemyPlane.pzjc(heroPlane.biuList) heroPlane.pzjc(enemyPlane.biuList) heroPlane.dealEvent(pygame.event.get()) pygame.display.update()
相关文章推荐
- python3 利用xlrd模块封装实现读取表格数据
- 【python学习笔记】使用类封装实现贪吃蛇
- 用封装ClippingNode实现新手引导(cocos2dx 3.2版本)
- simhash Java和Python版本的实现
- python3.5实现决策树c4.5(连续值版本)
- python封装Windows Event实现全局跨进程同步锁
- Python封装原理与实现方法详解
- [Python]解释器的几种实现版本
- python封装opencv的版本问题
- 如何在Anaconda中实现多版本python Spyder共存
- mac上实现切换python版本
- 【Python】Python解释器的几种实现版本
- python实现封装得到virustotal扫描结果
- python实现wininet封装模块
- python实现获取系统版本和mac信息上传到指定接口
- python的实现版本
- 自己实现的一个python版本的多线程 ftp扫描器,python版本3.0以上
- 实现python版本的按任意键继续/退出
- Cocos2d-x 3.2 lua飞机大战开发实例(二)敌机类的封装,以及碰撞检测的实现
- python实现指定目录下批量文件的单词计数:并发版本