上一篇(Python游戏之Pygame——太空飞机大战(一))文章简单对游戏进行了说明,给出了部分配置文件以及子弹类。下面给出敌机类和方法。
大家直到,飞机必须能非,最好能非直线飞行。因此必须有X_speed和Y_speed。飞机还要能发射子弹或导弹等,飞机有多少个发射位,多久发射一次,还有飞机被子弹击中或者敌机与英雄战机相撞等各种情况都是需要处理的。
下面先给出敌机类和方法。

class EnemyPlane(Sprite):def __init__(self, flightType, layerGroup, ePlaneGroup):self.groups = layerGroup, ePlaneGroupself.type = flightTypeself._layer = ENEMY_PLANES[self.type]['LAYER']super().__init__(self.groups)self.image = pygame.image.load(IMAGE_PATH + ENEMY_PLANES[self.type]['IMAGE']).convert_alpha()self.rect = Rect((random.randint(60, SCREEN_SIZE[0] - 60), 0), self.image.get_size())self.move_direction = random.choice([-1, 0, 1])     # 1表示向右, 0表示直线飞行self.defence = ENEMY_PLANES[self.type]['DEFENCE']self.destroyValue = ENEMY_PLANES[self.type]['COLLISION']self.bulletType = random.choice(list(ENEMY_BULLETS_TYPE.values()))self.missileType = random.choice(list(ENEMY_MISSILE_TYPE.values()))self.bShootCount = ENEMY_PLANES[self.type]['B_COUNT']self.mShootCount = ENEMY_PLANES[self.type]['M_COUNT']self.move_direction = random.randint(-1, 1)    # 左右移动时使用self.xSpeed = ENEMY_PLANES[self.type]['X_SPD']self.ySpeed = ENEMY_PLANES[self.type]['Y_SPD']self.speedupFactor = ENEMY_PLANES[self.type]['SP_FACTOR']# 被击落时是否释放包裹 ----------可以放在 is_killed()里面处理self.releasePowerPackage = random.randint(0, 300)self.released = Falseself.lastTick = 0self.shooting = 0def update(self, *args):tick = args[0]if tick < self.lastTick:returnself.lastTick = tickself.rect.bottom += self.ySpeed * self.speedupFactorif self.rect.right >= SCREEN_SIZE[0] - random.randint(0, 20):self.move_direction = -1elif self.rect.left <= random.randint(0, 20):self.move_direction = 1self.rect.left += self.move_direction * self.xSpeed# kill 飞出游戏界面的敌机if self.rect.top >= SCREEN_SIZE[1]:self.kill()# 降低射击速度,不然满屏都是子弹self.shooting += 1if self.shooting % 8 == 0:self.shoot_bullets()# 目前敌机最多一次只能发射一枚,最多二枚子弹,如果你想让敌机战力看起来更强,可以修改配制文件,并在下面增加发射处理。# 为了美观,最好考虑对称性def shoot_bullets(self):        if self.bShootCount == 1:Bullet(self.bulletType, (self.rect.centerx, self.rect.bottom + 6), self.groups[0], enemyBulletGroup)elif self.bShootCount == 2:Bullet(self.bulletType, (self.rect.centerx - 20, self.rect.bottom + 2), self.groups[0], enemyBulletGroup)Bullet(self.bulletType, (self.rect.centerx + 20, self.rect.bottom + 2), self.groups[0], enemyBulletGroup)else:passif self.mShootCount == 1:Bullet(self.missileType, (self.rect.centerx, self.rect.bottom + 6), self.groups[0], enemyBulletGroup)elif self.mShootCount == 2:Bullet(self.missileType, (self.rect.centerx - 20, self.rect.bottom + 2), self.groups[0], enemyBulletGroup)Bullet(self.missileType, (self.rect.centerx + 20, self.rect.bottom + 2), self.groups[0], enemyBulletGroup)else:pass# 生成 ReleasedPackage对象def release_package(self):if self.released :returnhBullet = int(self.releasePowerPackage > 290)Defence = int(self.releasePowerPackage > 295)hNuclear = int(self.releasePowerPackage == 300)powerType = PACKAGE_LIST[hBullet + Defence + hNuclear]if powerType is None:returnself.released = TrueReleasedPackage(powerType, self.rect.center, self.groups[0], packageGroup)# 被英雄子弹攻击def was_shot(self, bulletGroup, sound):for bullet in bulletGroup:if pygame.sprite.collide_rect(self, bullet):sound.play()self.defence -= bullet.destroyValueif self.defence <= 0:                    self.release_package()self.kill()bullet.kill()

可以看到,敌机的方法很简单,只有update(), shoot_bullets(),was_shot() 和 release_package(),非常简单。这里敌机被子弹击中并不是直接kill,而是根据自己的defence和子弹的destroyValue来处理,更接近真实情况。敌机坠毁时根据属性 releasePowerPackage 值(敌机出来时随机产生)判断是否释放包裹(可以根据配置文件来扩展各种武器、医药箱等)
ReleasedPackage类非常简单,如下:

class ReleasedPackage(Sprite):def __init__(self, bType, position, layerGroup, bGroup):self.type = bTypeself._layer = PACKAGES[self.type]['LAYER']self.groups = layerGroup, bGroupsuper().__init__(self.groups)self.image = pygame.image.load(IMAGE_PATH + PACKAGES[self.type]['IMAGE']).convert_alpha()self.rect = Rect(position, self.image.get_size())self.xSpeed = PACKAGES[self.type]['X_SPD']self.ySpeed = PACKAGES[self.type]['Y_SPD']self.speedupFactor = PACKAGES[self.type]['SP_FACTOR']self.direction = random.choice([-1, 1])self.lastTick = 0# 目前 子弹 和 导弹都是从发射点开始直线运行,后面将GALAXY的导弹改为自动寻敌模式def update(self, *args):tick = args[0]if tick < self.lastTick:returnself.lastTick = tick# 允许水平方向出界,不及时捡到就浪费self.rect.left += self.xSpeed * self.speedupFactor * self.directionself.rect.top += self.ySpeed * self.speedupFactor# 处理离开界面的包裹if self.rect.top < SCREEN_SIZE[1] or self.rect.right <= 0 or self.rect.left >= SCREEN_SIZE[0]:self.kill()

