学习 Python 之 Pygame 开发魂斗罗(十三)

news/2024/11/27 8:44:44/

学习 Python 之 Pygame 开发魂斗罗(十三)

    • 继续编写魂斗罗
      • 1. 创建敌人2类
      • 2. 编写敌人2类的draw()函数
      • 3. 编写敌人越界消失函数
      • 4. 编写敌人开火函数
      • 5. 把敌人2加入地图进行测试

继续编写魂斗罗

在上次的博客学习 Python 之 Pygame 开发魂斗罗(十二)中,我们解决了一些问题,这次我们新加入一个敌人,那我们就开始吧

下面是图片的素材

链接:https://pan.baidu.com/s/1X7tESkes_O6nbPxfpHD6hQ?pwd=hdly
提取码:hdly

1. 创建敌人2类

这次新加入一个敌人,首先创建敌人2的类

class Enemy2(pygame.sprite.Sprite):def __init__(self, x, y, direction, currentTime):pygame.sprite.Sprite.__init__(self)self.r = 0.0self.bulletPosition = 0self.rightImage = loadImage('../Image/Enemy/Enemy2/right.png')self.rightUpImage = loadImage('../Image/Enemy/Enemy2/rightUp.png')self.rightDownImage = loadImage('../Image/Enemy/Enemy2/rightDown.png')self.leftImage = loadImage('../Image/Enemy/Enemy2/right.png', True)self.leftUpImage = loadImage('../Image/Enemy/Enemy2/rightUp.png', True)self.leftDownImage = loadImage('../Image/Enemy/Enemy2/rightDown.png', True)self.type = 2if direction == Direction.RIGHT:self.image = self.rightImageelse:self.image = self.leftImageself.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.center = self.rect.x + self.rect.width / 2, self.rect.y + self.rect.height / 2self.isDestroy = Falseself.isFiring = Falseself.life = 1self.lastTime = currentTimeself.n = 0# 计算时间self.t = 0

这里设置了一下加载的图片,还有一些必要的属性

下面是这个敌人的图片

在这里插入图片描述
这个敌人发射子弹的放心是玩家的中心,所以我们要计算出玩家的中心,也要计算出敌人的中心,这样可以计算出玩家与敌人相距的x方向的距离和y方向的距离

所以我们要有一个计算敌人中心的函数

def getCenter(self):return self.rect.x + self.rect.width / 2, self.rect.y + self.rect.height / 2

有了计算中心的函数,现在就可以写draw()函数了

2. 编写敌人2类的draw()函数

这个敌人一共有6种状态,下面是示意图
在这里插入图片描述

这六个姿势就是6个图片

敌人的枪口始终对着我们的中心位置,因为在魂斗罗游戏中,这个敌人发射的子弹是一直跟着玩家的,玩家移动,它就移动枪口的位置,因此就有这6中姿势

下面我们首先计算出敌人和玩家的距离

在这里插入图片描述

我们通过图,可以看出x和y分别都是用人物的中心进行计算而得来的

∠1是玩家与敌人中心连线与水平方向的夹角,这个交的大小决定着敌人的姿势

在这里插入图片描述
这张图片中的蓝色线,是45度的线,所以我们把姿势定下来

当玩家在敌人左边时,计算玩家与敌人的夹角,如果大于45度,敌人就是姿势6,如果小于-45度,敌人状态就是姿势5,其他敌人的姿势就是通过这样的方法计算出来的

下面我们写代码

def draw(self, window: pygame.Surface, player: PlayerOne, currentTime):# 获取玩家中心playerCenter = player.getCenter()# 获取敌人中心center = self.getCenter()# 计算距离y = playerCenter[1] - center[1]x = playerCenter[0] - center[0]# 设置存放夹角的变量r = 0# 当 x = 0时,此时玩家在敌人的正上方,我们不做任何操作if x != 0:# 如果玩家在敌人的正上方,计算角度r = math.atan(y / x) * 180 / math.pi# 设置变量,用来记录敌人的姿势,敌人的姿势就是发射子弹时的样子self.bulletPosition = 1# 根据距离的正负关系判断玩家在敌人的左边还是右边if x >= 0:if -45 < r < 45:self.bulletPosition = 2self.image = self.rightImageelif r >= 45:self.bulletPosition = 3self.image = self.rightDownImageelif r <= -45:self.bulletPosition = 1self.image = self.rightUpImageelse:if -45 < r < 45:self.bulletPosition = 5self.image = self.leftImageelif r <= -45:self.bulletPosition = 4self.image = self.leftDownImageelif r >= 45:self.bulletPosition = 6self.image = self.leftUpImageself.r = rwindow.blit(self.image, self.rect)

