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

继续编写魂斗罗

在上次的博客学习 Python 之 Pygame 开发魂斗罗(六)中,我们实现玩家跳上和跳下,这次我们完成让玩家可以跳到水里

在水中,玩家的姿势会变得不一样,下面我们来实现一下

下面是图片的素材

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

1. 载入水中图片并添加在水中的标志

在玩家类的__init__()函数加载图片

# 加载玩家在水中的图片
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.isInWater = False

在这里插入图片描述

2. 修改玩家类函数

接下来我们把玩家类的函数进行调整

把原来update()函数中圈出的代码,写成一个函数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.downRightImage
        else:
            # 加载向左蹲下的图片
            self.image = self.downLeftImage

    # 角色站着
    if self.isStanding:
        if self.direction == Direction.RIGHT:
            if self.isUp:
                # 加载向右朝上的图片
                self.image = self.upRightImage
            elif self.isDown:
                # 加载向右蹲下的图片
                self.image = self.downRightImage
            else:
                # 加载向右站着的图片
                self.image = self.standRightImage
        else:
            # 向左也是同样的效果
            if self.isUp:
                self.image = self.upLeftImage
            elif self.isDown:
                self.image = self.downLeftImage
            else:
                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]

这个函数的作用是,如果玩家在陆地上,就按照这个函数来显示玩家的图片

我们再加一个waterUpdate()函数,作用是,如果玩家在水中,就按照这个函数来显示玩家的图片

def waterUpdate(self):
    pass

里面的代码稍后写

之后把这两个函数在update()函数中调用一下

def 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 fire(self, currentTime, playerBulletList):
    self.isFiring = True
    if len(playerBulletList) < PLAYER_BULLET_NUMBER:
        if currentTime - self.fireLastTimer > 150:
            playerBulletList.append(Bullet(self))
            self.fireLastTimer = currentTime

之后在下面的这四个函数中调用一下

在这里插入图片描述
把原来的代码替换成函数

在这里插入图片描述
在这里插入图片描述
其他的函数也是一样

好,这样的话,这一步就完成了

下面我们把walking()函数修改一下
在这里插入图片描述

把圈出的代码提出来写成一个函数

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.standRightImage
        elif self.direction == Direction.LEFT and self.isUp:
            self.image = self.upLeftImage
        elif self.direction == Direction.LEFT and not self.isUp:
            self.image = self.standLeftImage
        # 记下当前时间
        self.runLastTimer = currentTime
    else:
        # 如果是走动的图片,先判断方向
        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 += 1
                    else:
                        self.imageIndex = 0
                    self.runLastTimer = currentTime
        else:
            self.xSpeed = -PLAYER_X_SPEED
            if self.isUp or self.isDown:
                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 += 1
                    else:
                        self.imageIndex = 0
                    self.runLastTimer = currentTime

把刚才圈出来的部分进行修改

在这里插入图片描述
再创建一个walkingInWater函数

def walkingInWater(self, currentTime):
    pass

完整的代码

import pygame
from Constants import *
from Bullet import Bullet