ReleasedPackage类非常简单,只有一个update()方法,用于更新位置和出界处理。

好了,本文就到这里,下一篇将给出英雄战机的类和相关处理。

Python游戏之Pygame——太空飞机大战(二)相关推荐

  1. Python游戏之Pygame——太空飞机大战(三)

    上一篇(Python游戏之Pygame--太空飞机大战(二))完成了敌机类以及敌机坠毁时释放包裹类,这一篇将给出英雄战机类和处理.由于英雄战机是由游戏者操控的,所以要处理操控事件,比如往那个方向飞,发 ...

  2. Python游戏之Pygame——太空飞机大战(四)

    上一篇(Python游戏之Pygame--太空飞机大战(三))完成了英雄战机和星空,那么基本上飞机大战的主要元素都已经完成,该是总结成功玩自己游戏的时候了. 哦,差点忘了,Bullet类对于普通子弹和 ...

  3. Python游戏之Pygame——太空飞机大战(一)

    学习Python,最好的办法是实战,实战!我们知道,有无数的先人和大侠提供了非常多的软件包供我们选用.有时候学习是一件很枯燥的事,玩游戏就不一样了.我们玩的最多的游戏是别人开发的,能不能开发出一款自己 ...

  4. 转载:python中的pygame编写飞机大战(一)游戏框架搭建

    作者:还在琢磨  来源:CSDN  原文:https://blog.csdn.net/mbl114/article/details/78074742  版权声明:本文为博主原创文章,转载请附上博文链接 ...

  5. 转载:python中的pygame编写飞机大战(三) 子弹类的实现

    作者:还在琢磨  来源:CSDN  原文:https://blog.csdn.net/mbl114/article/details/78075095  版权声明:本文为博主原创文章,转载请附上博文链接 ...

  6. C++ 与cocos2d-x-4.0完成太空飞机大战 (二)

    C++ 与cocos2d-x-4.0完成太空飞机大战 (二) 动画演示 飞机精灵编码:AircraftSprite.cpp 飞机精灵编码:AircraftSprite.h 飞机动画编码:Aircraf ...

  7. pygame实现飞机大战游戏

    标题:pygame实现飞机大战游戏 源码链接:我的github地址 一.具体演示 1.怪兽分为小怪,和大怪:大怪可以发射子弹 2.英雄飞机共有10个生命值 3.英雄飞机可以上下左右移动 4.显示了英雄 ...

  8. 用Unity快速开发太空飞机大战游戏实战经验分享(上)

    用unity动手先来试试一个简单的太空飞机大战吧.看官请继续往下... 最终效果,可控制己方战机,朝目标敌机发射子弹,打飞机~~~!伴随想象,慢慢呈现这个太空飞机大战游戏. 1. 新建打飞机unity ...

  9. C++ 与cocos2d-x-4.0完成太空飞机大战 (一)

    C++ 与cocos2d-x-4.0完成太空飞机大战 (一) 动画演示 AppDelegate编码:AppDelegate.cpp AppDelegate编码:AppDelegate.h 关卡1场景编 ...

最新文章

  1. ssh-keygen
  2. 学术报告 | 数据库专家C.Mohan ——人工智能的前世今生
  3. 渗透测试-基于白名单执行payload--Compiler
  4. XAML Namespace http://schemas.microsoft.com/expression/blend/2008 is not resolved
  5. JCO连接SAP例子
  6. 2018南京网络赛 G. Lpl and Energy-saving Lamps (线段树非递归实现)
  7. 九度OJ 1051:数字阶梯求和
  8. CodeForces - 1323C Unusual Competitions(贪心)
  9. nodemailer使用_如何使用Nodemailer使用HTML作为内容发送电子邮件 Node.js
  10. 爬虫-10-响应对象的常用属性
  11. Wireshark 的使用 —— 过滤器(filter)
  12. Cannot change version of project facet Dynamic Web Module to 3.0
  13. 金蝶k3服务器物理内存过高,金蝶k3提示超出内存解决方案
  14. 移远BC26使用总结
  15. ies4linux 本地安装,Linux下离线安装ies4linux
  16. 项目管理如何建立有效的团队沟通机制
  17. 2020电赛F题总结回顾(openmv实现视觉)
  18. 计算机网络-第1章-PPT
  19. 自动打印照片是如何实现的
  20. python中xlrd模块_Python中的xlrd模块使用原理解析

热门文章

  1. cubemx stm32 基于uln2003模块的步进电机驱动代码
  2. 合工大离散数学实验 数据输出
  3. C++学习书籍推荐《Exceptional C++(英文)》下载
  4. yzh第十四课 调试技巧选讲
  5. wincc通过vb如何读取mysql_WINCC通过VB脚本读取数据库数据-工业支持中心-西门子中国...
  6. 全球及中国制成品房屋、模块化房屋和移动式房屋行业研究及十四五规划分析报告
  7. 游戏系列~拳皇(7)
  8. android手表怎样刷机包,刷机精灵V2.1.2发布 智能手表也能一键刷机
  9. 有没有html代码听力的软件吗,听力软件VoScreen,值得介绍一下
  10. 新版excel下拉数字递增