代码中我写了注释,这段代码的意思上面也说明明白了,大概就是计算出玩家和敌人的x方向距离和y方向距离,然后计算夹角,根据夹角的度数设置敌人的姿势

现在关键的一步来了,发射子弹的位置有了,下面就是要思考:如何让敌人对着玩家中心发射?

我们来想一下,在高中我们学过 路程 = 时间×速度,现在我们知道了路程,就是敌人和玩家中心的距离差,要计算速度,那么需要知道时间,那么如何计算时间呢?有了时间,我们通过公式 速度 = 路程 / 时间 计算出速度了

我们可以把调用draw()函数的时间间隔记录下来,把这个的平均值设置为时间

于是我们在构造函数中,就有了这两个变量

self.n = 0
# 时间
self.t = 0

n记录总的间隔数,t记录当前间隔的平均值

下面是计算思路:把每次调用该函数的时间间隔记录下来,根据调用的次数,计算出平均调用该函数的时间间隔,这个时间间隔就作为子弹的发射速度

self.n += 1
# 计算速度
total = self.t * self.n
total = total + abs(currentTime - self.lastTime)
self.lastTime = currentTime
self.t = total * 1.0 / (self.n + 1)

首先我们把上次调用该函数的时间记录到lastTime中,这次调用函数时的时间记录在currentTime 中,通过函数参数把currentTime值传进来

total = self.t * self.n

这句代码来计算总的时间间隔,因为最后我们要求两次调用该函数间隔的平均值

其次,我们使用下面的代码,把总的时间间隔和求出来

total = total + abs(currentTime - self.lastTime)

之后 self.lastTime = currentTime 记录当前的时间,准备进行下一次计算

最后,计算间隔的平均值

self.t = total * 1.0 / (self.n + 1)

举一个例子:

第一次计算出调用该函数的时间间隔是7ms,此时n = 1,t = 7,子弹的速度就设置为7

第二次计算出调用该函数的时间间隔是8ms,此时我们要计算8和7的平均值,此时 n = 2, t = (1 * 7 + 8) / 2= 7.5,所以此时的子弹速度为7.5

第二次计算出调用该函数的时间间隔是8ms,此时我们还是要计算平均值,于是有t = (7.5 * 2 + 8)/ 3,这个结果还是记录为子弹的速度

以此类推,每次都会更新这个间隔时间,这个间隔时间就是子弹的速度

所以,我们最后就计算出来了子弹的速度了

把代码写到draw()函数中,就完成了draw()函数

def draw(self, window: pygame.Surface, player: PlayerOne, currentTime):self.n += 1# 计算时间total = self.t * self.ntotal = total + abs(currentTime - self.lastTime)self.lastTime = currentTimeself.t = total * 1.0 / (self.n + 1)# 获取玩家中心playerCenter = player.getCenter()# 获取敌人中心center = self.getCenter()# 计算距离y = playerCenter[1] - center[1]x = playerCenter[0] - center[0]# 设置存放夹角的变量r = 0# 当 x = 0时,此时玩家在敌人的正上方,我们不做任何操作if x != 0:# 如果玩家在敌人的正上方,计算角度r = math.atan(y / x) * 180 / math.pi# 设置变量,用来记录敌人的姿势,敌人的姿势就是发射子弹时的样子self.bulletPosition = 1# 根据距离的正负关系判断玩家在敌人的左边还是右边if x >= 0:if -45 < r < 45:self.bulletPosition = 2self.image = self.rightImageelif r >= 45:self.bulletPosition = 3self.image = self.rightDownImageelif r <= -45:self.bulletPosition = 1self.image = self.rightUpImageelse:if -45 < r < 45:self.bulletPosition = 5self.image = self.leftImageelif r <= -45:self.bulletPosition = 4self.image = self.leftDownImageelif r >= 45:self.bulletPosition = 6self.image = self.leftUpImageself.r = rwindow.blit(self.image, self.rect)