class PlayerOne(pygame.sprite.Sprite):

    def __init__(self, currentTime):
        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 = currentTime
        self.fireLastTimer = currentTime

        # 选择当前要显示的图片
        self.image = self.standRightImage
        # 获取图片的rect
        self.rect = self.image.get_rect()
        # 设置角色的状态
        self.state = State.FALL
        # 角色的方向
        self.direction = Direction.RIGHT
        # 速度
        self.xSpeed = PLAYER_X_SPEED
        self.ySpeed = 0
        self.jumpSpeed = -11
        # 人物当前的状态标志
        self.isStanding = False
        self.isWalking = False
        self.isJumping = True
        self.isSquating = False
        self.isFiring = False
        self.isInWater = False
        # 重力加速度
        self.gravity = 0.8

        # 玩家上下方向
        self.isUp = False
        self.isDown = False

    def 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.downRightImage
            else:
                # 加载向左蹲下的图片
                self.image = self.downLeftImage

        # 角色站着
        if self.isStanding:
            if self.direction == Direction.RIGHT:
                if self.isUp:
                    # 加载向右朝上的图片
                    self.image = self.upRightImage
                elif self.isDown:
                    # 加载向右蹲下的图片
                    self.image = self.downRightImage
                else:
                    # 加载向右站着的图片
                    self.image = self.standRightImage
            else:
                # 向左也是同样的效果
                if self.isUp:
                    self.image = self.upLeftImage
                elif self.isDown:
                    self.image = self.downLeftImage
                else:
                    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):
        pass

    def standing(self, keys, currentTime, playerBulletList):
        """角色站立"""

        # 设置角色状态
        self.isStanding = True
        self.isWalking = False
        self.isJumping = False
        self.isSquating = False
        self.isUp = False
        self.isDown = False
        self.isFiring = False

        # 设置速度
        self.ySpeed = 0
        self.xSpeed = 0

        # 按下A键
        if keys[pygame.K_a]:
            # A按下,角色方向向左
            self.direction = Direction.LEFT
            # 改变角色的状态,角色进入移动状态
            self.state = State.WALK
            # 设置站立状态为False,移动状态为True
            self.isStanding = False
            self.isWalking = True
            # 向左移动,速度为负数,这样玩家的x坐标是减小的
            self.xSpeed = -PLAYER_X_SPEED
        # 按下D键
        elif keys[pygame.K_d]:
            # D按下,角色方向向右
            self.direction = Direction.RIGHT
            # 改变角色的状态,角色进入移动状态
            self.state = State.WALK
            # 设置站立状态为False,移动状态为True
            self.isStanding = False
            self.isWalking = True
            # 向右移动,速度为正数
            self.xSpeed = PLAYER_X_SPEED
        # 按下k键
        elif keys[pygame.K_k]:
            # K按下,角色进入跳跃状态,但是不会改变方向
            self.state = State.JUMP
            # 设置站立状态为False,跳跃状态为True
            # 不改变移动状态,因为移动的时候也可以跳跃
            self.isStanding = False
            self.isJumping = True
            # 设置速度,速度为负数,因为角色跳起后,要下落
            self.isUp = True
            self.ySpeed = self.jumpSpeed
        # 没有按下按键
        else:
            # 没有按下按键,角色依然是站立状态
            self.state = State.STAND
            self.isStanding = True

        # 按下w键
        if keys[pygame.K_w]:
            # W按下,角色向上,改变方向状态
            self.isUp = True
            self.isStanding = True
            self.isDown = False
            self.isSquating = False
        # 按下s键
        elif keys[pygame.K_s]:
            # S按下,角色蹲下,改变方向状态,并且蹲下状态设置为True
            self.isUp = False
            self.isStanding = False
            self.isDown = True
            self.isSquating = True

        if keys[pygame.K_j]:
            self.fire(currentTime, playerBulletList)

    def walking(self, keys, currentTime, playerBulletList):
        """角色行走,每10帧变换一次图片"""
        self.isStanding = False
        self.isWalking = True
        self.isJumping = False
        self.isSquating = False
        self.isFiring = False
        self.ySpeed = 0
        self.xSpeed = PLAYER_X_SPEED

        if self.isInWater:
            self.walkingInWater(currentTime)
        else:
            self.walkingInLand(currentTime)

        # 按下D键
        if keys[pygame.K_d]:
            self.direction = Direction.RIGHT
            self.xSpeed = PLAYER_X_SPEED
        # 按下A键
        elif keys[pygame.K_a]:
            self.direction = Direction.LEFT
            self.xSpeed = -PLAYER_X_SPEED
         # 按下S键
        elif keys[pygame.K_s]:
            self.isStanding = False
            self.isDown = True

        # 按下W键
        if keys[pygame.K_w]:
            self.isUp = True
            self.isDown = False
        # 没有按键按下
        else:
            self.state = State.STAND

        # 移动时按下K键
        if keys[pygame.K_k]:
            # 角色状态变为跳跃
            self.state = State.JUMP
            self.ySpeed = self.jumpSpeed
            self.isJumping = True
            self.isStanding = False
            self.isUp = True

        if 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.standRightImage
            elif self.direction == Direction.LEFT and self.isUp:
                self.image = self.upLeftImage
            elif self.direction == Direction.LEFT and not self.isUp:
                self.image = self.standLeftImage
            # 记下当前时间
            self.runLastTimer = currentTime
        else:
            # 如果是走动的图片,先判断方向
            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 += 1
                        else:
                            self.imageIndex = 0
                        self.runLastTimer = currentTime
            else:
                self.xSpeed = -PLAYER_X_SPEED
                if self.isUp or self.isDown:
                    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 += 1
                        else:
                            self.imageIndex = 0
                        self.runLastTimer = currentTime

    def walkingInWater(self, currentTime):
        pass

    def jumping(self, keys, currentTime, playerBulletList):
        """跳跃"""
        # 设置标志
        self.isJumping = True
        self.isStanding = False
        self.isDown = False
        self.isSquating = False
        self.isFiring = False
        # 更新速度
        self.ySpeed += self.gravity
        if currentTime - self.runLastTimer > 115:
            if self.upImageIndex < 3:
                self.upImageIndex += 1
            else:
                self.upImageIndex = 0
            # 记录变换图片的时间,为下次变换图片做准备
            self.runLastTimer = currentTime

        if keys[pygame.K_d]:
            self.direction = Direction.RIGHT

        elif keys[pygame.K_a]:
            self.direction = Direction.LEFT

        # 按下W键
        if keys[pygame.K_w]:
            self.isUp = True
            self.isDown = False
        elif keys[pygame.K_s]:
            self.isUp = False
            self.isDown = True

        if self.ySpeed >= 0:
            self.state = State.FALL

        if not keys[pygame.K_k]:
            self.state = State.FALL

        if keys[pygame.K_j]:
            self.fire(currentTime, playerBulletList)

    def falling(self, keys, currentTime, playerBulletList):
        # 下落时速度越来越快,所以速度需要一直增加
        self.ySpeed += self.gravity
        if currentTime - self.runLastTimer > 115:
            if self.upImageIndex < 3:
                self.upImageIndex += 1
            else:
                self.upImageIndex = 0
            self.runLastTimer = currentTime

        if keys[pygame.K_d]:
            self.direction = Direction.RIGHT
            self.isWalking = False

        elif keys[pygame.K_a]:
            self.direction = Direction.LEFT
            self.isWalking = False

        if keys[pygame.K_j]:
            self.fire(currentTime, playerBulletList)

    def fire(self, currentTime, playerBulletList):
        self.isFiring = True
        if len(playerBulletList) < PLAYER_BULLET_NUMBER:
            if currentTime - self.fireLastTimer > 150:
                playerBulletList.append(Bullet(self))
                self.fireLastTimer = currentTime

3. 增加河的碰撞体

我们修改主类代码,增加河的碰撞体

首先增加一个列表,用来存放河的碰撞体

在这里插入图片描述
其次,创建初始化河碰撞体函数,并把河碰撞体列表放入总的碰撞体列表

def initRiver(self):
    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))
    MainGame.riverGroup.add(river1)
    MainGame.riverGroup.add(river2)
    MainGame.riverGroup.add(river3)
    MainGame.colliderGroup.add(MainGame.riverGroup)

这里Collider()构造函数中要多一个参数,表示颜色,我们等下修改一下

之后,在update()函数中调用

def update(self, window, player1BulletList):
    # 加载背景
    window.blit(self.background, self.backRect)
    # 更新物体
    currentTime = pygame.time.get_ticks()
    MainGame.allSprites.update(self.keys, currentTime, player1BulletList)
    self.updatePlayerPosition()
    drawPlayerOneBullet(player1BulletList)
    # 摄像机移动
    self.camera()
    # 显示物体
    MainGame.allSprites.draw(window)
    for collider in MainGame.landGroup:
        r = collider.draw(window, self.player1.rect.y)
        # 如果没有画出来,表示玩家高度低于直线,所有把直线从组中删除
        if not r:
            # 删除前先检查一下是不是在组中
            if collider in MainGame.colliderGroup:
                # 删除并加入栈
                MainGame.colliderStack.insert(0, collider)
                MainGame.colliderGroup.remove(collider)
        else:
            # 如果画出来了,判断一下玩家距离是否高于线的距离
            if collider.rect.y > self.player1.rect.bottom:
                # 如果是的话,且冲突栈不为空,那么从栈中取出一个元素放入冲突组,最前面的元素一定是先如队列的
                if len(MainGame.colliderStack) > 0:
                    f = MainGame.colliderStack.pop()
                    MainGame.colliderGroup.add(f)
    MainGame.riverGroup.draw(window)

