首页IT科技魂斗罗python源代码(学习 Python 之 Pygame 开发魂斗罗(四))

魂斗罗python源代码(学习 Python 之 Pygame 开发魂斗罗(四))

时间2025-06-17 22:25:04分类IT科技浏览6933
导读:继续编写魂斗罗 在上次的博客学习 Python 之 Pygame 开发魂斗罗(三)中,我们完成了角色的移动和跳跃,下面我们来继续写魂斗罗。...

继续编写魂斗罗

在上次的博客学习 Python 之 Pygame 开发魂斗罗(三)中              ,我们完成了角色的移动和跳跃                       ,下面我们来继续写魂斗罗               。

下面是图片的素材

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

提取码:hdly

1. 创建子弹类

发射子弹首先要有子弹类

我们来创建一下 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) # 销毁开关 self.isDestroy = False 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

2. 根据玩家方向和状态设置子弹发射的位置

由于玩家的方向和状态不一样        ,所以玩家开火后              ,子弹发射的位置也不一样

下面我们来看看角色状态和发射子弹的位置

(1). 站立向右发射子弹

(x,y) 是图片的位置                      ,我们计算出        ,此状态下       ,子弹发射的位置是 (x+24,y+11)

下面我们来设置一下

self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7

2.5是人物放大的倍数                      ,加载图像的时候把图像放大了2.5倍               ,所以设置子弹发射位置的时候       ,同样要乘以2.5

if person.isStanding: # 判断方向 # 方向向右 if person.direction == Direction.RIGHT: # 向上 if person.isUp: pass # 向右 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 # 方向向左 else: if person.isUp: pass else: pass elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: pass else: pass elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: pass elif person.isDown: pass else: pass else: if person.isUp: pass elif person.isDown: pass else: pass elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass (2). 站立向左发射子弹 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 # 根据角色的方向和状态设置子弹发射的位置 # 角色站着 if person.isStanding: # 判断方向 if person.direction == Direction.RIGHT: # 向上 if person.isUp: pass else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: pass else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: pass else: pass elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: pass elif person.isDown: pass else: pass else: if person.isUp: pass elif person.isDown: pass else: pass elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass (3). 站立朝上发射子弹

站立向右朝上发射子弹

self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0

这里我微微调整了一下y的值                      ,方向向上               ,所以子弹的y是逐渐减小的,所以y速度是负数

# 根据角色的方向和状态设置子弹发射的位置 # 角色站着 if person.isStanding: # 判断方向 if person.direction == Direction.RIGHT: # 向上 if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: pass else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: pass else: pass elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: pass elif person.isDown: pass else: pass else: if person.isUp: pass elif person.isDown: pass else: pass elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass

站立向左朝上发射子弹也是同样的计算方式

self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 # 根据角色的方向和状态设置子弹发射的位置 # 角色站着 if person.isStanding: # 判断方向 if person.direction == Direction.RIGHT: # 向上 if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: pass else: pass elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: pass elif person.isDown: pass else: pass else: if person.isUp: pass elif person.isDown: pass else: pass elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass (4). 蹲下发射子弹

向右示意图

self.rect.x += 34 * 2.5 self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = 7 # 根据角色的方向和状态设置子弹发射的位置 # 角色站着 if person.isStanding: # 判断方向 if person.direction == Direction.RIGHT: # 向上 if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: self.rect.x += 34 * 2.5 self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: pass elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: pass elif person.isDown: pass else: pass else: if person.isUp: pass elif person.isDown: pass else: pass elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass

同样的                      ,设置向左的发射位置

self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = -7 # 根据角色的方向和状态设置子弹发射的位置 # 角色站着 if person.isStanding: # 判断方向 if person.direction == Direction.RIGHT: # 向上 if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: self.rect.x += 34 * 2.5 self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: pass elif person.isDown: pass else: pass else: if person.isUp: pass elif person.isDown: pass else: pass elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass (5). 向斜方发射子弹