3. 编写敌人越界消失函数

当敌人创建出来后,我们没有消灭,他就会随着玩家向右移动消失在玩家的窗口中,为了方式程序中存在大量的无效的敌人数据,我们要检查程序,让那些离开窗口的敌人自动销毁

编写检查函数

def checkPosition(self, x, y):if abs(self.rect.x - x) > 2000:self.isDestroy = Trueelif abs(self.rect.y - y) > 600:self.isDestroy = True

当然,敌人1类也有该函数

4. 编写敌人开火函数

由于敌人2发射的子弹要对着玩家的方向发射,这里我们要修改子弹类

在这里插入图片描述
把子弹的初始速度变为0

修改构造函数参数

在这里插入图片描述
enemyType 是敌人的类型,类型不一样,发射位置不一样

parameter是一些额外的参数,敌人2发射子弹时,这里面就是传入一些必要的信息,用来计算

接下来将原来的逻辑进行修改

在这里插入图片描述
大部分代码没有改变,加了一个if-else语句

下面我们来写敌人2的子弹逻辑代码

elif enemyType == 2:self.index = 0bulletPosition = parameter[0]player = parameter[1]playerCenter = player.getCenter()if player.isDown or player.isSquating:# 下蹲、蹲下、在水中时,让人物中心下移动,下移动代表y坐标的值相加playerCenter = (playerCenter[0], playerCenter[1] + 8)elif player.isInWater:playerCenter = (playerCenter[0], playerCenter[1] + 15)t = parameter[2]# t *= 15r = parameter[3]if bulletPosition == 1:self.rect.x += 19 * PLAYER_SCALEself.rect.y += -1 * PLAYER_SCALEself.ySpeed = - abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 2:self.rect.x += 25 * PLAYER_SCALEself.rect.y += 10 * PLAYER_SCALEs = -1if r > 0:s = 1self.ySpeed = s * abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 3:self.rect.x += 25 * PLAYER_SCALEself.rect.y += 25 * PLAYER_SCALEself.ySpeed = abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 4:self.rect.x += -1 * PLAYER_SCALEself.rect.y += 25 * PLAYER_SCALEself.ySpeed = abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 5:self.rect.x += -1 * PLAYER_SCALEself.rect.y += 10 * PLAYER_SCALEs = 1if r > 0:s = -1self.ySpeed = s * abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 6:self.rect.x += -1 * PLAYER_SCALEself.rect.y += -1 * PLAYER_SCALEself.ySpeed = - abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / tself.xSpeed /= 5self.ySpeed /= 5
self.image = self.images[self.index]

完整的子弹类构造函数

def __init__(self, person, enemyType = 0, parameter = None):pygame.sprite.Sprite.__init__(self)self.images = [loadImage('../Image/Bullet/bullet1.png')]self.index = 0# 速度self.xSpeed = 1self.ySpeed = 1self.rect = pygame.Rect(person.rect)# 类型0表示不是敌人if enemyType == 0:if person.isInWater:self.waterPosition(person)else:self.landPosition(person)# 敌人1elif enemyType == 1:self.index = 0if 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# 敌人2elif enemyType == 2:self.index = 0# 从额外参数中获取敌人的姿势,即子弹的发射位置bulletPosition = parameter[0]# 获取玩家对象player = parameter[1]# 获取玩家中心playerCenter = player.getCenter()# 让人物中心下移if player.isDown or player.isSquating:# 下蹲、蹲下、在水中时,让人物中心下移动,下移动代表y坐标的值相加playerCenter = (playerCenter[0], playerCenter[1] + 8)elif player.isInWater:playerCenter = (playerCenter[0], playerCenter[1] + 15)# 获取子弹移动的时间t = parameter[2]# t *= 15# 获取敌人与玩家连线与水平方向的夹角r = parameter[3]# 根据子弹的发射位置(敌人的姿势)计算敌人的发射子弹的位置和子弹的速度if bulletPosition == 1:self.rect.x += 19 * PLAYER_SCALEself.rect.y += -1 * PLAYER_SCALE# 计算公式,|x0 - x1| / t = vself.ySpeed = - abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 2:self.rect.x += 25 * PLAYER_SCALEself.rect.y += 10 * PLAYER_SCALE# s 表示方向这里可以直接根据r的大小,计算出子弹的速度是减少还是增加# 减少表示向负方向移动s = -1if r > 0:s = 1self.ySpeed = s * abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 3:self.rect.x += 25 * PLAYER_SCALEself.rect.y += 25 * PLAYER_SCALEself.ySpeed = abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 4:self.rect.x += -1 * PLAYER_SCALEself.rect.y += 25 * PLAYER_SCALEself.ySpeed = abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 5:self.rect.x += -1 * PLAYER_SCALEself.rect.y += 10 * PLAYER_SCALEs = 1if r > 0:s = -1self.ySpeed = s * abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 6:self.rect.x += -1 * PLAYER_SCALEself.rect.y += -1 * PLAYER_SCALEself.ySpeed = - abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / tself.xSpeed /= 5self.ySpeed /= 5self.image = self.images[self.index]# 销毁开关self.isDestroy = False