然后再修改一下Collider类的__init__()函数

def __init__(self, x, y, width, height, color = (255, 0, 0)):
    pygame.sprite.Sprite.__init__(self)

    self.image = pygame.Surface((width, height)).convert()
    self.image.fill(color)
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y

使创建的冲突默认为红色显示

最后在主类的__init__()函数中,把initRiver()函数调用一下

在这里插入图片描述
现在我们运行一下,看看效果

在这里插入图片描述
河的碰撞体出现了,我们人物可以站在上面

4. 实现玩家在河中的样子

当我掉落到河里时,应该是游泳的状态,我们要设置一下,在碰撞体那里设置

修改updatePlayerPosition()函数

增加代码

# 检测碰撞
# 这里是玩家和所有碰撞组中的碰撞体检测碰撞,如果发生了碰撞,就会返回碰撞到的碰撞体对象
collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
# 如果发生碰撞,判断是不是在河里
if collider in MainGame.riverGroup:
    # 在河里设置isInWater
    self.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.rightInWaterImage
    else:
        self.player1.image = self.player1.leftInWaterImage
# 判断是不是在陆地上
elif collider in MainGame.landGroup:
        self.player1.isInWater = False

在这里插入图片描述
在这里插入图片描述
这里主要是判断碰撞体是陆地还是河,如果是河就要把 isInWater 设置为True,这样在显示人物图片的时候,就能够显示人在河里的图片

这个代码

# 根据玩家方向,加载落入河中的一瞬间的图片
if self.player1.direction == Direction.RIGHT:
    self.player1.image = self.player1.rightInWaterImage
else:
    self.player1.image = self.player1.leftInWaterImage

用来设置落入河中的一瞬间的图片,即下面的图片

在这里插入图片描述

这幅图是玩家落水溅出水花的图片

下面是完整的updatePlayerPosition()函数

def updatePlayerPosition(self):
    # 在index的循环次数中,不进行碰撞检测,用来让玩家向下跳跃
    if self.index > 0:
        self.index -= 1
        self.player1.rect.x += self.player1.xSpeed
        self.player1.rect.y += self.player1.ySpeed
        self.player1.isDown = False
    else:
        # 首先更新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 = 15
        else:
            # 检测碰撞
            # 这里是玩家和所有碰撞组中的碰撞体检测碰撞,如果发生了碰撞,就会返回碰撞到的碰撞体对象
            collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
            # 如果发生碰撞,判断是不是在河里
            if collider in MainGame.riverGroup:
                # 在河里设置isInWater
                self.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.rightInWaterImage
                else:
                    self.player1.image = self.player1.leftInWaterImage
            # 判断是不是在陆地上
            elif collider in MainGame.landGroup:
                    self.player1.isInWater = False
            # 如果发生碰撞
            if collider:
                # 判断一下人物的y速度,如果大于0,则说明玩家已经接触到了碰撞体表面,需要让玩家站在表面,不掉下去
                if self.player1.ySpeed > 0:
                    self.player1.ySpeed = 0
                    self.player1.state = State.WALK
                    self.player1.rect.bottom = collider.rect.top
            else:
                # 否则的话,我们创建一个玩家的复制
                tempPlayer = copy.copy(self.player1)
                # 让玩家的纵坐标—+1,看看有没有发生碰撞
                tempPlayer.rect.y += 1
                # 如果没有发生碰撞,就说明玩家下面不是碰撞体,是空的
                if not pygame.sprite.spritecollideany(tempPlayer, MainGame.colliderGroup):
                    # 如果此时不是跳跃状态,那么就让玩家变成下落状态,因为玩家在跳跃时,是向上跳跃,不需要对下面的物体进行碰撞检测
                    if tempPlayer.state != State.JUMP:
                        self.player1.state = State.FALL
                tempPlayer.rect.y -= 1

        # 更新x的位置
        self.player1.rect.x += self.player1.xSpeed
        # 同样的检查碰撞
        collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
        # 如果发生了碰撞
        if collider:
            # 判断玩家的x方向速度,如果大于0,表示右边有碰撞体
            if self.player1.xSpeed > 0:
                # 设置玩家的右边等于碰撞体的左边
                self.player1.rect.right = collider.rect.left
            else:
                # 左边有碰撞体
                self.player1.rect.left = collider.rect.right
            self.player1.xSpeed = 0

此时我们运行,会是如下情况

在这里插入图片描述
因为此时是isInWater状态,玩家类的update()函数执行的显示图片的函数是空语句,所以,图片会保持之前的样子,之前我们设置了图片是落水溅出水花的,所以现在我们就看到了上面这幅图片

下面我们对玩家类的waterUpdate()函数进行修改

def waterUpdate(self):
    if self.isSquating:
        if self.direction == Direction.RIGHT:
            self.image = self.diveRightImageInWater
        else:
            self.image = self.diveLeftImageInWater

    if self.isStanding:
        if self.direction == Direction.RIGHT:
            if self.isFiring:
                if self.isUp:
                    self.image = self.upRightImageInWater
                else:
                    self.image = self.fireRightInWater
            else:
                if self.isUp:
                    self.image = self.upRightImageInWater
                else:
                    self.image = self.standRightImageInWater
        else:
            if self.isFiring:
                if self.isUp:
                    self.image = self.upLeftImageInWater
                else:
                    self.image = self.fireLeftInWater
            else:
                if self.isUp:
                    self.image = self.upLeftImageInWater
                else:
                    self.image = self.standLeftImageInWater

    if self.isWalking:
        if self.direction == Direction.RIGHT:
            if self.isUp:
                self.image = self.obliqueRightInWater
            else:
                if self.isFiring:
                    self.image = self.fireRightInWater
                else:
                    self.image = self.standRightImageInWater
        else:
            if self.isUp:
                self.image = self.obliqueLeftInWater
            else:
                if self.isFiring:
                    self.image = self.fireLeftInWater
                else:
                    self.image = self.standLeftImageInWater

