使用math.sin函数可以模拟角色的跳跃。
如下图所示当sin(0) = 0,sin(pi)=0

取得跳跃高度的函数定义如下:
BounceHeight是角色最高可以跳的高度,当math.sin()值为1的时候,就可以达到最大值。
bounceRate 则指跳跃的速度,该值越大则跳跃越慢
currentBounce这个值则是由用户循环增大的,即由0,增加到bounceRate,也就是说可以实现sin函数由0-1-0这个变化过程。
def getBounceAmount(currentBounce, bounceRate, bounceHeight):
# Returns the number of pixels to offset based on the bounce.
# Larger bounceRate means a slower bounce.
# Larger bounceHeight means a higher bounce.
# currentBounce will always be less than bounceRate
return int(math.sin( (math.pi / float(bounceRate)) * currentBounce ) * bounceHeight)
跳跃函数例子:
#当角色在移动或者playerObj['bounce']!=0,即正在跳跃中时,则+1,
#也就是说即使不是移动状态的,如果已经跳起则必须跳完,即完成0》1》0的跳跃过程。
if (moveLeft or moveRight or moveUp or moveDown) or playerObj['bounce'] != 0:
playerObj['bounce'] += 1#CurrentBounce+1加到BounceRate ,这样以完成sin(pi/BounceRate×currentBounce)变化过程 *0》1》0
if playerObj['bounce'] > BOUNCERATE:
playerObj['bounce'] = 0 # reset bounce amount
1824

被折叠的 条评论
为什么被折叠?