之后我们来写玩家2类的开火函数了

def fire(self, enemyBulletList, player):i = random.randint(0, 30)if i == 5:self.isFiring = TrueenemyBulletList.append(Bullet(self, 2, (self.bulletPosition, player, self.t, self.r)))

设置开火的频率,开火就创建一个子弹对象,把相应的额外参数传入

好,至此就完成了敌人2类

5. 把敌人2加入地图进行测试

来到主类,编写全局函数,用来创建敌人2

在这里插入图片描述

def generateEnemy2(x, y):enemy = Enemy2(x, y, MainGame.player1, pygame.time.get_ticks())MainGame.enemyList.append(enemy)MainGame.allSprites.add(enemy)MainGame.enemyGroup.add(enemy)

之后在generateEnemy()函数中调用

在这里插入图片描述
这个代码就是在指定的位置上创建一个敌人2

if -2005 < self.backRect.x < -2000:if self.enemyBoolList[2]:self.enemyBoolList[2] = FalsegenerateEnemy2(MainGame.player1.rect.x + 540, 465)

接下来运行一下游戏,看看效果

在这里插入图片描述
出现了报错信息,应该是子弹类的构造函数进行了修改,所有创建子弹的函数都要修改,我们下面一一进行修改

首先进入enemyUpdate()函数,由于我们加入了敌人2,所以调用draw()函数的时候要进行判断了,因为敌人1和敌人2的draw()函数参数不一样

在这里插入图片描述
将代码进行修改

def enemyUpdate(enemyList, enemyBulletList):# 遍历整个敌人列表for enemy in enemyList:if enemy.type == 1: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)elif enemy.type == 2: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(MainGame.window, MainGame.player1, pygame.time.get_ticks())enemy.fire(enemyBulletList, MainGame.player1)

变化的地方就是敌人2的draw()函数和fire()函数多了一个参数

之后给敌人1类加入成员变量type

self.type = 1

在这里插入图片描述
我们再运行游戏,看看问题

在这里插入图片描述
可以看到敌人发射子弹的方向一直向着玩家

但是还有个问题,就是玩家屏幕向右移动的时候,子弹会停下来

这应该是窗口移动时,没让子弹也一起移动

我们修改一下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.cameraAdaptionfor bullet in MainGame.enemyBulletList:bullet.rect.x -= self.cameraAdaptionfor explode in MainGame.explodeList:explode.rect.x -= self.cameraAdaption

在这里插入图片描述
好,接下来我们再运行一下游戏,看看效果

在这里插入图片描述
可以看到,子弹也会随着窗口移动而移动啦

我们现在就完了敌人2了,下面就是加入其他敌人和BOSS了

完整的主类代码

import copy
import sys
import pygame
from Constants import *
from PlayerOne import PlayerOne
from Collider import Collider
from Enemy1 import Enemy1
from Explode import Explode
from Enemy2 import Enemy2def 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.type == 1: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)elif enemy.type == 2: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(MainGame.window, MainGame.player1, pygame.time.get_ticks())enemy.fire(enemyBulletList, MainGame.player1)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)def generateEnemy2(x, y):enemy = Enemy2(x, y, MainGame.player1, pygame.time.get_ticks())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.cameraAdaptionfor bullet in MainGame.enemyBulletList:bullet.rect.x -= self.cameraAdaptionfor explode in MainGame.explodeList:explode.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())if -2005 < self.backRect.x < -2000:if self.enemyBoolList[2]:self.enemyBoolList[2] = FalsegenerateEnemy2(MainGame.player1.rect.x + 540, 465)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()