根据玩家的状态设置不同的图片

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

在这里插入图片描述
实现了

现在开火试试

在这里插入图片描述
子弹出现了问题,发射子弹的位置不对

接下来我们修改一下

5. 修改玩家在河中开火子弹的位置

我们进入Bullet类
把根据玩家位置设置子弹位置的代码提出来,单独写成一个函数

def landPosition(self, person):
    if person.isStanding:
        if person.direction == Direction.RIGHT:
            if person.isUp:
                self.rect.x += 10 * PLAYER_SCALE
                self.rect.y += -1 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = 0
            else:
                self.rect.x += 24 * PLAYER_SCALE
                self.rect.y += 11 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = 7
        else:
            if person.isUp:
                self.rect.x += 10 * PLAYER_SCALE
                self.rect.y += -1 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = 0
            else:
                self.rect.y += 11 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = -7

    elif person.isSquating and not person.isWalking:
        if person.direction == Direction.RIGHT:
            self.rect.x += 34 * PLAYER_SCALE
            self.rect.y += 25 * PLAYER_SCALE
            self.ySpeed = 0
            self.xSpeed = 7
        else:
            self.rect.y += 25 * PLAYER_SCALE
            self.ySpeed = 0
            self.xSpeed = -7

    elif person.isWalking:
        if person.direction == Direction.RIGHT:
            if person.isUp:
                self.rect.x += 20 * PLAYER_SCALE
                self.rect.y += -1 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = 7
            elif person.isDown:
                self.rect.x += 21 * PLAYER_SCALE
                self.rect.y += 20 * PLAYER_SCALE
                self.ySpeed = 7
                self.xSpeed = 7
            else:
                self.rect.x += 24 * PLAYER_SCALE
                self.rect.y += 11 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = 7
        else:
            if person.isUp:
                self.rect.x += -3 * PLAYER_SCALE
                self.rect.y += -1 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = -7
            elif person.isDown:
                self.rect.x += -3 * PLAYER_SCALE
                self.rect.y += 20 * PLAYER_SCALE
                self.ySpeed = 7
                self.xSpeed = -7
            else:
                self.rect.y += 11 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = -7

    elif person.isJumping or person.state == State.FALL:
        if person.direction == Direction.RIGHT:
            self.rect.x += 16 * PLAYER_SCALE
            self.rect.y += 8 * PLAYER_SCALE
            self.ySpeed = 0
            self.xSpeed = 7
        else:
            self.rect.x += -2 * PLAYER_SCALE
            self.rect.y += 8 * PLAYER_SCALE
            self.ySpeed = 0
            self.xSpeed = -7

原来的位置改成下面的代码

在这里插入图片描述
这里根据玩家是否在水中,设置子弹的位置

我们写一下waterPosition()函数

def waterPosition(self, person):
    if person.isStanding:
        if person.direction == Direction.RIGHT:
            if person.isUp:
                self.rect.x += 14 * PLAYER_SCALE
                self.rect.y += 7 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = 0
            else:
                self.rect.x += 27 * PLAYER_SCALE
                self.rect.y += 29 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = 7
        else:
            if person.isUp:
                self.rect.x += 7 * PLAYER_SCALE
                self.rect.y += 3 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = 0
            else:
                self.rect.x += -1 * PLAYER_SCALE
                self.rect.y += 29 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = -7

    elif person.isWalking:
        if person.direction == Direction.RIGHT:
            if person.isUp:
                self.rect.x += 23 * PLAYER_SCALE
                self.rect.y += 17 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = 7
            else:
                self.rect.x += 27 * PLAYER_SCALE
                self.rect.y += 29 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = 7
        else:
            if person.isUp:
                self.rect.x += -3 * PLAYER_SCALE
                self.rect.y += -1 * PLAYER_SCALE
                self.ySpeed = -7
                self.xSpeed = -7
            else:
                self.rect.x += -1 * PLAYER_SCALE
                self.rect.y += 29 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = -7

玩家子弹发射的位置参考下面的示意图

斜着开火

在这里插入图片描述

向上开火
在这里插入图片描述

左右开火
在这里插入图片描述
写好后,我们来运行一下

在这里插入图片描述
运行正常

我们再试试潜入水中,开火

在这里插入图片描述
发现是可以发射出子弹的,这里不对,在水中是不能发射子弹的

在这里插入图片描述
而且在水中我们也可以跳跃,这也是不行的,下面我们来解决一下这个问题

6. 解决玩家在潜水状态可以发射子弹的问题

修改玩家类fire()函数

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 = currentTime

设置潜水状态不能开火

运行一下
在这里插入图片描述
现在潜水状态就不能开火啦

7. 解决玩家在河中可以跳跃的问题

首先把玩家类walkingInWater()函数修改一下

def walkingInWater(self, currentTime):
    if self.isStanding:
        # 设置为斜射
        if self.direction == Direction.RIGHT and self.isUp:
            self.image = self.upRightImageInWater
        elif self.direction == Direction.RIGHT and not self.isUp:
            self.image = self.standRightImageInWater
        elif self.direction == Direction.LEFT and self.isUp:
            self.image = self.upLeftImageInWater
        elif self.direction == Direction.LEFT and not self.isUp:
            self.image = self.standLeftImageInWater
        self.runLastTimer = currentTime
    else:
        # 如果是走动的图片
        if self.direction == Direction.RIGHT:
            self.xSpeed = PLAYER_X_SPEED
            if self.isUp:
                self.image = self.obliqueRightInWater
                self.runLastTimer = currentTime
            else:
                self.image = self.standRightImageInWater
                self.runLastTimer = currentTime
        else:
            self.xSpeed = PLAYER_X_SPEED
            if self.isUp:
                self.image = self.obliqueLeftInWater
                self.runLastTimer = currentTime
            else:
                self.image = self.standLeftImageInWater
                self.runLastTimer = currentTime

其次修改玩家类walking()函数

在这里插入图片描述
改成下面的样子

在这里插入图片描述
完整的walking()函数代码

