学习 Python 之 Pygame 开发魂斗罗(六)
- 继续编写魂斗罗
- 1. 创建碰撞类
- 2. 给地图添加碰撞体
- 3. 让人物可以掉下去
- 4. 实现人物向下跳跃
- 5. 完整的代码
继续编写魂斗罗
在上次的博客学习 Python 之 Pygame 开发魂斗罗(五)中,我们实现了加载地图和地图随玩家移动,接下来我们来实现一下物体碰撞
在魂斗罗中,有些地方玩家可以站在上面,但是有些地方是不可以的,下面我们来实现一下
下面是图片的素材
链接:https://pan.baidu.com/s/1X7tESkes_O6nbPxfpHD6hQ?pwd=hdly
提取码:hdly
1. 创建碰撞类
import pygame
from Constants import *
class Collider(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height)).convert()
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, window, y):
window.blit(self.image, self.rect)
碰撞体要继承pygame.sprite.Sprite
类
下面的图中,标出了碰撞体的位置,在这些平台上,玩家不会掉了,如果没有站在这些平台上,玩家就会掉落
这些画线的地方,实际上也是图片
在碰撞类代码中,先创建了一个图片,然后指定颜色,我们设置为红色,方便在地图上看到这些碰撞体
self.image = pygame.Surface((width, height)).convert()
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
2. 给地图添加碰撞体
在主类中增加类变量
# 冲突
landGroup = pygame.sprite.Group()
colliderGroup = pygame.sprite.Group()
landGroup 来存放陆地碰撞体
colliderGroup 来存放所以的碰撞体
接下来实现一个函数用来创建陆地碰撞体
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)
首先单独创建6块陆地,然后加入陆地组,然后把陆地组加入碰撞体全体组
其中,这6块陆地的位置,是我在地图中计算出来的
LAND_THICKNESS是陆地的厚度,在Constants.py中增加变量
有了创建陆地碰撞体函数,我们来调用一下
在主类__init__()函数中增加代码
# 加载场景景物
self.initLand()
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()
修改update()函数
def update(self, window, player1BulletList):
# 加载背景
window.blit(self.background, self.backRect)
# 更新物体
currentTime = pygame.time.get_ticks()
MainGame.allSprites.update(self.keys, currentTime, player1BulletList)
drawPlayerOneBullet(player1BulletList)
# 摄像机移动
self.camera()
# 显示物体
MainGame.allSprites.draw(window)
for collider in MainGame.landGroup:
collider.draw(window, self.player1.rect.y)
然后修改camera()函数,让所有碰撞体也要移动
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
之后我们来运行一下,看看有没有问题
我们发现玩家并没有落在红线上,这是为什么?
因为我们在玩家类中设置了,当高度到达指定位置时,就会停下,所以我们要修改玩家类的update()函数
把玩家类中的更新位置的代码注释掉,我们在主类中创建一个函数,用来更新位置
def updatePlayerPosition(self):
# 首先更新y的位置
self.player1.rect.y += self.player1.ySpeed
# 检测碰撞
# 这里是玩家和所有碰撞组中的碰撞体检测碰撞,如果发生了碰撞,就会返回碰撞到的碰撞体对象
collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
# 如果发生碰撞
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
接下来调用一下这个函数
接下来运行游戏,看看效果
可以看到人物落到了线上
我们一直向左走,看看能不能掉下去
发现掉不下去,我们需要修改一下代码
3. 让人物可以掉下去
在人物的falling()函数中,我们之前是设置了人物不能掉到地面高度以下
正是这个原因,才让角色不会下落
我们把这段代码删了即可
再运行一下,看看能不能掉下去
走到桥头,没有红线了,就掉下去了,这就实现了人物掉下去,后面只需要设置一下,掉下去算玩家死亡,根据玩家生命值判断是否让玩家复活即可
4. 实现人物向下跳跃
当我们运行游戏时,按住s和k键,应该是可以跳到下面的台阶的,但是现在是不行的,我们来写一下代码
我们首先考虑一下,为什么向下跳玩家掉不下去?
玩家向下跳跃后,我们看到实际他是向上跳跃,并且回落到原来的位置上,与玩家脚下的陆地发生了碰撞,让玩家无法掉下去,那么我们得想一个办法,我们可以这样做:让玩家向下跳跃时,下方的陆地不进行碰撞检测,这样玩家就可以掉下去了,而且玩家向下跳跃时,起跳的高度不应该跟正常跳跃的高度一样高
玩家向下跳,isDown和isJumping都会为True,根据这个条件,我们来写实现
思路:当玩家向下跳跃时,让玩家在一定的时间内不进行碰撞检测,这样就可以不检测脚下的陆地,而检测落下去的陆地,只要时间设置的合适,就可以让当前脚下的陆地不进行碰撞检测,这个时间我们用循环来设置,即在指定的循环次数内,不进行碰撞检测
下面我们来实现一下
在主类__init__()函数中设置变量
index变量用来设置多少次循环内不进行碰撞检测
修改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
else:
# 检测碰撞
# 这里是玩家和所有碰撞组中的碰撞体检测碰撞,如果发生了碰撞,就会返回碰撞到的碰撞体对象
collider = pygame.sprite.spritecollideany(self.player1, MainGame.colliderGroup)
# 如果发生碰撞
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
当玩家按下向下跳跃后,在35次循环内,玩家不进行碰撞检测,35次是我自己调试得出的,大家可以自己试试,看看具体是多少次,也许每个人的是不一样的
我们运行一下,发现往下跳不下去
我们修改一下跳跃的加速度
改成0.8
再运行一下
现在就实现了往下跳了,但是不能往上跳,因为向上跳会碰撞检测,跳不上去,会出现问题(飞到线的边界掉落下去)
接下来我们来解决一下这个问题
为了让人物能够向上跳跃到上面的横线,我们可以这样做:当人物跳下去之后,上面的横线消失,当人物再次向上跳跃的时候,当人物过了线,线就会显示出来
如下方两幅图片所显示的
当玩家再向上跳的时候,线又重新出现了
尽管玩家没有踩上去,但是当玩家过了线的高度时,线又重新出现了
按照上面所说的思路,我们就可以实现玩家向上跳跃了
首先,我们要修改一下Collider类的draw()函数
当玩家的高度 大于 当前碰撞体的高度时,碰撞体不画出来,画出来返回True,没有画出来返回False
class Collider(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height)).convert()
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, window, y):
if y > self.rect.bottom:
return False
else:
window.blit(self.image, self.rect)
return True
虽然没有画出来,但是实际上是存在碰撞体的,只不过我们看不到,我们要根据返回值,把没画出来的碰撞体从总的碰撞体组中删除掉,等到玩家的高度超过删除掉的碰撞体的高度时,再把它显示出来
接下来,在主类中增加一个变量,用来保存刚才删除的碰撞体
# 冲突栈
colliderStack = []
修改update()函数中的代码
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)
然后在updatePlayerPosition()函数中增加代码
让玩家向上跳跃时,也不会进行碰撞检测,这样玩家向上跳跃时就可以直接跳过横线,刚一跳过横线,就开始碰撞检测,刚好就可以落到线上不掉下去了
下面我们运行一下看看效果
我们发现,直接按跳跃键,就会出问题
但是按住向上和跳跃键,就没有问题,可以跳上横线
我们需要修改一下代码,让普通跳跃也能触发向上的按键
修改玩家类的standing()函数,让玩家在跳跃时,方向就会向上
self.isUp = True
同样,在walking()函数中,也要加上
修改完后,我们再修改一下主类
在camera()函数中,放到碰撞体栈的物体也要更新,不然玩家跳下去了,向前移动后,再跳上来,原来的横线的位置会偏右,因为没有随地图的移动向后移动
到此,我们代码就完了,试试效果
我们发现没有任何问题啦
5. 完整的代码
主类代码
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()
# 冲突栈
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.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)
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 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:
# 判断一下人物的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
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.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.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)
# # 更新位置
# # 记录前一次的位置坐标
# 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.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.isFiring = True
if len(playerBulletList) < PLAYER_BULLET_NUMBER:
if currentTime - self.fireLastTimer > 150:
playerBulletList.append(Bullet(self))
self.fireLastTimer = currentTime
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.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
# 按下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.isFiring = True
if len(playerBulletList) < PLAYER_BULLET_NUMBER:
if currentTime - self.fireLastTimer > 150:
playerBulletList.append(Bullet(self))
self.fireLastTimer = 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.isFiring = True
if len(playerBulletList) < PLAYER_BULLET_NUMBER:
if currentTime - self.fireLastTimer > 150:
playerBulletList.append(Bullet(self))
self.fireLastTimer = currentTime
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 self.rect.bottom > GROUND_HEIGHT:
# self.state = State.WALK
# self.ySpeed = 0
# self.rect.bottom = GROUND_HEIGHT
# self.isJumping = False
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.isFiring = True
if len(playerBulletList) < PLAYER_BULLET_NUMBER:
if currentTime - self.fireLastTimer > 150:
playerBulletList.append(Bullet(self))
self.fireLastTimer = currentTime
碰撞体类代码
import pygame
from Constants import *
class Collider(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height)).convert()
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, window, y):
if y > self.rect.bottom:
return False
else:
window.blit(self.image, self.rect)
return True
子弹类代码
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 * 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
# 销毁开关
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
常量代码
from enum import Enum
import pygame
class State(Enum):
STAND = 1
WALK = 2
JUMP = 3
FALL = 4
SQUAT = 5
class Direction(Enum):
RIGHT = 1
LEFT = 2
# 玩家放缩倍数
PLAYER_SCALE = 1.9
def loadImage(filename, hReverse = False):
image = pygame.image.load(filename)
if hReverse:
image = pygame.transform.flip(image, True, False)
rect = image.get_rect()
image = pygame.transform.scale(
image,
(int(rect.width * PLAYER_SCALE), int(rect.height * PLAYER_SCALE))
)
image = image.convert_alpha()
return image
# 设置窗口大小
SCREEN_HEIGHT = 600
SCREEN_WIDTH = 800
GROUND_HEIGHT = 295
# 玩家x方向速度
PLAYER_X_SPEED = 3
# 设置玩家子弹上限
PLAYER_BULLET_NUMBER = 15
# 地图放缩倍数
MAP_SCALE = 2.5
# 陆地的厚度
LAND_THICKNESS = 1
# 一块地的长度
LAND_LENGTH = 32