完整敌人1类代码

import randomimport pygame
from Constants import *
from Bullet import Bulletclass Enemy1(pygame.sprite.Sprite):def __init__(self, x, y, direction, currentTime):pygame.sprite.Sprite.__init__(self)self.lastTime = currentTimeself.fireTime = currentTimeself.rightImages = [loadImage('../Image/Enemy/Enemy1/1.png'),loadImage('../Image/Enemy/Enemy1/2.png'),loadImage('../Image/Enemy/Enemy1/3.png')]self.leftImages = [loadImage('../Image/Enemy/Enemy1/1.png', True),loadImage('../Image/Enemy/Enemy1/2.png', True),loadImage('../Image/Enemy/Enemy1/3.png', True)]self.rightFireImage = loadImage('../Image/Enemy/Enemy1/fire.png')self.leftFireImage = loadImage('../Image/Enemy/Enemy1/fire.png', True)self.fallImage = loadImage('../Image/Enemy/Enemy1/fall.png', True)self.rightFireImage = loadImage('../Image/Enemy/Enemy1/fire.png')self.leftFireImage = loadImage('../Image/Enemy/Enemy1/fire.png', True)self.rightFallImage = loadImage('../Image/Enemy/Enemy1/fall.png')self.leftFallImage = loadImage('../Image/Enemy/Enemy1/fall.png', True)self.index = 0self.direction = directionif self.direction == Direction.RIGHT:self.image = self.rightImages[self.index]else:self.image = self.leftImages[self.index]self.rect = self.image.get_rect()self.isFalling = Falseself.rect.x = xself.rect.y = yself.speed = 3self.isDestroy = Falseself.isFiring = Falseself.life = 1self.type = 1def move(self, currentTime):# 首先判断敌人是否开火,如果是开火状态,就不能移动if not self.isFiring:# 没有开火,就根据方向移动,这里我设置敌人只能向一个方向移动,不能转身if self.direction == Direction.RIGHT:self.rect.left += self.speedelse:self.rect.left -= self.speedelse:# 如果此时是开火状态,判断一下上次开火的时间和这次的时间是否相差1000# 这个的作用在于让敌人开火的时候站在那里不动,因为敌人移动时是不能开火的if currentTime - self.fireTime > 1000:# 如果两次开火间隔相差很大,那么就可以让敌人再次开火self.isFiring = Falseself.fireTime = currentTimedef draw(self, currentTime):if self.isFiring:if self.direction == Direction.RIGHT:self.image = self.rightFireImageelse:self.image = self.leftFireImageelse:if currentTime - self.lastTime > 115:if self.index < 2:self.index += 1else:self.index = 0self.lastTime = currentTimeif self.direction == Direction.RIGHT:self.image = self.rightImages[self.index]else:self.image = self.leftImages[self.index]def fire(self, enemyBulletList):if not self.isFalling:i = random.randint(0, 50)if i == 5:if not self.isFiring:self.isFiring = TrueenemyBulletList.append(Bullet(self, True))def checkPosition(self, x, y):if abs(self.rect.x - x) > 1000:self.isDestroy = Trueelif abs(self.rect.y - y) > 600:self.isDestroy = True

完整的敌人2类代码

