[英]python - Jumping in pygame
我想编辑以下跳转代码。
我想让角色快速跳跃(减少jump_count ),但在每次减少jump_count 时,我希望跳跃的最大高度值保持不变。 没有任何改变。 你能帮助我吗?
更多解释:
jump_count = 10
跳跃高度 = 0.6
最大跳跃高度= -36.999999999999986
jump_count = 5
跳跃高度=???
最大跳跃高度= -36.999999999999986
jump_count = 3
跳跃高度=???
最大跳跃高度= -36.999999999999986
代码:
class Character:
def __init__(self):
# Position
self.x = 70
self.y = 194
# Move
self.velocity = 10
# jump:
self.is_jump = False
# Jump count
self.jump_count_range = 10
self.jump_count = self.jump_count_range
# Jump height
self.constant_jump_height_range = 0.6
self.jump_height = self.constant_jump_height_range
def Jump(self):
if self.is_jump:
if self.jump_count >= -1 * self.jump_count_range:
neg = 1
if self.jump_count < 0:
neg = -1
self.y -= (self.jump_count ** 2) * self.jump_height * neg
self.jump_count -= 1
else:
self.is_jump = False
self.jump_count = self.jump_count_range
只需添加类似max_jump_height
的东西,就像这样
class Character:
def __init__(self):
# Position
self.x = 70
self.y = 194
# Move
self.velocity = 10
# jump:
self.is_jump = False
# Jump count
self.jump_count_range = 10
self.jump_count = self.jump_count_range
# Jump height
self.constant_jump_height_range = 0.6
self.jump_height = self.constant_jump_height_range
self.max_jump_height = -36.999999999999986
def Jump(self):
if self.is_jump:
if self.jump_count >= -1 * self.jump_count_range:
neg = 1
if self.jump_count < 0:
neg = -1
if self.max_jump_height < (self.jump_count ** 2) * self.jump_height * neg:
return
else:
self.y -= (self.jump_count ** 2) * self.jump_height * neg
self.jump_count -= 1
else:
self.is_jump = False
self.jump_count = self.jump_count_range
尝试添加这个:
float JumpVelocity,
float JumpDampening=0.1f;
void Jump()
{
JumpVelocity = 0.5f;
gameObject.rigidbody.useGravity = false;
}
void FixedUpdate()
{
Vector3 pos = transform.position;
if (JumpVelocity != 0)
{
pos.y += JumpVelocity;
JumpVelocity -= JumpDampening;
if (JumpVelocity <= 0)
{
gameObject.rigidbody.useGravity = true;
JumpVelocity = 0;
}
}
transform.position = pos;
}
在上面的 object 上的重力在跳跃时关闭,在跳跃弧的峰值重新打开,因此重力再次接管以使 GO 下降。 在 JumVelocity 中使用较高的起始值以获得更快的跳跃,在 JumpDampening 中使用较高的值以更快地减慢跳跃或较低的值以使跳跃变得更高并具有更明显的弧线。
或者你可以通过做来玩弄重力
if(inAir) { // most code has something like this already
Vector3 vel = rigidbody.velocity;
vel.y-=BONUS_GRAV*Time.deltaTime;
rigidbody.velocity=vel;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.