def walking(self, keys, currentTime, playerBulletList):
    """角色行走,每10帧变换一次图片"""
    self.isStanding = False
    self.isWalking = True
    self.isJumping = False
    self.isSquating = False
    self.isFiring = False
    self.ySpeed = 0
    self.xSpeed = PLAYER_X_SPEED

    if self.isInWater:
        self.walkingInWater(currentTime)
    else:
        self.walkingInLand(currentTime)

    # 按下D键
    if keys[pygame.K_d]:
        self.direction = Direction.RIGHT
        self.xSpeed = PLAYER_X_SPEED
    # 按下A键
    elif keys[pygame.K_a]:
        self.direction = Direction.LEFT
        self.xSpeed = -PLAYER_X_SPEED
     # 按下S键
    elif keys[pygame.K_s]:
        self.isStanding = False
        self.isDown = True
        self.isUp = False

    # 按下W键
    if keys[pygame.K_w]:
        self.isUp = True
        self.isDown = False
    # 没有按键按下
    else:
        self.state = State.STAND

    # 移动时按下K键
    if keys[pygame.K_k]:
        # 角色状态变为跳跃
        if not self.isInWater:
            self.state = State.JUMP
            self.ySpeed = self.jumpSpeed
            self.isJumping = True
            self.isStanding = False
            self.isUp = True

    if keys[pygame.K_j]:
        self.fire(currentTime, playerBulletList)

同样地,standing()函数中的跳跃代码也要修改

在这里插入图片描述
完整的standing()函数代码

def standing(self, keys, currentTime, playerBulletList):
    """角色站立"""

    # 设置角色状态
    self.isStanding = True
    self.isWalking = False
    self.isJumping = False
    self.isSquating = False
    self.isUp = False
    self.isDown = False
    self.isFiring = False

    # 设置速度
    self.ySpeed = 0
    self.xSpeed = 0

    # 按下A键
    if keys[pygame.K_a]:
        # A按下,角色方向向左
        self.direction = Direction.LEFT
        # 改变角色的状态,角色进入移动状态
        self.state = State.WALK
        # 设置站立状态为False,移动状态为True
        self.isStanding = False
        self.isWalking = True
        # 向左移动,速度为负数,这样玩家的x坐标是减小的
        self.xSpeed = -PLAYER_X_SPEED
    # 按下D键
    elif keys[pygame.K_d]:
        # D按下,角色方向向右
        self.direction = Direction.RIGHT
        # 改变角色的状态,角色进入移动状态
        self.state = State.WALK
        # 设置站立状态为False,移动状态为True
        self.isStanding = False
        self.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 = False
            self.isJumping = True
            # 设置速度,速度为负数,因为角色跳起后,要下落
            self.isUp = True
            self.ySpeed = self.jumpSpeed
    # 没有按下按键
    else:
        # 没有按下按键,角色依然是站立状态
        self.state = State.STAND
        self.isStanding = True

    # 按下w键
    if keys[pygame.K_w]:
        # W按下,角色向上,改变方向状态
        self.isUp = True
        self.isStanding = True
        self.isDown = False
        self.isSquating = False
    # 按下s键
    elif keys[pygame.K_s]:
        # S按下,角色蹲下,改变方向状态,并且蹲下状态设置为True
        self.isUp = False
        self.isStanding = False
        self.isDown = True
        self.isSquating = True

    if keys[pygame.K_j]:
        self.fire(currentTime, playerBulletList)

接下来可以运行一下了,试试效果

在这里插入图片描述
发现跳跃问题就解决啦

8. 解决玩家从河到陆地状态没有转换的问题

我们现在试试让玩家上岸

在这里插入图片描述
发现上岸了还是在水中的样子

我们来解决一下这个问题

这个问题是因为在水平方向上碰撞体不判断所导致

进入主类的updatePlayerPosition()函数,找到下面圈出的代码

在这里插入代码片

这个代码是水平移动时检测碰撞体,这里我们要加入垂直检测碰撞体

首先创建一个玩家的复制,让他的纵坐标 +1 ,看看有没有遇到碰撞,如果有,说明玩家下方有碰撞体,然后判断一下碰撞体属于哪一个组,根据组的种类判断是否在水中

直接加入代码

tempPlayer = copy.copy(self.player1)
tempPlayer.rect.y += 1
if c := pygame.sprite.spritecollideany(tempPlayer, MainGame.colliderGroup):
    if c in MainGame.landGroup:
        self.player1.isInWater = False
    elif c in MainGame.riverGroup:
        self.player1.isInWater = True
tempPlayer.rect.y -= 1

在这里插入图片描述

之后运行一下,看看有没有解决
在这里插入图片描述

哈哈,修复了

下面是完整的代码

完整的主类代码

import copy
import sys
import pygame
from Constants import *
from PlayerOne import PlayerOne
from Collider import Collider


def drawPlayerOneBullet(player1BulletList):
    for bullet in player1BulletList:
        if bullet.isDestroy:
            player1BulletList.remove(bullet)
        else:
            bullet.draw(MainGame.window)
            bullet.move()

