当前位置: 首页 > news >正文

建设工程专注在哪个网站wordpress 页面 模板

建设工程专注在哪个网站,wordpress 页面 模板,好网站建设公司开发,网站icp备案证明文件学习 Python 之 Pygame 开发魂斗罗#xff08;十二#xff09;继续编写魂斗罗1. 修改玩家扣减生命值2. 解决玩家下蹲子弹不会击中玩家而是直接让玩家死亡的问题3. 完善地图4. 增加产生敌人函数#xff0c;解决一直产生敌人的问题5. 给玩家类增加计算玩家中心的方法继续编写魂… 学习 Python 之 Pygame 开发魂斗罗十二继续编写魂斗罗1. 修改玩家扣减生命值2. 解决玩家下蹲子弹不会击中玩家而是直接让玩家死亡的问题3. 完善地图4. 增加产生敌人函数解决一直产生敌人的问题5. 给玩家类增加计算玩家中心的方法继续编写魂斗罗 在上次的博客学习 Python 之 Pygame 开发魂斗罗十一中我们实现了敌人击中玩家碰到玩家玩家死亡的效果但是还有一点问题这次我们来解决一下之后加入一下地图碰撞体调整一下整体代码为加入新的敌人做准备 下面是图片的素材 链接https://pan.baidu.com/s/1X7tESkes_O6nbPxfpHD6hQ?pwdhdly 提取码hdly 1. 修改玩家扣减生命值 下面是玩家碰到敌人子弹的函数我们看到玩家生命值减少是修改变量的值 我们对代码进行修改把玩家扣减生命值的代码写成函数的形式 在玩家类中新增函数 def damage(self, damage):if not self.isInvincible:self.life - damagereturn Truereturn False再添加成员变量 self.isInvincible Trueself.isInvincible变量用来让玩家有无敌时间当玩家复活后没有落地之前应该是无敌的这样防止玩家刚复活就被子弹击中死亡了 damage函数用来扣减玩家生命值返回值表示是否扣减成功 我们来到主类把damage()函数调用一下 进入updatePlayerPosition()函数找到下方红框中的位置 修改代码 # 与敌人碰撞 if pygame.sprite.spritecollideany(MainGame.player1, MainGame.enemyGroup):if MainGame.player1.damage(1):MainGame.explodeList.append(Explode(MainGame.player1, ExplodeVariety.PLAYER1))initPlayer1(MainGame.player1.life)然后来到子弹类修改collidePlayer()函数 def collidePlayer(self, player, explodeList):# 函数的返回值用来表示是否要重新初始化玩家# 如果当前子弹和玩家发生碰撞if pygame.sprite.collide_rect(self, player):if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Truereturn False刚才我们设置了isInvincible变量此时运行游戏玩家一定是无敌的因为isInvincible初始值是True我们让玩家一落地变成不是无敌的 进入updatePlayerPosition()函数增加下面的代码 if MainGame.player1.isInvincible:# 玩家落地不无敌MainGame.player1.isInvincible False好了我们就修改完成了运行一下看看有没有问题 运行成功没有问题 2. 解决玩家下蹲子弹不会击中玩家而是直接让玩家死亡的问题 当我们蹲下时子弹不会从玩家上面飞过而是判断为玩家被击中 这个原因是因为玩家的图片很大判定碰撞的时候要进行判断如果玩家是蹲下的情况要计算子弹是否进入了玩家中心以下的范围 我们来到子弹类对colliderPlayer函数修改 def collidePlayer(self, player, explodeList):if pygame.sprite.collide_rect(self, player):# 蹲下的时候由于图片上半部分是空白所有子弹必须击中下半部分才判断为玩家被击中if player.isDown or player.isSquating:x player.rect.xy player.rect.y player.rect.height / 2 5if (x self.rect.x player.rect.x player.rect.width) and (y self.rect.y player.rect.y player.rect.height):if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Trueelif player.isInWater:x player.rect.xy player.rect.y player.rect.height / 2if (x self.rect.x player.rect.x player.rect.width) and (y self.rect.y player.rect.y player.rect.height):if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Trueelse:if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Truereturn False现在我们再运行一下看看效果 我们看到玩家蹲下时子弹会从玩家上面过去了 3. 完善地图 接下来我们把地图碰撞体完善一下修改主类函数initLand() def initLand():land1 Collider(81, 119 * MAP_SCALE, 737 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)# land1 Collider(81, 119 * MAP_SCALE, 8000 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land2 Collider(400, 151 * MAP_SCALE, 96 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land3 Collider(640, 183 * MAP_SCALE, 33 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land4 Collider(880, 183 * MAP_SCALE, 33 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land5 Collider(720, 215 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land6 Collider(1040, 154 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land7 Collider(1600, 166 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land8 Collider(1120 * RATIO, 215 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land9 Collider(1650 * RATIO, 119 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land10 Collider(2185 * RATIO, 119 * MAP_SCALE, 8 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land11 Collider(2595 * RATIO, 215 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land12 Collider(2770 * RATIO, 167 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land13 Collider(2535 * RATIO, 87 * MAP_SCALE, 16 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land14 Collider(2950 * RATIO, 151 * MAP_SCALE, 7 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land15 Collider(3185 * RATIO, 215 * MAP_SCALE, 6 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land16 Collider(3420 * RATIO, 119 * MAP_SCALE, 7 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land17 Collider(3537 * RATIO, 183 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land18 Collider(3715 * RATIO, 183 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land19 Collider(3890 * RATIO, 167 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land20 Collider(3775 * RATIO, 87 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land21 Collider(4010 * RATIO, 151 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land22 Collider(4125 * RATIO, 119 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land23 Collider(4304 * RATIO, 151 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land24 Collider(4304 * RATIO, 216 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land25 Collider(4361 * RATIO, 183 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land26 Collider(4537 * RATIO, 119 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land27 Collider(4598 * RATIO, 87 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land28 Collider(4657 * RATIO, 167 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land29 Collider(4598 * RATIO, 216 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land30 Collider(4776 * RATIO, 119 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land31 Collider(4835 * RATIO, 151 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land32 Collider(5010 * RATIO, 216 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land33 Collider(5250 * RATIO, 183 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land34 Collider(5423 * RATIO, 151 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land35 Collider(5543 * RATIO, 119 * MAP_SCALE, 4 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land36 Collider(5601 * RATIO, 167 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land37 Collider(5541 * RATIO, 216 * MAP_SCALE, 8 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land38 Collider(5776 * RATIO, 151 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land39 Collider(5836 * RATIO, 183 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)MainGame.playerLandGroup pygame.sprite.Group(land1, land2, land3, land4, land5, land6, land7, land8, land9, land10,land11, land12, land13, land14, land15, land16, land17, land18, land19, land20,land21, land22, land23, land24, land25, land26, land27, land28, land29, land30,land31, land32, land33, land34, land35, land36, land37, land38, land39)eland1 Collider(81, 119 * MAP_SCALE, 737 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)eland8 Collider(1120 * RATIO, 215 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)eland9 Collider(1650 * RATIO, 119 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)eland10 Collider(2185 * RATIO, 119 * MAP_SCALE, 8 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)MainGame.enemyLandGroup pygame.sprite.Group(eland1, eland8, eland9, eland10)MainGame.playerColliderGroup.add(MainGame.playerLandGroup)MainGame.enemyColliderGroup.add(MainGame.enemyLandGroup)这张图是碰撞体图 我们现在再来运行一下 我们可以看到现在地图后面都有陆地碰撞体一直持续到boss那里 4. 增加产生敌人函数解决一直产生敌人的问题 在现在的代码中我们产生敌人的逻辑直接写在了update()函数里为了方便管理我们把代码提出来写成函数 把上面红框圈出的代码提出来写成函数 但是我们之前已经定义过generateEnemy()函数了 我们把原来的generateEnemy()函数改名为generateEnemy1()表示创建的是敌人1并且把它变为全局函数 generateEnemy()表示创建的是全部敌人 def generateEnemy1(self, x, y, direction, currentTime):# 根据玩家的当前位置和方向产生一个敌人enemy Enemy1(x, y, direction, currentTime)# 分别加入敌人列表所有角色组敌人碰撞组MainGame.enemyList.append(enemy)MainGame.allSprites.add(enemy)MainGame.enemyGroup.add(enemy)之后添加成员变量 self.enemyBoolList [True for _ in range(5)]这个数组用来指定敌人产生个数的这里写的5表示有五个位置产生敌人 那么为什么要有这个数组呢 在原来的产生敌人代码中 if -1505 self.backRect.x -1500:generateEnemy1(MainGame.player1.rect.x 600, POSITION_1, Direction.LEFT, pygame.time.get_ticks())generateEnemy1(MainGame.player1.rect.x - 360, POSITION_1, Direction.RIGHT, pygame.time.get_ticks())我们可以看到当玩家进入了这个范围实际是地图移动到了这个范围就会产生敌人但是如果移动到这个范围后玩家停止移动了那么就会一直产生敌人这显然不是我们想看到的我们想的是玩家走到范围内就产生敌人代码只执行一次但是按照原来的逻辑如果玩家一直站在范围里面代码就会一直执行所以我们得设置一个enemyBoolList如果是True就执行代码否则就不执行 修改后的产生敌人的代码如下 这里就设置了只执行一次产生敌人的代码列表的长度表示敌人产生点的个数 def generateEnemy(self):if -1505 self.backRect.x -1500:if self.enemyBoolList[0]:self.enemyBoolList[0] FalsegenerateEnemy1(MainGame.player1.rect.x 600, POSITION_1, Direction.LEFT, pygame.time.get_ticks())generateEnemy1(MainGame.player1.rect.x - 360, POSITION_1, Direction.RIGHT, pygame.time.get_ticks())if -1705 self.backRect.x -1700:if self.enemyBoolList[1]:self.enemyBoolList[1] FalsegenerateEnemy1(MainGame.player1.rect.x - 360, POSITION_1, Direction.RIGHT, pygame.time.get_ticks())generateEnemy1(MainGame.player1.rect.x - 400, POSITION_1, Direction.RIGHT,pygame.time.get_ticks())我们运行一下游戏看看有没有问题 ok一切正常 5. 给玩家类增加计算玩家中心的方法 敌人2发射的子弹带有追踪效果因此我们要时刻计算玩家的中心 我们给玩家类加入代码 def getCenter(self):return self.rect.x self.rect.width / 2, self.rect.y self.rect.height / 2 y0这个代码是计算玩家中心位置的 y0是中心的偏移量我们在Constants.py中设置 至此就完啦接下来就是创建敌人2了 完整的主类代码 import copy import sys import pygame from Constants import * from PlayerOne import PlayerOne from Collider import Collider from Enemy1 import Enemy1 from Explode import Explodedef drawPlayerOneBullet(player1BulletList):for bullet in player1BulletList:if bullet.isDestroy:player1BulletList.remove(bullet)else:bullet.draw(MainGame.window)bullet.move()bullet.collideEnemy(MainGame.enemyList, MainGame.explodeList)def enemyUpdate(enemyList, enemyBulletList):# 遍历整个敌人列表for enemy in enemyList:# 如果敌人已经被摧毁了if enemy.isDestroy:# 删除它的相关信息enemyList.remove(enemy)MainGame.allSprites.remove(enemy)MainGame.enemyGroup.remove(enemy)# 否则else:# 检查位置enemy.checkPosition(MainGame.player1.rect.x, MainGame.player1.rect.y)# 显示敌人enemy.draw(pygame.time.get_ticks())# 敌人移动enemy.move(pygame.time.get_ticks())# 敌人开火enemy.fire(enemyBulletList)def updateEnemyPosition():# 遍历全部敌人列表for enemy in MainGame.enemyList:# 创建一个复制t copy.copy(enemy)t.rect.y 1# 让复制的y加1看看有没有发生碰撞这里看的碰撞是enemyColliderGroup中的碰撞collide pygame.sprite.spritecollideany(t, MainGame.enemyColliderGroup)# 没有发生碰撞让敌人下落if not collide:enemy.rect.y 4enemy.isFalling True# 改变下落时的图片enemy.image enemy.rightFallImage if enemy.direction Direction.RIGHT else enemy.leftFallImageelse:enemy.isFalling False# 如果与河发生碰撞表示敌人落到了水中那么敌人直接死亡if collide in MainGame.enemyRiverGroup:enemy.isDestroy TrueMainGame.explodeList.append(Explode(enemy))t.rect.y - 1def drawEnemyBullet(enemyBulletList):for bullet in enemyBulletList:if bullet.isDestroy:enemyBulletList.remove(bullet)else:bullet.draw(MainGame.window)bullet.move()if bullet.collidePlayer(MainGame.player1, MainGame.explodeList):initPlayer1(MainGame.player1.life)def initLand():land1 Collider(81, 119 * MAP_SCALE, 737 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)# land1 Collider(81, 119 * MAP_SCALE, 8000 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land2 Collider(400, 151 * MAP_SCALE, 96 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land3 Collider(640, 183 * MAP_SCALE, 33 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land4 Collider(880, 183 * MAP_SCALE, 33 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land5 Collider(720, 215 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land6 Collider(1040, 154 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land7 Collider(1600, 166 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land8 Collider(1120 * RATIO, 215 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land9 Collider(1650 * RATIO, 119 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land10 Collider(2185 * RATIO, 119 * MAP_SCALE, 8 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land11 Collider(2595 * RATIO, 215 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land12 Collider(2770 * RATIO, 167 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land13 Collider(2535 * RATIO, 87 * MAP_SCALE, 16 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land14 Collider(2950 * RATIO, 151 * MAP_SCALE, 7 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land15 Collider(3185 * RATIO, 215 * MAP_SCALE, 6 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land16 Collider(3420 * RATIO, 119 * MAP_SCALE, 7 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land17 Collider(3537 * RATIO, 183 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land18 Collider(3715 * RATIO, 183 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land19 Collider(3890 * RATIO, 167 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land20 Collider(3775 * RATIO, 87 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land21 Collider(4010 * RATIO, 151 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land22 Collider(4125 * RATIO, 119 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land23 Collider(4304 * RATIO, 151 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land24 Collider(4304 * RATIO, 216 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land25 Collider(4361 * RATIO, 183 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land26 Collider(4537 * RATIO, 119 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land27 Collider(4598 * RATIO, 87 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land28 Collider(4657 * RATIO, 167 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land29 Collider(4598 * RATIO, 216 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land30 Collider(4776 * RATIO, 119 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land31 Collider(4835 * RATIO, 151 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land32 Collider(5010 * RATIO, 216 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land33 Collider(5250 * RATIO, 183 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land34 Collider(5423 * RATIO, 151 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land35 Collider(5543 * RATIO, 119 * MAP_SCALE, 4 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land36 Collider(5601 * RATIO, 167 * MAP_SCALE, 3 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land37 Collider(5541 * RATIO, 216 * MAP_SCALE, 8 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land38 Collider(5776 * RATIO, 151 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)land39 Collider(5836 * RATIO, 183 * MAP_SCALE, 1 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)MainGame.playerLandGroup pygame.sprite.Group(land1, land2, land3, land4, land5, land6, land7, land8, land9, land10,land11, land12, land13, land14, land15, land16, land17, land18, land19, land20,land21, land22, land23, land24, land25, land26, land27, land28, land29, land30,land31, land32, land33, land34, land35, land36, land37, land38, land39)eland1 Collider(81, 119 * MAP_SCALE, 737 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)eland8 Collider(1120 * RATIO, 215 * MAP_SCALE, 2 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)eland9 Collider(1650 * RATIO, 119 * MAP_SCALE, 5 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)eland10 Collider(2185 * RATIO, 119 * MAP_SCALE, 8 * LAND_LENGTH * MAP_SCALE, LAND_THICKNESS * MAP_SCALE)MainGame.enemyLandGroup pygame.sprite.Group(eland1, eland8, eland9, eland10)MainGame.playerColliderGroup.add(MainGame.playerLandGroup)MainGame.enemyColliderGroup.add(MainGame.enemyLandGroup)def initRiver():river1 Collider(0, 215 * MAP_SCALE, 289 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE, (0, 0, 255))river2 Collider(880, 215 * MAP_SCALE, 255 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE, (0, 0, 255))river3 Collider(1680, 215 * MAP_SCALE, 737 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE, (0, 0, 255))eRiver1 Collider(0, 215 * MAP_SCALE, 289 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE, (0, 0, 255))eRiver3 Collider(1680, 215 * MAP_SCALE, 737 * MAP_SCALE, LAND_THICKNESS * MAP_SCALE, (0, 0, 255))MainGame.playerRiverGroup pygame.sprite.Group(river1, river2, river3)MainGame.enemyRiverGroup pygame.sprite.Group(eRiver1, eRiver3)MainGame.playerColliderGroup.add(MainGame.playerRiverGroup)MainGame.enemyColliderGroup.add(MainGame.enemyRiverGroup)def drawExplode(explodeList):for explode in explodeList:if explode.isDestroy:explodeList.remove(explode)else:if explode.isUseTime:explode.draw(MainGame.window, pygame.time.get_ticks())else:explode.draw(MainGame.window)def initPlayer1(life):if life 0:passMainGame.allSprites.remove(MainGame.player1)MainGame.player1 PlayerOne(pygame.time.get_ticks(), life)MainGame.player1.rect.x 80MainGame.player1.rect.bottom 0# 把角色放入组中方便统一管理MainGame.allSprites.add(MainGame.player1)def generateEnemy1(x, y, direction, currentTime):# 根据玩家的当前位置和方向产生一个敌人enemy Enemy1(x, y, direction, currentTime)# 分别加入敌人列表所有角色组敌人碰撞组MainGame.enemyList.append(enemy)MainGame.allSprites.add(enemy)MainGame.enemyGroup.add(enemy)class MainGame:player1 NoneallSprites pygame.sprite.Group()# 敌人enemyList []window None# 子弹player1BulletList []enemyBulletList []# 爆炸效果explodeList []# 冲突playerLandGroup pygame.sprite.Group()playerRiverGroup pygame.sprite.Group()enemyLandGroup pygame.sprite.Group()enemyRiverGroup pygame.sprite.Group()playerColliderGroup pygame.sprite.Group()enemyColliderGroup pygame.sprite.Group()enemyGroup pygame.sprite.Group()bridgeGroup pygame.sprite.Group()# 冲突栈colliderStack []def __init__(self):# 设置成员变量self.background Noneself.backRect Noneself.enemyBoolList [True for _ in range(5)]# 初始化展示模块pygame.display.init()SCREEN_SIZE (SCREEN_WIDTH, SCREEN_HEIGHT)# 初始化窗口MainGame.window pygame.display.set_mode(SCREEN_SIZE)# 设置窗口标题pygame.display.set_caption(魂斗罗角色)# 是否结束游戏self.isEnd False# 获取按键self.keys pygame.key.get_pressed()# 帧率self.fps 60self.clock pygame.time.Clock()# 角色initPlayer1(3)# 加载背景self.initBackground()# 摄像头调整self.cameraAdaption 0# 加载场景景物initLand()initRiver()# 碰撞失效间隔self.index 0# 显示玩家生命值self.lifeImage loadImage(../Image/Player/Player1/Life/life.png)def run(self):while not self.isEnd:# 设置背景颜色pygame.display.get_surface().fill((0, 0, 0))# 游戏场景和景物更新函数self.update(MainGame.window, MainGame.player1BulletList)# 获取窗口中的事件self.getPlayingModeEvent()# 更新窗口pygame.display.update()# 设置帧率self.clock.tick(self.fps)fps self.clock.get_fps()caption 魂斗罗 - {:.2f}.format(fps)pygame.display.set_caption(caption)else:sys.exit()def getPlayingModeEvent(self):# 获取事件列表for event in pygame.event.get():# 点击窗口关闭按钮if event.type pygame.QUIT:self.isEnd True# 键盘按键按下elif event.type pygame.KEYDOWN:self.keys pygame.key.get_pressed()# 键盘按键抬起elif event.type pygame.KEYUP:self.keys pygame.key.get_pressed()def update(self, window, player1BulletList):# 加载背景window.blit(self.background, self.backRect)# 显示生命图标self.drawLifeImage(MainGame.window)# 敌人更新enemyUpdate(MainGame.enemyList, MainGame.enemyBulletList)drawExplode(MainGame.explodeList)drawPlayerOneBullet(MainGame.player1BulletList)drawEnemyBullet(MainGame.enemyBulletList)# 更新人物currentTime pygame.time.get_ticks()MainGame.allSprites.update(self.keys, currentTime, player1BulletList)self.updatePlayerPosition()updateEnemyPosition()# 摄像机移动self.camera()# 显示物体MainGame.allSprites.draw(window)# 加载敌人self.generateEnemy()for collider in MainGame.playerLandGroup:r collider.draw(window, self.player1.rect.y)# 如果没有画出来表示玩家高度低于直线所有把直线从组中删除if not r:# 删除前先检查一下是不是在组中if collider in MainGame.playerColliderGroup:# 删除并加入栈MainGame.colliderStack.insert(0, collider)MainGame.playerColliderGroup.remove(collider)else:# 如果画出来了判断一下玩家距离是否高于线的距离if collider.rect.y self.player1.rect.bottom:# 如果是的话且冲突栈不为空那么从栈中取出一个元素放入冲突组最前面的元素一定是先如队列的if len(MainGame.colliderStack) 0:f MainGame.colliderStack.pop()MainGame.playerColliderGroup.add(f)MainGame.playerRiverGroup.draw(window)def camera(self):# 如果玩家的右边到达了屏幕的一半if self.player1.rect.right SCREEN_WIDTH / 2:if not (self.backRect.x -3500 * MAP_SCALE):# 计算出超过的距离self.cameraAdaption self.player1.rect.right - SCREEN_WIDTH / 2# 让背景向右走这么多距离self.backRect.x - self.cameraAdaption# 场景中的物体都走这么多距离self.mapObjectMove()def mapObjectMove(self):for sprite in MainGame.allSprites:sprite.rect.x - self.cameraAdaptionfor collider in MainGame.playerColliderGroup:collider.rect.x - self.cameraAdaptionfor collider in MainGame.colliderStack:collider.rect.x - self.cameraAdaptionfor collider in MainGame.enemyColliderGroup:collider.rect.x - self.cameraAdaptiondef updatePlayerPosition(self):# 在index的循环次数中不进行碰撞检测用来让玩家向下跳跃if self.index 0:self.index - 1self.player1.rect.x self.player1.xSpeedself.player1.rect.y self.player1.ySpeedself.player1.isDown Falseelse:# 首先更新y的位置self.player1.rect.y self.player1.ySpeed# 玩家向下跳跃35次循环内不进行碰撞检测if self.player1.state State.JUMP and self.player1.isDown:self.index 35# 玩家向上跳跃15次循环内不进行碰撞检测elif self.player1.state State.JUMP and self.player1.isUp:self.index 15else:# 检测碰撞# 这里是玩家和所有碰撞组中的碰撞体检测碰撞如果发生了碰撞就会返回碰撞到的碰撞体对象collider pygame.sprite.spritecollideany(self.player1, MainGame.playerColliderGroup)# 如果发生碰撞判断是不是在河里if collider in MainGame.playerRiverGroup:self.riverCollide()# 判断是不是在陆地上elif collider in MainGame.playerLandGroup:self.player1.isInWater False# 如果发生碰撞if collider:if MainGame.player1.isInvincible:# 玩家落地不无敌MainGame.player1.isInvincible False# 判断一下人物的y速度如果大于0则说明玩家已经接触到了碰撞体表面需要让玩家站在表面不掉下去if self.player1.ySpeed 0:self.player1.ySpeed 0self.player1.state State.WALKself.player1.rect.bottom collider.rect.topelse:# 否则的话我们创建一个玩家的复制tempPlayer copy.copy(self.player1)# 让玩家的纵坐标—1看看有没有发生碰撞tempPlayer.rect.y 1# 如果没有发生碰撞就说明玩家下面不是碰撞体是空的if not pygame.sprite.spritecollideany(tempPlayer, MainGame.playerColliderGroup):# 如果此时不是跳跃状态那么就让玩家变成下落状态因为玩家在跳跃时是向上跳跃不需要对下面的物体进行碰撞检测if tempPlayer.state ! State.JUMP:self.player1.state State.FALLtempPlayer.rect.y - 1# 与敌人碰撞if pygame.sprite.spritecollideany(MainGame.player1, MainGame.enemyGroup):if MainGame.player1.damage(1):MainGame.explodeList.append(Explode(MainGame.player1, ExplodeVariety.PLAYER1))initPlayer1(MainGame.player1.life)# 更新x的位置self.player1.rect.x self.player1.xSpeed# 同样的检查碰撞collider pygame.sprite.spritecollideany(self.player1, MainGame.playerColliderGroup)# 如果发生了碰撞if collider:# 判断玩家的x方向速度如果大于0表示右边有碰撞体if self.player1.xSpeed 0:# 设置玩家的右边等于碰撞体的左边self.player1.rect.right collider.rect.leftelse:# 左边有碰撞体self.player1.rect.left collider.rect.rightself.player1.xSpeed 0tempPlayer copy.copy(self.player1)tempPlayer.rect.y 1if c : pygame.sprite.spritecollideany(tempPlayer, MainGame.playerColliderGroup):if c in MainGame.playerLandGroup:self.player1.isInWater Falseelif c in MainGame.playerRiverGroup:self.player1.isInWater TruetempPlayer.rect.y - 1def riverCollide(self):# 在河里设置isInWaterself.player1.isInWater True# 设置玩家在河里不能跳跃self.player1.isJumping False# 默认落下去是站在河里的self.player1.isStanding True# 玩家方向不能向下self.player1.isDown False# 根据玩家方向加载落入河中的一瞬间的图片if self.player1.direction Direction.RIGHT:self.player1.image self.player1.rightInWaterImageelse:self.player1.image self.player1.leftInWaterImagedef generateEnemy(self):if -1505 self.backRect.x -1500:if self.enemyBoolList[0]:self.enemyBoolList[0] FalsegenerateEnemy1(MainGame.player1.rect.x 600, POSITION_1, Direction.LEFT, pygame.time.get_ticks())generateEnemy1(MainGame.player1.rect.x - 360, POSITION_1, Direction.RIGHT, pygame.time.get_ticks())if -1705 self.backRect.x -1700:if self.enemyBoolList[1]:self.enemyBoolList[1] FalsegenerateEnemy1(MainGame.player1.rect.x - 360, POSITION_1, Direction.RIGHT, pygame.time.get_ticks())generateEnemy1(MainGame.player1.rect.x - 400, POSITION_1, Direction.RIGHT,pygame.time.get_ticks())def initBackground(self):# 读取背景图片self.background pygame.image.load(../Image/Map/1/Background/First(No Bridge).png)self.backRect self.background.get_rect()self.background pygame.transform.scale(self.background,(int(self.backRect.width * MAP_SCALE),int(self.backRect.height * MAP_SCALE)))self.backRect.x -1280def drawLifeImage(self, window):# 如果玩家的生命值大于3那么生命值图标就显示3个if MainGame.player1.life 3:number 3# 否则有几个显示几个肯定不超过三个else:number MainGame.player1.liferect self.lifeImage.get_rect()# 设置生命值图标的显示位置rect.y 5for i in range(number):# 每个图标之间的距离为25像素rect.x 5 i * 20window.blit(self.lifeImage, rect)if __name__ __main__:MainGame().run()完整的玩家类代码 from Constants import * from Bullet import Bulletclass PlayerOne(pygame.sprite.Sprite):def __init__(self, currentTime, life):pygame.sprite.Sprite.__init__(self)# 加载角色图片self.standRightImage loadImage(../Image/Player/Player1/Right/stand.png)self.standLeftImage loadImage(../Image/Player/Player1/Left/stand.png)self.upRightImage loadImage(../Image/Player/Player1/Up/upRight(small).png)self.upLeftImage loadImage(../Image/Player/Player1/Up/upLeft(small).png)self.downRightImage loadImage(../Image/Player/Player1/Down/down.png)self.downLeftImage loadImage(../Image/Player/Player1/Down/down.png, True)self.obliqueUpRightImages [loadImage(../Image/Player/Player1/Up/rightUp1.png),loadImage(../Image/Player/Player1/Up/rightUp2.png),loadImage(../Image/Player/Player1/Up/rightUp3.png),]self.obliqueUpLeftImages [loadImage(../Image/Player/Player1/Up/rightUp1.png, True),loadImage(../Image/Player/Player1/Up/rightUp2.png, True),loadImage(../Image/Player/Player1/Up/rightUp3.png, True),]self.obliqueDownRightImages [loadImage(../Image/Player/Player1/ObliqueDown/1.png),loadImage(../Image/Player/Player1/ObliqueDown/2.png),loadImage(../Image/Player/Player1/ObliqueDown/3.png),]self.obliqueDownLeftImages [loadImage(../Image/Player/Player1/ObliqueDown/1.png, True),loadImage(../Image/Player/Player1/ObliqueDown/2.png, True),loadImage(../Image/Player/Player1/ObliqueDown/3.png, True),]# 角色向右的全部图片self.rightImages [loadImage(../Image/Player/Player1/Right/run1.png),loadImage(../Image/Player/Player1/Right/run2.png),loadImage(../Image/Player/Player1/Right/run3.png)]# 角色向左的全部图片self.leftImages [loadImage(../Image/Player/Player1/Left/run1.png),loadImage(../Image/Player/Player1/Left/run2.png),loadImage(../Image/Player/Player1/Left/run3.png)]# 角色跳跃的全部图片self.upRightImages [loadImage(../Image/Player/Player1/Jump/jump1.png),loadImage(../Image/Player/Player1/Jump/jump2.png),loadImage(../Image/Player/Player1/Jump/jump3.png),loadImage(../Image/Player/Player1/Jump/jump4.png),]self.upLeftImages [loadImage(../Image/Player/Player1/Jump/jump1.png, True),loadImage(../Image/Player/Player1/Jump/jump2.png, True),loadImage(../Image/Player/Player1/Jump/jump3.png, True),loadImage(../Image/Player/Player1/Jump/jump4.png, True),]self.rightFireImages [loadImage(../Image/Player/Player1/Right/fire1.png),loadImage(../Image/Player/Player1/Right/fire2.png),loadImage(../Image/Player/Player1/Right/fire3.png),]self.leftFireImages [loadImage(../Image/Player/Player1/Right/fire1.png, True),loadImage(../Image/Player/Player1/Right/fire2.png, True),loadImage(../Image/Player/Player1/Right/fire3.png, True),]# 加载玩家在水中的图片self.upRightImageInWater loadImage(../Image/Player/Player1/Water/up.png)self.upLeftImageInWater loadImage(../Image/Player/Player1/Water/up.png, True)self.diveRightImageInWater loadImage(../Image/Player/Player1/Water/dive.png)self.diveLeftImageInWater loadImage(../Image/Player/Player1/Water/dive.png, True)self.standRightImageInWater loadImage(../Image/Player/Player1/Water/stand.png)self.standLeftImageInWater loadImage(../Image/Player/Player1/Water/stand.png, True)self.fireRightInWater loadImage(../Image/Player/Player1/Water/standFire.png)self.fireLeftInWater loadImage(../Image/Player/Player1/Water/standFire.png, True)self.obliqueRightInWater loadImage(../Image/Player/Player1/Water/obliqueRight.png)self.obliqueLeftInWater loadImage(../Image/Player/Player1/Water/obliqueRight.png, True)self.rightInWaterImage loadImage(../Image/Player/Player1/Water/inWater.png)self.leftInWaterImage loadImage(../Image/Player/Player1/Water/inWater.png, True)# 角色左右移动下标self.imageIndex 0# 角色跳跃下标self.upImageIndex 0# 角色斜射下标self.obliqueImageIndex 0# 上一次显示图片的时间self.runLastTimer currentTimeself.fireLastTimer currentTime# 选择当前要显示的图片self.image self.standRightImage# 获取图片的rectself.rect self.image.get_rect()# 设置角色的状态self.state State.FALL# 角色的方向self.direction Direction.RIGHT# 速度self.xSpeed PLAYER_X_SPEEDself.ySpeed 0self.jumpSpeed -11# 人物当前的状态标志self.isStanding Falseself.isWalking Falseself.isJumping Trueself.isSquating Falseself.isFiring Falseself.isInWater False# 重力加速度self.gravity 0.8# 玩家上下方向self.isUp Falseself.isDown Falseself.life lifeself.isInvincible Truedef update(self, keys, currentTime, playerBulletList):# 更新站或者走的状态# 根据状态响应按键if self.state State.STAND:self.standing(keys, currentTime, playerBulletList)elif self.state State.WALK:self.walking(keys, currentTime, playerBulletList)elif self.state State.JUMP:self.jumping(keys, currentTime, playerBulletList)elif self.state State.FALL:self.falling(keys, currentTime, playerBulletList)# 更新动画if self.isInWater:self.waterUpdate()else:self.landUpdate()def landUpdate(self):# 跳跃状态if self.isJumping:# 根据方向if self.direction Direction.RIGHT:# 方向向右角色加载向右跳起的图片self.image self.upRightImages[self.upImageIndex]else:# 否则方向向左角色加载向左跳起的图片self.image self.upLeftImages[self.upImageIndex]# 角色蹲下if self.isSquating:if self.direction Direction.RIGHT:# 加载向右蹲下的图片self.image self.downRightImageelse:# 加载向左蹲下的图片self.image self.downLeftImage# 角色站着if self.isStanding:if self.direction Direction.RIGHT:if self.isUp:# 加载向右朝上的图片self.image self.upRightImageelif self.isDown:# 加载向右蹲下的图片self.image self.downRightImageelse:# 加载向右站着的图片self.image self.standRightImageelse:# 向左也是同样的效果if self.isUp:self.image self.upLeftImageelif self.isDown:self.image self.downLeftImageelse:self.image self.standLeftImage# 角色移动if self.isWalking:if self.direction Direction.RIGHT:if self.isUp:# 加载斜右上的图片self.image self.obliqueUpRightImages[self.obliqueImageIndex]elif self.isDown:# 加载斜右下的图片self.image self.obliqueDownRightImages[self.obliqueImageIndex]else:# 加载向右移动的图片根据开火状态是否加载向右开火移动的图片if self.isFiring:self.image self.rightFireImages[self.imageIndex]else:self.image self.rightImages[self.imageIndex]else:if self.isUp:self.image self.obliqueUpLeftImages[self.obliqueImageIndex]elif self.isDown:self.image self.obliqueDownLeftImages[self.obliqueImageIndex]else:if self.isFiring:self.image self.leftFireImages[self.imageIndex]else:self.image self.leftImages[self.imageIndex]def waterUpdate(self):if self.isSquating:if self.direction Direction.RIGHT:self.image self.diveRightImageInWaterelse:self.image self.diveLeftImageInWaterif self.isStanding:if self.direction Direction.RIGHT:if self.isFiring:if self.isUp:self.image self.upRightImageInWaterelse:self.image self.fireRightInWaterelse:if self.isUp:self.image self.upRightImageInWaterelse:self.image self.standRightImageInWaterelse:if self.isFiring:if self.isUp:self.image self.upLeftImageInWaterelse:self.image self.fireLeftInWaterelse:if self.isUp:self.image self.upLeftImageInWaterelse:self.image self.standLeftImageInWaterif self.isWalking:if self.direction Direction.RIGHT:if self.isUp:self.image self.obliqueRightInWaterelse:if self.isFiring:self.image self.fireRightInWaterelse:self.image self.standRightImageInWaterelse:if self.isUp:self.image self.obliqueLeftInWaterelse:if self.isFiring:self.image self.fireLeftInWaterelse:self.image self.standLeftImageInWaterdef standing(self, keys, currentTime, playerBulletList):角色站立# 设置角色状态self.isStanding Trueself.isWalking Falseself.isJumping Falseself.isSquating Falseself.isUp Falseself.isDown Falseself.isFiring False# 设置速度self.ySpeed 0self.xSpeed 0# 按下A键if keys[pygame.K_a]:# A按下角色方向向左self.direction Direction.LEFT# 改变角色的状态角色进入移动状态self.state State.WALK# 设置站立状态为False移动状态为Trueself.isStanding Falseself.isWalking True# 向左移动速度为负数这样玩家的x坐标是减小的self.xSpeed -PLAYER_X_SPEED# 按下D键elif keys[pygame.K_d]:# D按下角色方向向右self.direction Direction.RIGHT# 改变角色的状态角色进入移动状态self.state State.WALK# 设置站立状态为False移动状态为Trueself.isStanding Falseself.isWalking True# 向右移动速度为正数self.xSpeed PLAYER_X_SPEED# 按下k键elif keys[pygame.K_k]:if not self.isInWater:# K按下角色进入跳跃状态但是不会改变方向self.state State.JUMP# 设置站立状态为False跳跃状态为True# 不改变移动状态因为移动的时候也可以跳跃self.isStanding Falseself.isJumping True# 设置速度速度为负数因为角色跳起后要下落self.isUp Trueself.ySpeed self.jumpSpeed# 没有按下按键else:# 没有按下按键角色依然是站立状态self.state State.STANDself.isStanding True# 按下w键if keys[pygame.K_w]:# W按下角色向上改变方向状态self.isUp Trueself.isStanding Trueself.isDown Falseself.isSquating False# 按下s键elif keys[pygame.K_s]:# S按下角色蹲下改变方向状态并且蹲下状态设置为Trueself.isUp Falseself.isStanding Falseself.isDown Trueself.isSquating Trueif keys[pygame.K_j]:self.fire(currentTime, playerBulletList)def walking(self, keys, currentTime, playerBulletList):角色行走每10帧变换一次图片self.isStanding Falseself.isWalking Trueself.isJumping Falseself.isSquating Falseself.isFiring Falseself.ySpeed 0self.xSpeed PLAYER_X_SPEEDif self.isInWater:self.walkingInWater(currentTime)else:self.walkingInLand(currentTime)# 按下D键if keys[pygame.K_d]:self.direction Direction.RIGHTself.xSpeed PLAYER_X_SPEED# 按下A键elif keys[pygame.K_a]:self.direction Direction.LEFTself.xSpeed -PLAYER_X_SPEED# 按下S键elif keys[pygame.K_s]:self.isStanding Falseself.isDown Trueself.isUp False# 按下W键if keys[pygame.K_w]:self.isUp Trueself.isDown False# 没有按键按下else:self.state State.STAND# 移动时按下K键if keys[pygame.K_k]:# 角色状态变为跳跃if not self.isInWater:self.state State.JUMPself.ySpeed self.jumpSpeedself.isJumping Trueself.isStanding Falseself.isUp Trueif keys[pygame.K_j]:self.fire(currentTime, playerBulletList)def walkingInLand(self, currentTime):# 如果当前是站立的图片if self.isStanding:# 方向向右方向向上if self.direction Direction.RIGHT and self.isUp:# 设置为向右朝上的图片self.image self.upRightImage# 方向向右elif self.direction Direction.RIGHT and not self.isUp:# 设置为向右站立的图片self.image self.standRightImageelif self.direction Direction.LEFT and self.isUp:self.image self.upLeftImageelif self.direction Direction.LEFT and not self.isUp:self.image self.standLeftImage# 记下当前时间self.runLastTimer currentTimeelse:# 如果是走动的图片先判断方向if self.direction Direction.RIGHT:# 设置速度self.xSpeed PLAYER_X_SPEED# 根据上下方向觉得是否角色要加载斜射的图片if self.isUp or self.isDown:# isUp True表示向上斜射# isDown True表示向下斜射# 计算上一次加载图片到这次的时间如果大于115即11.5帧即上次加载图片到这次加载图片之间已经加载了11张图片if currentTime - self.runLastTimer 115:# 那么就可以加载斜着奔跑的图片# 如果角色加载的图片不是第三张则加载下一张就行if self.obliqueImageIndex 2:self.obliqueImageIndex 1# 否则就加载第一张图片else:self.obliqueImageIndex 0# 记录变换图片的时间为下次变换图片做准备self.runLastTimer currentTime# 不是斜射else:# 加载正常向右奔跑的图片if currentTime - self.runLastTimer 115:if self.imageIndex 2:self.imageIndex 1else:self.imageIndex 0self.runLastTimer currentTimeelse:self.xSpeed -PLAYER_X_SPEEDif self.isUp or self.isDown:if currentTime - self.runLastTimer 115:if self.obliqueImageIndex 2:self.obliqueImageIndex 1else:self.obliqueImageIndex 0self.runLastTimer currentTimeelse:if currentTime - self.runLastTimer 115:if self.imageIndex 2:self.imageIndex 1else:self.imageIndex 0self.runLastTimer currentTimedef walkingInWater(self, currentTime):if self.isStanding:# 设置为斜射if self.direction Direction.RIGHT and self.isUp:self.image self.upRightImageInWaterelif self.direction Direction.RIGHT and not self.isUp:self.image self.standRightImageInWaterelif self.direction Direction.LEFT and self.isUp:self.image self.upLeftImageInWaterelif self.direction Direction.LEFT and not self.isUp:self.image self.standLeftImageInWaterself.runLastTimer currentTimeelse:# 如果是走动的图片if self.direction Direction.RIGHT:self.xSpeed PLAYER_X_SPEEDif self.isUp:self.image self.obliqueRightInWaterself.runLastTimer currentTimeelse:self.image self.standRightImageInWaterself.runLastTimer currentTimeelse:self.xSpeed PLAYER_X_SPEEDif self.isUp:self.image self.obliqueLeftInWaterself.runLastTimer currentTimeelse:self.image self.standLeftImageInWaterself.runLastTimer currentTimedef jumping(self, keys, currentTime, playerBulletList):跳跃# 设置标志self.isJumping Trueself.isStanding Falseself.isDown Falseself.isSquating Falseself.isFiring False# 更新速度self.ySpeed self.gravityif currentTime - self.runLastTimer 115:if self.upImageIndex 3:self.upImageIndex 1else:self.upImageIndex 0# 记录变换图片的时间为下次变换图片做准备self.runLastTimer currentTimeif keys[pygame.K_d]:self.direction Direction.RIGHTelif keys[pygame.K_a]:self.direction Direction.LEFT# 按下W键if keys[pygame.K_w]:self.isUp Trueself.isDown Falseelif keys[pygame.K_s]:self.isUp Falseself.isDown Trueif self.ySpeed 0:self.state State.FALLif not keys[pygame.K_k]:self.state State.FALLif keys[pygame.K_j]:self.fire(currentTime, playerBulletList)def falling(self, keys, currentTime, playerBulletList):# 下落时速度越来越快所以速度需要一直增加self.ySpeed self.gravityif currentTime - self.runLastTimer 115:if self.upImageIndex 3:self.upImageIndex 1else:self.upImageIndex 0self.runLastTimer currentTimeif keys[pygame.K_d]:self.direction Direction.RIGHTself.isWalking Falseelif keys[pygame.K_a]:self.direction Direction.LEFTself.isWalking Falseif keys[pygame.K_j]:self.fire(currentTime, playerBulletList)def fire(self, currentTime, playerBulletList):self.isFiring True# 潜水状态下不能开火if not (self.isInWater and self.isSquating):if len(playerBulletList) PLAYER_BULLET_NUMBER:if currentTime - self.fireLastTimer 150:playerBulletList.append(Bullet(self))self.fireLastTimer currentTimedef damage(self, damage):if not self.isInvincible:self.life - damagereturn Truereturn Falsedef getCenter(self):return self.rect.x self.rect.width / 2, self.rect.y self.rect.height / 2 y0完整的子弹类代码 import pygame from Constants import * from Explode import Explodeclass Bullet(pygame.sprite.Sprite):def __init__(self, person, isEnemy False):pygame.sprite.Sprite.__init__(self)self.images [loadImage(../Image/Bullet/bullet1.png)]self.index 0self.image self.images[self.index]# 速度self.xSpeed 1self.ySpeed 1self.rect pygame.Rect(person.rect)if not isEnemy:if person.isInWater:self.waterPosition(person)else:self.landPosition(person)else:if person.direction Direction.RIGHT:self.rect.x 27 * PLAYER_SCALEself.rect.y 7 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:self.rect.x -1 * PLAYER_SCALEself.rect.y 7 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7# 销毁开关self.isDestroy Falsedef landPosition(self, person):if person.isStanding:if person.direction Direction.RIGHT:if person.isUp:self.rect.x 10 * PLAYER_SCALEself.rect.y -1 * PLAYER_SCALEself.ySpeed -7self.xSpeed 0else:self.rect.x 24 * PLAYER_SCALEself.rect.y 11 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:if person.isUp:self.rect.x 10 * PLAYER_SCALEself.rect.y -1 * PLAYER_SCALEself.ySpeed -7self.xSpeed 0else:self.rect.y 11 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7elif person.isSquating and not person.isWalking:if person.direction Direction.RIGHT:self.rect.x 34 * PLAYER_SCALEself.rect.y 25 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:self.rect.y 25 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7elif person.isWalking:if person.direction Direction.RIGHT:if person.isUp:self.rect.x 20 * PLAYER_SCALEself.rect.y -1 * PLAYER_SCALEself.ySpeed -7self.xSpeed 7elif person.isDown:self.rect.x 21 * PLAYER_SCALEself.rect.y 20 * PLAYER_SCALEself.ySpeed 7self.xSpeed 7else:self.rect.x 24 * PLAYER_SCALEself.rect.y 11 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:if person.isUp:self.rect.x -3 * PLAYER_SCALEself.rect.y -1 * PLAYER_SCALEself.ySpeed -7self.xSpeed -7elif person.isDown:self.rect.x -3 * PLAYER_SCALEself.rect.y 20 * PLAYER_SCALEself.ySpeed 7self.xSpeed -7else:self.rect.y 11 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7elif person.isJumping or person.state State.FALL:if person.direction Direction.RIGHT:self.rect.x 16 * PLAYER_SCALEself.rect.y 8 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:self.rect.x -2 * PLAYER_SCALEself.rect.y 8 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7def waterPosition(self, person):if person.isStanding:if person.direction Direction.RIGHT:if person.isUp:self.rect.x 14 * PLAYER_SCALEself.rect.y 7 * PLAYER_SCALEself.ySpeed -7self.xSpeed 0else:self.rect.x 27 * PLAYER_SCALEself.rect.y 29 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:if person.isUp:self.rect.x 7 * PLAYER_SCALEself.rect.y 3 * PLAYER_SCALEself.ySpeed -7self.xSpeed 0else:self.rect.x -1 * PLAYER_SCALEself.rect.y 29 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7elif person.isWalking:if person.direction Direction.RIGHT:if person.isUp:self.rect.x 23 * PLAYER_SCALEself.rect.y 17 * PLAYER_SCALEself.ySpeed -7self.xSpeed 7else:self.rect.x 27 * PLAYER_SCALEself.rect.y 29 * PLAYER_SCALEself.ySpeed 0self.xSpeed 7else:if person.isUp:self.rect.x -3 * PLAYER_SCALEself.rect.y -1 * PLAYER_SCALEself.ySpeed -7self.xSpeed -7else:self.rect.x -1 * PLAYER_SCALEself.rect.y 29 * PLAYER_SCALEself.ySpeed 0self.xSpeed -7def move(self):self.rect.x self.xSpeedself.rect.y self.ySpeedself.checkBullet()def draw(self, window):window.blit(self.image, self.rect)def checkBullet(self):toDestroy Falseif self.rect.top 0 or self.rect.top 600:toDestroy Trueif self.rect.left 0 or self.rect.right 900:toDestroy Trueif toDestroy:self.isDestroy Truedef collideEnemy(self, enemyList, explodeList):for enemy in enemyList:if pygame.sprite.collide_rect(self, enemy):self.isDestroy Trueenemy.isDestroy TrueexplodeList.append(Explode(enemy))def collidePlayer(self, player, explodeList):if pygame.sprite.collide_rect(self, player):# 蹲下的时候由于图片上半部分是空白所有子弹必须击中下半部分才判断为玩家被击中if player.isDown or player.isSquating:x player.rect.xy player.rect.y player.rect.height / 2 5if (x self.rect.x player.rect.x player.rect.width) and (y self.rect.y player.rect.y player.rect.height):if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Trueelif player.isInWater:x player.rect.xy player.rect.y player.rect.height / 2if (x self.rect.x player.rect.x player.rect.width) and (y self.rect.y player.rect.y player.rect.height):if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Trueelse:if player.damage(1):self.isDestroy TrueexplodeList.append(Explode(player, ExplodeVariety.PLAYER1))return Truereturn False如果代码中有问题请各位小伙伴们提出我会进行修改的
http://www.tj-hxxt.cn/news/134722.html