向斜右上方发射子弹示意图

self.rect.x += 20 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 7

向斜左上方发射子弹时的子弹位置

self.rect.x += -3 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = -7

向斜右下方发射子弹示意图

self.rect.x += 21 * 2.5 self.rect.y += 20 * 2.5 self.ySpeed = 7 self.xSpeed = 7

向斜左下方发射子弹时的子弹位置

self.rect.x += -3 * 2.5 self.rect.y += 20 * 2.5 self.ySpeed = 7 self.xSpeed = -7 (6). 奔跑时发射子弹

奔跑时发射子弹和站立发射子弹是一样的

向右

self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7

向左

self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7

完整的代码

if person.isStanding: if person.direction == Direction.RIGHT: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: self.rect.x += 34 * 2.5 self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: self.rect.x += 20 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 7 elif person.isDown: self.rect.x += 21 * 2.5 self.rect.y += 20 * 2.5 self.ySpeed = 7 self.xSpeed = 7 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += -3 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = -7 elif person.isDown: self.rect.x += -3 * 2.5 self.rect.y += 20 * 2.5 self.ySpeed = 7 self.xSpeed = -7 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: pass else: pass (7). 跳跃时发射子弹 elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: self.rect.x += 16 * 2.5 self.rect.y += 8 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: self.rect.x += -2 * 2.5 self.rect.y += 8 * 2.5 self.ySpeed = 0 self.xSpeed = -7

完整子弹类代码

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.isStanding: if person.direction == Direction.RIGHT: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += 10 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 0 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isSquating and not person.isWalking: if person.direction == Direction.RIGHT: self.rect.x += 34 * 2.5 self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: self.rect.y += 25 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isWalking: if person.direction == Direction.RIGHT: if person.isUp: self.rect.x += 20 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = 7 elif person.isDown: self.rect.x += 21 * 2.5 self.rect.y += 20 * 2.5 self.ySpeed = 7 self.xSpeed = 7 else: self.rect.x += 24 * 2.5 self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: if person.isUp: self.rect.x += -3 * 2.5 self.rect.y += -1 * 2.5 self.ySpeed = -7 self.xSpeed = -7 elif person.isDown: self.rect.x += -3 * 2.5 self.rect.y += 20 * 2.5 self.ySpeed = 7 self.xSpeed = -7 else: self.rect.y += 11 * 2.5 self.ySpeed = 0 self.xSpeed = -7 elif person.isJumping or person.state == State.FALL: if person.direction == Direction.RIGHT: self.rect.x += 16 * 2.5 self.rect.y += 8 * 2.5 self.ySpeed = 0 self.xSpeed = 7 else: self.rect.x += -2 * 2.5 self.rect.y += 8 * 2.5 self.ySpeed = 0 self.xSpeed = -7 # 销毁开关 self.isDestroy = False 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

3. 在玩家类中加入开火

在常量文件中设置子弹上限

# 设置玩家子弹上限 PLAYER_BULLET_NUMBER = 15

在玩家类的四个状态函数中加入发射的代码

if keys[pygame.K_j]: self.isFiring = True if len(playerBulletList) < PLAYER_BULLET_NUMBER: # 设置子弹发射的间隔为150 if currentTime - self.fireLastTimer > 150: playerBulletList.append(Bullet(self)) self.fireLastTimer = currentTime

完整的玩家类

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.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.STAND # 角色的方向 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.gravity = 0.7 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) # 更新位置 # 记录前一次的位置坐标 pre = self.rect.x self.rect.x += self.xSpeed self.rect.y += self.ySpeed # 如果x位置小于0了                       ,就不能移动,防止人物跑到屏幕左边 if self.rect.x <= 0: self.rect.x = pre # 更新动画 # 跳跃状态 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 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.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.

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
提升seo搜索排名(如何提高SEO关键词首页排名) 如何优化SEO提升流量站的流量?(掌握这些技巧,轻松实现流量站的快速提升!)