class MainGame:

    player1 = None
    allSprites = None

    window = None

    # 子弹
    player1BulletList = []

    # 冲突
    landGroup = pygame.sprite.Group()
    colliderGroup = pygame.sprite.Group()
    riverGroup = pygame.sprite.Group()

    # 冲突栈
    colliderStack = []

    def __init__(self):
        # 初始化展示模块
        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 = 60
        self.clock = pygame.time.Clock()

        # 初始化角色
        MainGame.player1 = PlayerOne(pygame.time.get_ticks())
        # 设置角色的初始位置
        MainGame.player1.rect.x = 80
        MainGame.player1.rect.bottom = 0

        # 把角色放入组中,方便统一管理
        MainGame.allSprites = pygame.sprite.Group(MainGame.player1)

        # 读取背景图片
        self.background = pygame.image.load('../Image/Map/第一关BG.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 = -1280

        # 摄像头调整
        self.cameraAdaption = 0

        # 加载场景景物
        self.initLand()
        self.initRiver()

        # 碰撞失效间隔
        self.index = 0

    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)
        # 更新物体
        currentTime = pygame.time.get_ticks()
        MainGame.allSprites.update(self.keys, currentTime, player1BulletList)
        self.updatePlayerPosition()
        drawPlayerOneBullet(player1BulletList)
        # 摄像机移动
        self.camera()
        # 显示物体
        MainGame.allSprites.draw(window)
        for collider in MainGame.landGroup:
            r = collider.draw(window, self.player1.rect.y)
            # 如果没有画出来,表示玩家高度低于直线,所有把直线从组中删除
            if not r:
                # 删除前先检查一下是不是在组中
                if collider in MainGame.colliderGroup:
                    # 删除并加入栈
                    MainGame.colliderStack.insert(0, collider)
                    MainGame.colliderGroup.remove(collider)
            else:
                # 如果画出来了,判断一下玩家距离是否高于线的距离
                if collider.rect.y > self.player1.rect.bottom:
                    # 如果是的话,且冲突栈不为空,那么从栈中取出一个元素放入冲突组,最前面的元素一定是先如队列的
                    if len(MainGame.colliderStack) > 0:
                        f = MainGame.colliderStack.pop()
                        MainGame.colliderGroup.add(f)
        MainGame.riverGroup.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
                # 场景中的物体都走这么多距离
                for sprite in MainGame.allSprites:
                    sprite.rect.x -= self.cameraAdaption
                for collider in MainGame.colliderGroup:
                    collider.rect.x -= self.cameraAdaption
                for collider in MainGame.colliderStack:
                    collider.rect.x -= self.cameraAdaption

    def initLand(self):
        land1 = Collider(81, 119 * MAP_SCALE, 737 * 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)
        MainGame.landGroup = pygame.sprite.Group(land1, land2, land3, land4, land5, land6)
        MainGame.colliderGroup.add(MainGame.landGroup)

    def initRiver(self):
        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))
        MainGame.riverGroup.add(river1)
        MainGame.riverGroup.add(river2)
        MainGame.riverGroup.add(river3)
        MainGame.colliderGroup.add(MainGame.riverGroup)

    def updatePlayerPosition(self):
        # 在index的循环次数中,不进行碰撞检测,用来让玩家向下跳跃
        if self.index > 0:
            self.index -= 1
            self.player1.rect.x += self.player1.xSpeed
            self.player1.rect.y += self.player1.ySpeed
            self.player1.isDown = False
        else:
            # 首先更新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 = 15
            else:
                # 检测碰撞
                # 这里是玩家和所有碰撞组中的碰撞体检测碰撞,如果发生了碰撞,就会返回碰撞到的碰撞体对象
                collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
                # 如果发生碰撞,判断是不是在河里
                if collider in MainGame.riverGroup:
                    # 在河里设置isInWater
                    self.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.rightInWaterImage
                    else:
                        self.player1.image = self.player1.leftInWaterImage
                # 判断是不是在陆地上
                elif collider in MainGame.landGroup:
                        self.player1.isInWater = False
                # 如果发生碰撞
                if collider:
                    # 判断一下人物的y速度,如果大于0,则说明玩家已经接触到了碰撞体表面,需要让玩家站在表面,不掉下去
                    if self.player1.ySpeed > 0:
                        self.player1.ySpeed = 0
                        self.player1.state = State.WALK
                        self.player1.rect.bottom = collider.rect.top
                else:
                    # 否则的话,我们创建一个玩家的复制
                    tempPlayer = copy.copy(self.player1)
                    # 让玩家的纵坐标—+1,看看有没有发生碰撞
                    tempPlayer.rect.y += 1
                    # 如果没有发生碰撞,就说明玩家下面不是碰撞体,是空的
                    if not pygame.sprite.spritecollideany(tempPlayer, MainGame.colliderGroup):
                        # 如果此时不是跳跃状态,那么就让玩家变成下落状态,因为玩家在跳跃时,是向上跳跃,不需要对下面的物体进行碰撞检测
                        if tempPlayer.state != State.JUMP:
                            self.player1.state = State.FALL
                    tempPlayer.rect.y -= 1

            # 更新x的位置
            self.player1.rect.x += self.player1.xSpeed
            # 同样的检查碰撞
            collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
            # 如果发生了碰撞
            if collider:
                # 判断玩家的x方向速度,如果大于0,表示右边有碰撞体
                if self.player1.xSpeed > 0:
                    # 设置玩家的右边等于碰撞体的左边
                    self.player1.rect.right = collider.rect.left
                else:
                    # 左边有碰撞体
                    self.player1.rect.left = collider.rect.right
                self.player1.xSpeed = 0

            tempPlayer = copy.copy(self.player1)
            tempPlayer.rect.y += 1
            if c := pygame.sprite.spritecollideany(tempPlayer, MainGame.colliderGroup):
                if c in MainGame.landGroup:
                    self.player1.isInWater = False
                elif c in MainGame.riverGroup:
                    self.player1.isInWater = True
            tempPlayer.rect.y -= 1


if __name__ == '__main__':
    MainGame().run()

完整的玩家类代码

import pygame
from Constants import *
from Bullet import Bullet