相关文章:

  • 交流做病理切片的网站来宾北京网站建设
  • 有pc网站 移动网站怎么做wordpress 推荐 配置
  • 企业网站建设源码深圳市光明区住房和建设局网站
  • 学做网站最好的网站用c做网站
  • 甘肃省第九建设集团网站专业建设工作计划
  • 网站推广怎么样做对网站建设的问题
  • 大连网站建设具体流程是什么广州骏域网站建设专家手机电脑版
  • 优质的聊城网站建设郑州网络推广服务
  • 网站制作课程多少钱网站开发前端技术趋势
  • 网站平台由什么搭建wordpress左对齐代码
  • wordpress 停站永久有效的代理ip
  • 做网站新手流程哪个网站做视频有收益
  • 大专网络营销专业好不好关键词诊断优化全部关键词
  • 有什么做衣服的网站吗咸阳网站开发联系方式
  • 英文站网站源码游戏ui设计
  • 如何制作网站后台信息门户网站是什么
  • 网站建设广找金手指排名贰肆微信机器人
  • 杭州市建设工程造价管理协会网站厦门工程信息网
  • 网站404设置西乡塘网站建设
  • 网站备案最多需要多久wordpress 缓存在那
  • 济南建设网站的公司吗建网站一定要备案吗
  • 深圳网站建设亿联时代网站建设规划书中包含内容
  • 武昌网站建设有域名有服务器怎么做网站
  • 做网站就是做点击率传媒公司网站建设
  • 企业网站制作的市场嘉定制作企业网站
  • 网站建设硬件配置无锡抖音代运营公司
  • 大余做网站公司微商城新零售app
  • 深圳企业网站建设设计公司官方网站的推广策划怎么做
  • 网站建设中跳转页面源码网站的推广和优化方案
  • 简洁大气网站模板广东网站设计公司价格