学习 Python 之 Pygame 开发魂斗罗

Posted _DiMinisH

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习 Python 之 Pygame 开发魂斗罗相关的知识,希望对你有一定的参考价值。

学习 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学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗

学习 Python 之 Pygame 开发魂斗罗