class PlayerOne(pygame.sprite.Sprite):

    def __init__(self, currentTime):
        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 = currentTime
        self.fireLastTimer = currentTime

        # 选择当前要显示的图片
        self.image = self.standRightImage
        # 获取图片的rect
        self.rect = self.image.get_rect()
        # 设置角色的状态
        self.state = State.FALL
        # 角色的方向
        self.direction = Direction.RIGHT
        # 速度
        self.xSpeed = PLAYER_X_SPEED
        self.ySpeed = 0
        self.jumpSpeed = -11
        # 人物当前的状态标志
        self.isStanding = False
        self.isWalking = False
        self.isJumping = True
        self.isSquating = False
        self.isFiring = False
        self.isInWater = False
        # 重力加速度
        self.gravity = 0.8

        # 玩家上下方向
        self.isUp = False
        self.isDown = False

    def 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.downRightImage
            else:
                # 加载向左蹲下的图片
                self.image = self.downLeftImage

        # 角色站着
        if self.isStanding:
            if self.direction == Direction.RIGHT:
                if self.isUp:
                    # 加载向右朝上的图片
                    self.image = self.upRightImage
                elif self.isDown:
                    # 加载向右蹲下的图片
                    self.image = self.downRightImage
                else:
                    # 加载向右站着的图片
                    self.image = self.standRightImage
            else:
                # 向左也是同样的效果
                if self.isUp:
                    self.image = self.upLeftImage
                elif self.isDown:
                    self.image = self.downLeftImage
                else:
                    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.diveRightImageInWater
            else:
                self.image = self.diveLeftImageInWater

        if self.isStanding:
            if self.direction == Direction.RIGHT:
                if self.isFiring:
                    if self.isUp:
                        self.image = self.upRightImageInWater
                    else:
                        self.image = self.fireRightInWater
                else:
                    if self.isUp:
                        self.image = self.upRightImageInWater
                    else:
                        self.image = self.standRightImageInWater
            else:
                if self.isFiring:
                    if self.isUp:
                        self.image = self.upLeftImageInWater
                    else:
                        self.image = self.fireLeftInWater
                else:
                    if self.isUp:
                        self.image = self.upLeftImageInWater
                    else:
                        self.image = self.standLeftImageInWater

        if self.isWalking:
            if self.direction == Direction.RIGHT:
                if self.isUp:
                    self.image = self.obliqueRightInWater
                else:
                    if self.isFiring:
                        self.image = self.fireRightInWater
                    else:
                        self.image = self.standRightImageInWater
            else:
                if self.isUp:
                    self.image = self.obliqueLeftInWater
                else:
                    if self.isFiring:
                        self.image = self.fireLeftInWater
                    else:
                        self.image = self.standLeftImageInWater

    def standing(self, keys, currentTime, playerBulletList):
        """角色站立"""

        # 设置角色状态
        self.isStanding = True
        self.isWalking = False
        self.isJumping = False
        self.isSquating = False
        self.isUp = False
        self.isDown = False
        self.isFiring = False

        # 设置速度
        self.ySpeed = 0
        self.xSpeed = 0

        # 按下A键
        if keys[pygame.K_a]:
            # A按下,角色方向向左
            self.direction = Direction.LEFT
            # 改变角色的状态,角色进入移动状态
            self.state = State.WALK
            # 设置站立状态为False,移动状态为True
            self.isStanding = False
            self.isWalking = True
            # 向左移动,速度为负数,这样玩家的x坐标是减小的
            self.xSpeed = -PLAYER_X_SPEED
        # 按下D键
        elif keys[pygame.K_d]:
            # D按下,角色方向向右
            self.direction = Direction.RIGHT
            # 改变角色的状态,角色进入移动状态
            self.state = State.WALK
            # 设置站立状态为False,移动状态为True
            self.isStanding = False
            self.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 = False
                self.isJumping = True
                # 设置速度,速度为负数,因为角色跳起后,要下落
                self.isUp = True
                self.ySpeed = self.jumpSpeed
        # 没有按下按键
        else:
            # 没有按下按键,角色依然是站立状态
            self.state = State.STAND
            self.isStanding = True

        # 按下w键
        if keys[pygame.K_w]:
            # W按下,角色向上,改变方向状态
            self.isUp = True
            self.isStanding = True
            self.isDown = False
            self.isSquating = False
        # 按下s键
        elif keys[pygame.K_s]:
            # S按下,角色蹲下,改变方向状态,并且蹲下状态设置为True
            self.isUp = False
            self.isStanding = False
            self.isDown = True
            self.isSquating = True

        if keys[pygame.K_j]:
            self.fire(currentTime, playerBulletList)

    def walking(self, keys, currentTime, playerBulletList):
        """角色行走,每10帧变换一次图片"""
        self.isStanding = False
        self.isWalking = True
        self.isJumping = False
        self.isSquating = False
        self.isFiring = False
        self.ySpeed = 0
        self.xSpeed = PLAYER_X_SPEED

        if self.isInWater:
            self.walkingInWater(currentTime)
        else:
            self.walkingInLand(currentTime)

        # 按下D键
        if keys[pygame.K_d]:
            self.direction = Direction.RIGHT
            self.xSpeed = PLAYER_X_SPEED
        # 按下A键
        elif keys[pygame.K_a]:
            self.direction = Direction.LEFT
            self.xSpeed = -PLAYER_X_SPEED
         # 按下S键
        elif keys[pygame.K_s]:
            self.isStanding = False
            self.isDown = True
            self.isUp = False

        # 按下W键
        if keys[pygame.K_w]:
            self.isUp = True
            self.isDown = False
        # 没有按键按下
        else:
            self.state = State.STAND

        # 移动时按下K键
        if keys[pygame.K_k]:
            # 角色状态变为跳跃
            if not self.isInWater:
                self.state = State.JUMP
                self.ySpeed = self.jumpSpeed
                self.isJumping = True
                self.isStanding = False
                self.isUp = True

        if 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.standRightImage
            elif self.direction == Direction.LEFT and self.isUp:
                self.image = self.upLeftImage
            elif self.direction == Direction.LEFT and not self.isUp:
                self.image = self.standLeftImage
            # 记下当前时间
            self.runLastTimer = currentTime
        else:
            # 如果是走动的图片,先判断方向
            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 += 1
                        else:
                            self.imageIndex = 0
                        self.runLastTimer = currentTime
            else:
                self.xSpeed = -PLAYER_X_SPEED
                if self.isUp or self.isDown:
                    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 += 1
                        else:
                            self.imageIndex = 0
                        self.runLastTimer = currentTime

    def walkingInWater(self, currentTime):
        if self.isStanding:
            # 设置为斜射
            if self.direction == Direction.RIGHT and self.isUp:
                self.image = self.upRightImageInWater
            elif self.direction == Direction.RIGHT and not self.isUp:
                self.image = self.standRightImageInWater
            elif self.direction == Direction.LEFT and self.isUp:
                self.image = self.upLeftImageInWater
            elif self.direction == Direction.LEFT and not self.isUp:
                self.image = self.standLeftImageInWater
            self.runLastTimer = currentTime
        else:
            # 如果是走动的图片
            if self.direction == Direction.RIGHT:
                self.xSpeed = PLAYER_X_SPEED
                if self.isUp:
                    self.image = self.obliqueRightInWater
                    self.runLastTimer = currentTime
                else:
                    self.image = self.standRightImageInWater
                    self.runLastTimer = currentTime
            else:
                self.xSpeed = PLAYER_X_SPEED
                if self.isUp:
                    self.image = self.obliqueLeftInWater
                    self.runLastTimer = currentTime
                else:
                    self.image = self.standLeftImageInWater
                    self.runLastTimer = currentTime

    def jumping(self, keys, currentTime, playerBulletList):
        """跳跃"""
        # 设置标志
        self.isJumping = True
        self.isStanding = False
        self.isDown = False
        self.isSquating = False
        self.isFiring = False
        # 更新速度
        self.ySpeed += self.gravity
        if currentTime - self.runLastTimer > 115:
            if self.upImageIndex < 3:
                self.upImageIndex += 1
            else:
                self.upImageIndex = 0
            # 记录变换图片的时间,为下次变换图片做准备
            self.runLastTimer = currentTime

        if keys[pygame.K_d]:
            self.direction = Direction.RIGHT

        elif keys[pygame.K_a]:
            self.direction = Direction.LEFT

        # 按下W键
        if keys[pygame.K_w]:
            self.isUp = True
            self.isDown = False
        elif keys[pygame.K_s]:
            self.isUp = False
            self.isDown = True

        if self.ySpeed >= 0:
            self.state = State.FALL

        if not keys[pygame.K_k]:
            self.state = State.FALL

        if keys[pygame.K_j]:
            self.fire(currentTime, playerBulletList)

    def falling(self, keys, currentTime, playerBulletList):
        # 下落时速度越来越快,所以速度需要一直增加
        self.ySpeed += self.gravity
        if currentTime - self.runLastTimer > 115:
            if self.upImageIndex < 3:
                self.upImageIndex += 1
            else:
                self.upImageIndex = 0
            self.runLastTimer = currentTime

        if keys[pygame.K_d]:
            self.direction = Direction.RIGHT
            self.isWalking = False

        elif keys[pygame.K_a]:
            self.direction = Direction.LEFT
            self.isWalking = False

        if 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 = currentTime