import math
import randomimport pygame
from Constants import *
from Bullet import Bullet
from PlayerOne import PlayerOneclass Enemy2(pygame.sprite.Sprite):def __init__(self, x, y, direction, currentTime):pygame.sprite.Sprite.__init__(self)self.r = 0.0self.bulletPosition = 0self.rightImage = loadImage('../Image/Enemy/Enemy2/right.png')self.rightUpImage = loadImage('../Image/Enemy/Enemy2/rightUp.png')self.rightDownImage = loadImage('../Image/Enemy/Enemy2/rightDown.png')self.leftImage = loadImage('../Image/Enemy/Enemy2/right.png', True)self.leftUpImage = loadImage('../Image/Enemy/Enemy2/rightUp.png', True)self.leftDownImage = loadImage('../Image/Enemy/Enemy2/rightDown.png', True)self.type = 2if direction == Direction.RIGHT:self.image = self.rightImageelse:self.image = self.leftImageself.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.center = self.rect.x + self.rect.width / 2, self.rect.y + self.rect.height / 2self.isDestroy = Falseself.isFiring = Falseself.life = 1self.lastTime = currentTimeself.n = 0# 计算时间self.t = 0def getCenter(self):return self.rect.x + self.rect.width / 2, self.rect.y + self.rect.height / 2def draw(self, window: pygame.Surface, player: PlayerOne, currentTime):self.n += 1# 计算时间total = self.t * self.ntotal = total + abs(currentTime - self.lastTime)self.lastTime = currentTimeself.t = total * 1.0 / (self.n + 1)# 获取玩家中心playerCenter = player.getCenter()# 获取敌人中心center = self.getCenter()# 计算距离y = playerCenter[1] - center[1]x = playerCenter[0] - center[0]# 设置存放夹角的变量r = 0# 当 x = 0时,此时玩家在敌人的正上方,我们不做任何操作if x != 0:# 如果玩家在敌人的正上方,计算角度r = math.atan(y / x) * 180 / math.pi# 设置变量,用来记录敌人的姿势,敌人的姿势就是发射子弹时的样子self.bulletPosition = 1# 根据距离的正负关系判断玩家在敌人的左边还是右边if x >= 0:if -45 < r < 45:self.bulletPosition = 2self.image = self.rightImageelif r >= 45:self.bulletPosition = 3self.image = self.rightDownImageelif r <= -45:self.bulletPosition = 1self.image = self.rightUpImageelse:if -45 < r < 45:self.bulletPosition = 5self.image = self.leftImageelif r <= -45:self.bulletPosition = 4self.image = self.leftDownImageelif r >= 45:self.bulletPosition = 6self.image = self.leftUpImageself.r = rwindow.blit(self.image, self.rect)def fire(self, enemyBulletList, player):i = random.randint(0, 30)if i == 5:self.isFiring = TrueenemyBulletList.append(Bullet(self, 2, (self.bulletPosition, player, self.t, self.r)))def checkPosition(self, x, y):if abs(self.rect.x - x) > 2000:self.isDestroy = Trueelif abs(self.rect.y - y) > 600:self.isDestroy = True

完整的子弹类代码

import pygame
from Constants import *
from Explode import Explodeclass Bullet(pygame.sprite.Sprite):def __init__(self, person, enemyType = 0, parameter = None):pygame.sprite.Sprite.__init__(self)self.images = [loadImage('../Image/Bullet/bullet1.png')]self.index = 0# 速度self.xSpeed = 1self.ySpeed = 1self.rect = pygame.Rect(person.rect)# 类型0表示不是敌人if enemyType == 0:if person.isInWater:self.waterPosition(person)else:self.landPosition(person)# 敌人1elif enemyType == 1:self.index = 0if 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# 敌人2elif enemyType == 2:self.index = 0# 从额外参数中获取敌人的姿势,即子弹的发射位置bulletPosition = parameter[0]# 获取玩家对象player = parameter[1]# 获取玩家中心playerCenter = player.getCenter()# 让人物中心下移if player.isDown or player.isSquating:# 下蹲、蹲下、在水中时,让人物中心下移动,下移动代表y坐标的值相加playerCenter = (playerCenter[0], playerCenter[1] + 8)elif player.isInWater:playerCenter = (playerCenter[0], playerCenter[1] + 15)# 获取子弹移动的时间t = parameter[2]# t *= 15# 获取敌人与玩家连线与水平方向的夹角r = parameter[3]# 根据子弹的发射位置(敌人的姿势)计算敌人的发射子弹的位置和子弹的速度if bulletPosition == 1:self.rect.x += 19 * PLAYER_SCALEself.rect.y += -1 * PLAYER_SCALE# 计算公式,|x0 - x1| / t = vself.ySpeed = - abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 2:self.rect.x += 25 * PLAYER_SCALEself.rect.y += 10 * PLAYER_SCALE# s 表示方向这里可以直接根据r的大小,计算出子弹的速度是减少还是增加# 减少表示向负方向移动s = -1if r > 0:s = 1self.ySpeed = s * abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 3:self.rect.x += 25 * PLAYER_SCALEself.rect.y += 25 * PLAYER_SCALEself.ySpeed = abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 4:self.rect.x += -1 * PLAYER_SCALEself.rect.y += 25 * PLAYER_SCALEself.ySpeed = abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 5:self.rect.x += -1 * PLAYER_SCALEself.rect.y += 10 * PLAYER_SCALEs = 1if r > 0:s = -1self.ySpeed = s * abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / telif bulletPosition == 6:self.rect.x += -1 * PLAYER_SCALEself.rect.y += -1 * PLAYER_SCALEself.ySpeed = - abs(self.rect.y - playerCenter[1]) * 1.0 / tself.xSpeed = - abs(self.rect.x - playerCenter[0]) * 1.0 / tself.xSpeed /= 5self.ySpeed /= 5self.image = self.images[self.index]# 销毁开关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.ppmy.cn/news/35011.html

