人物的行走速度这边分成水平方向(X轴)和竖直方向(Y轴),水平方向的速度要考虑加速度和摩擦力,竖直方向的速度要考虑重力加速度。
游戏中的人物有下面几个主要的状态:
向上跳和向下降落的状态判断可能一开始比较难理解,可以看后面的具体实现,目的是如果玩家长按jump键时,可以让人物跳的更高。
上面的判断是否站在某个物体上,或者是否碰到某个物体,就需要用到物体之间的碰撞检测。
对于游戏中出现的每一样东西,比如砖块,箱子,水管,地面,还有人物都可以看成是一个独立的物体,所以每个物体类都继承了pygame的精灵类pg.sprite.Sprite,可以使用精灵类提供的碰撞检测函数来判断。
设置source\constants.py 中的变量DEBUG值为True,可以看到图1的游戏截图,比如最简单的地面,可以看成是一个长方形的物体。
因为人物是站在地面上,且水平速度为0,所以当前的人物状态就是站立不动。
图1
游戏实现代码的github链接 超级玛丽
这边是csdn的下载链接 超级玛丽
有一个单独的人物类,在source\components\player.py 中,其中有个handle_state 函数,根据人物当前的状态执行不同的函数。
为了简洁下面所有函数中将不相关的代码都省略掉了。
def handle_state(self, keys, fire_group):
if self.state == c.STAND:
self.standing(keys, fire_group)
elif self.state == c.WALK:
self.walking(keys, fire_group)
elif self.state == c.JUMP:
self.jumping(keys, fire_group)
elif self.state == c.FALL:
self.falling(keys, fire_group)
人物的状态就是上面说的4个状态:
人物类关于行走速度的成员变量先了解下:
水平方向相关的
竖直方向相关的
看下最复杂的 walking 函数,keys数组是当前按下的键盘输入,tools.keybinding中值的含义如下
keybinding = {
'action':pg.K_s,
'jump':pg.K_a,
'left':pg.K_LEFT,
'right':pg.K_RIGHT,
'down':pg.K_DOWN
}
def walking(self, keys, fire_group):
if keys[tools.keybinding['action']]:
self.max_x_vel = self.max_run_vel
self.x_accel = self.run_accel
else:
self.max_x_vel = self.max_walk_vel
self.x_accel = self.walk_accel
if keys[tools.keybinding['jump']]:
if self.allow_jump:
self.state = c.JUMP
if abs(self.x_vel) > 4:
self.y_vel = self.jump_vel - .5
else:
self.y_vel = self.jump_vel
if keys[tools.keybinding['left']]:
self.facing_right = False
if self.x_vel > 0:
self.frame_index = 5
self.x_accel = c.SMALL_TURNAROUND
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel, True)
elif keys[tools.keybinding['right']]:
self.facing_right = True
if self.x_vel < 0:
self.frame_index = 5
self.x_accel = c.SMALL_TURNAROUND
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel)
else:
if self.facing_right:
if self.x_vel > 0:
self.x_vel -= self.x_accel
else:
self.x_vel = 0
self.state = c.STAND
else:
if self.x_vel < 0:
self.x_vel += self.x_accel
else:
self.x_vel = 0
self.state = c.STAND
def cal_vel(self, vel, max_vel, accel, isNegative=False):
""" max_vel and accel must > 0 """
if isNegative:
new_vel = vel * -1
else:
new_vel = vel
if (new_vel + accel) < max_vel:
new_vel += accel
else:
new_vel = max_vel
if isNegative:
return new_vel * -1
else:
return new_vel
再看下jumping 函数,
JUMP_GRAVITY = .31
GRAVITY = 1.01
def jumping(self, keys, fire_group):
""" y_vel value: positive is down, negative is up """
self.allow_jump = False
self.frame_index = 4
self.gravity = c.JUMP_GRAVITY
self.y_vel += self.gravity
if self.y_vel >= 0 and self.y_vel < self.max_y_vel:
self.gravity = c.GRAVITY
self.state = c.FALL
if keys[tools.keybinding['right']]:
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel)
elif keys[tools.keybinding['left']]:
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel, True)
if not keys[tools.keybinding['jump']]:
self.gravity = c.GRAVITY
self.state = c.FALL
standing函数和 falling 函数比较简单,就省略了。
人物的碰撞检测代码在 source\states\level.py 中的入口是update_player_position函数 ,可以看到这边分成水平方向和竖直方向:
def update_player_position(self):
self.player.rect.x += round(self.player.x_vel)
if self.player.rect.x < self.start_x:
self.player.rect.x = self.start_x
elif self.player.rect.right > self.end_x:
self.player.rect.right = self.end_x
self.check_player_x_collisions()
if not self.player.dead:
self.player.rect.y += round(self.player.y_vel)
self.check_player_y_collisions()
具体实现时将同一类物体放在一个pygame.sprite.Group类中,
pygame.sprite.Group
A container class to hold and manage multiple Sprite objects.
Group(*sprites) -> Group
这样每次调用pg.sprite.spritecollideany 函数就能判断人物和这一类物体是否有碰撞。
pygame.sprite.spritecollideany()
Simple test if a sprite intersects anything in a group.
spritecollideany(sprite, group, collided = None) -> Sprite Collision with the returned sprite.
spritecollideany(sprite, group, collided = None) -> None No collision
不同物体的group如下,另外敌人,金币和蘑菇等物体的碰撞检测先忽略。
因为不同种类group撞击时,后续产生的结果会有区别,所有需要对每一类group分别进行碰撞检测。
X轴方向上面3类group如果检测到有碰撞时,会调用adjust_player_for_x_collisions 函数,来调整人物的X轴位置。
def check_player_x_collisions(self):
ground_step_pipe = pg.sprite.spritecollideany(self.player, self.ground_step_pipe_group)
brick = pg.sprite.spritecollideany(self.player, self.brick_group)
box = pg.sprite.spritecollideany(self.player, self.box_group)
...
if box:
self.adjust_player_for_x_collisions(box)
elif brick:
self.adjust_player_for_x_collisions(brick)
elif ground_step_pipe:
if (ground_step_pipe.name == c.MAP_PIPE and
ground_step_pipe.type == c.PIPE_TYPE_HORIZONTAL):
return
self.adjust_player_for_x_collisions(ground_step_pipe)
elif powerup:
...
elif enemy:
...
elif coin:
...
adjust_player_for_x_collisions 函数先根据人物和碰撞物体的X轴相对位置,判断人物在碰撞物体的左边还是右边,来调整人物的X轴位置,然后设置人物水平方向的速度为0。
def adjust_player_for_x_collisions(self, collider):
if collider.name == c.MAP_SLIDER:
return
if self.player.rect.x < collider.rect.x:
self.player.rect.right = collider.rect.left
else:
self.player.rect.left = collider.rect.right
self.player.x_vel = 0
check_player_y_collisions 函数也是对不同group分别进行碰撞检测,Y轴方向这3类group如果检测到有碰撞时,会调用adjust_player_for_y_collisions 函数,来调整人物的Y轴位置。
最后调用check_is_falling函数判断人物是否要设成 向下降落 的状态。
def check_player_y_collisions(self):
ground_step_pipe = pg.sprite.spritecollideany(self.player, self.ground_step_pipe_group)
# decrease runtime delay: when player is on the ground, don't check brick and box
if self.player.rect.bottom < c.GROUND_HEIGHT:
brick = pg.sprite.spritecollideany(self.player, self.brick_group)
box = pg.sprite.spritecollideany(self.player, self.box_group)
brick, box = self.prevent_collision_conflict(brick, box)
else:
brick, box = False, False
if box:
self.adjust_player_for_y_collisions(box)
elif brick:
self.adjust_player_for_y_collisions(brick)
elif ground_step_pipe:
self.adjust_player_for_y_collisions(ground_step_pipe)
elif enemy:
...
elif shell:
...
self.check_is_falling(self.player)
adjust_player_for_y_collisions 函数先根据人物和碰撞物体的Y轴相对位置,判断人物在碰撞物体的下边还是上边,来调整人物的Y轴位置
def adjust_player_for_y_collisions(self, sprite):
if self.player.rect.top > sprite.rect.top:
if sprite.name == c.MAP_BRICK:
...
elif sprite.name == c.MAP_BOX:
...
elif (sprite.name == c.MAP_PIPE and
sprite.type == c.PIPE_TYPE_HORIZONTAL):
return
self.player.y_vel = 7
self.player.rect.top = sprite.rect.bottom
self.player.state = c.FALL
else:
self.player.y_vel = 0
self.player.rect.bottom = sprite.rect.top
if self.player.state == c.FLAGPOLE:
self.player.state = c.WALK_AUTO
elif self.player.state == c.END_OF_LEVEL_FALL:
self.player.state = c.WALK_AUTO
else:
self.player.state = c.WALK
check_is_falling函数 判断人物下方是否有物体,有个小技巧,就是先将人物的Y轴位置向下移动1,然后判断和上面三类group是否有碰撞:
最后将人物的Y轴位置恢复(向上移动1)。
def check_is_falling(self, sprite):
sprite.rect.y += 1
check_group = pg.sprite.Group(self.ground_step_pipe_group,
self.brick_group, self.box_group)
if pg.sprite.spritecollideany(sprite, check_group) is None:
if (sprite.state == c.WALK_AUTO or
sprite.state == c.END_OF_LEVEL_FALL):
sprite.state = c.END_OF_LEVEL_FALL
elif (sprite.state != c.JUMP and
sprite.state != c.FLAGPOLE and
not self.in_frozen_state()):
sprite.state = c.FALL
sprite.rect.y -= 1