完整的子弹类代码

import pygame
from Constants import *

class Bullet(pygame.sprite.Sprite):

    def __init__(self, person):
        pygame.sprite.Sprite.__init__(self)
        self.images = [
            loadImage('../Image/Bullet/bullet1.png')
        ]
        self.index = 0
        self.image = self.images[self.index]
        # 速度
        self.xSpeed = 1
        self.ySpeed = 1
        self.rect = pygame.Rect(person.rect)

        if person.isInWater:
            self.waterPosition(person)
        else:
            self.landPosition(person)

        # 销毁开关
        self.isDestroy = False

    def landPosition(self, person):
        if person.isStanding:
            if person.direction == Direction.RIGHT:
                if person.isUp:
                    self.rect.x += 10 * PLAYER_SCALE
                    self.rect.y += -1 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = 0
                else:
                    self.rect.x += 24 * PLAYER_SCALE
                    self.rect.y += 11 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = 7
            else:
                if person.isUp:
                    self.rect.x += 10 * PLAYER_SCALE
                    self.rect.y += -1 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = 0
                else:
                    self.rect.y += 11 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = -7

        elif person.isSquating and not person.isWalking:
            if person.direction == Direction.RIGHT:
                self.rect.x += 34 * PLAYER_SCALE
                self.rect.y += 25 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = 7
            else:
                self.rect.y += 25 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = -7

        elif person.isWalking:
            if person.direction == Direction.RIGHT:
                if person.isUp:
                    self.rect.x += 20 * PLAYER_SCALE
                    self.rect.y += -1 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = 7
                elif person.isDown:
                    self.rect.x += 21 * PLAYER_SCALE
                    self.rect.y += 20 * PLAYER_SCALE
                    self.ySpeed = 7
                    self.xSpeed = 7
                else:
                    self.rect.x += 24 * PLAYER_SCALE
                    self.rect.y += 11 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = 7
            else:
                if person.isUp:
                    self.rect.x += -3 * PLAYER_SCALE
                    self.rect.y += -1 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = -7
                elif person.isDown:
                    self.rect.x += -3 * PLAYER_SCALE
                    self.rect.y += 20 * PLAYER_SCALE
                    self.ySpeed = 7
                    self.xSpeed = -7
                else:
                    self.rect.y += 11 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = -7

        elif person.isJumping or person.state == State.FALL:
            if person.direction == Direction.RIGHT:
                self.rect.x += 16 * PLAYER_SCALE
                self.rect.y += 8 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = 7
            else:
                self.rect.x += -2 * PLAYER_SCALE
                self.rect.y += 8 * PLAYER_SCALE
                self.ySpeed = 0
                self.xSpeed = -7

    def waterPosition(self, person):
        if person.isStanding:
            if person.direction == Direction.RIGHT:
                if person.isUp:
                    self.rect.x += 14 * PLAYER_SCALE
                    self.rect.y += 7 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = 0
                else:
                    self.rect.x += 27 * PLAYER_SCALE
                    self.rect.y += 29 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = 7
            else:
                if person.isUp:
                    self.rect.x += 7 * PLAYER_SCALE
                    self.rect.y += 3 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = 0
                else:
                    self.rect.x += -1 * PLAYER_SCALE
                    self.rect.y += 29 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = -7

        elif person.isWalking:
            if person.direction == Direction.RIGHT:
                if person.isUp:
                    self.rect.x += 23 * PLAYER_SCALE
                    self.rect.y += 17 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = 7
                else:
                    self.rect.x += 27 * PLAYER_SCALE
                    self.rect.y += 29 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = 7
            else:
                if person.isUp:
                    self.rect.x += -3 * PLAYER_SCALE
                    self.rect.y += -1 * PLAYER_SCALE
                    self.ySpeed = -7
                    self.xSpeed = -7
                else:
                    self.rect.x += -1 * PLAYER_SCALE
                    self.rect.y += 29 * PLAYER_SCALE
                    self.ySpeed = 0
                    self.xSpeed = -7

    def move(self):
        self.rect.x += self.xSpeed
        self.rect.y += self.ySpeed
        self.checkBullet()

    def draw(self, window):
        window.blit(self.image, self.rect)

    def checkBullet(self):
        toDestroy = False
        if self.rect.top < 0 or self.rect.top > 600:
            toDestroy = True
        if self.rect.left < 0 or self.rect.right > 900:
            toDestroy = True
        if toDestroy:
            self.isDestroy = True

猜你喜欢

转载自blog.csdn.net/qq_37354060/article/details/129267260
今日推荐