相关文章

乐观锁和悲观锁 面试题

Mysql的乐观锁和悲观锁 实现方式加锁时机常见的调用方式优势不足适用场景乐观锁开发自定义更新数据的时候sql语句中进行version的判断高并发容易出现不一致的问题高并发读&#xff0c;少写悲观锁Mysql内置查询数据的开始select * for update保证一致性低并发互联网高并发场景极…

基于Golang实现多人在线游戏的AOI算法

1、AOI基本介绍 游戏的AOI(Area of Interest)算法应该算作游戏的基础核心了&#xff0c;许多逻辑都是因为AOI进出事件驱动的&#xff0c;许多网络同步数据也是因为AOI进出事件产生的。因此&#xff0c;良好的AOI算法和基于AOI算法的优化&#xff0c;是提高游戏性能的关键。 为…

ubuntu下使用GCC开发单片机的过程

以下是一个简单的单片机C程序示例,实现的功能是控制LED灯的闪烁: #include <reg52.h> // 导入单片机的寄存器定义void main() {while(1) { // 无限循环P1 = 0x00; // P1口输出低电平delay(1000); // 延时1秒P1 = 0xff; // P1口输出高电平delay(1000); // 延时1秒…

[刷题]背包问题

递归问题特性 ①问题有最优子结构&#xff1a;问题存在最优解&#xff0c;且与其子问题最优解重合 ②无后效&#xff1a;前后状态值只和值本身有关&#xff0c;和问题无关。 解决思路&#xff1a; ①将原问题分解为子问题 ②确定状态 ③确定初始状态值 ④确定状态转移方程&…

b01lers(php.galf)

目录 前文 正文 前文 <?phpclass A{public $codeNULL;public $argsNULL;public function __construct($code,$argsNULL){$this->code$code;$this->args$args;print_r("2333") ;} public function __invoke($code,$args){echo $code;print_r("执行inv…

[入门必看]数据结构2.3:线性表的链式表示

[入门必看]数据结构2.3&#xff1a;线性表的链式表示第二章 线性表2.3 线性表的链式表示知识总览2.3.1 单链表的定义2.3.2_1 单链表的插入删除2.3.2_2 单链表的查找2.3.2_3 单链表的建立2.3.3 双链表2.3.4 循环链表2.3.5 静态链表2.3.6 顺序表和链表的比较2.3.1 单链表的定义单…

【3.24】Mybatis常见面试题

Mybatis常见面试题 #{}和&#xffe5;{}的区别是什么&#xff1f; 【#】&#xff1a;底层执行SQL使用PreparedStatement对象&#xff0c;预编译SQL&#xff0c;相对安全。入参使用占位符的方式。 【$】&#xff1a;底层执行SQL使用Statement对象&#xff0c;入参使用SQL拼接的…

upload-lab通关

1.pass-1 前端js检查&#xff0c;修改js或修改后缀绕过查看源代码&#xff0c;可以发现是客户端的进行检查:两种方法&#xff1a;(1).禁用js或修改js代码(2).抓包修改后缀这里采用抓包修改后缀先改成可以上传的后缀名&#xff0c;在抓包修改后缀名为php1.php代码<?php phpi…