简体   繁体   English

python - 跳入 pygame

[英]python - Jumping in pygame

I want to edit on the following jump code.我想编辑以下跳转代码。

I want to make the character jump fast (decrease jump_count ), but in each decreasing of jump_count I want the value of maximum height of jump stay same.我想让角色快速跳跃(减少jump_count ),但在每次减少jump_count 时,我希望跳跃的最大高度值保持不变。 without any changing.没有任何改变。 Can you help me?你能帮助我吗?


More explain :更多解释

jump_count = 10 jump_count = 10

jump_height = 0.6跳跃高度 = 0.6

maximum height of jump= -36.999999999999986最大跳跃高度= -36.999999999999986


jump_count = 5 jump_count = 5

jump_height =???跳跃高度=???

maximum height of jump= -36.999999999999986最大跳跃高度= -36.999999999999986


jump_count = 3 jump_count = 3

jump_height =???跳跃高度=???

maximum height of jump= -36.999999999999986最大跳跃高度= -36.999999999999986


Code:代码:

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

Just add something like max_jump_height , like this只需添加类似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

Try adding this instead:尝试添加这个:

 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;
 }

In the above gravity on the object is off when jumping up, turned back on at the peak of the jump arc so gravity takes over again to bring the GO down.在上面的 object 上的重力在跳跃时关闭,在跳跃弧的峰值重新打开,因此重力再次接管以使 GO 下降。 Play with higher start values in JumVelocity for quicker jumps, higher values in JumpDampening to slow the jump down quicker or lower values to let the jump get higher and have a more noticeable arc.在 JumVelocity 中使用较高的起始值以获得更快的跳跃,在 JumpDampening 中使用较高的值以更快地减慢跳跃或较低的值以使跳跃变得更高并具有更明显的弧线。

or you could just play around with the gravity by doing或者你可以通过做来玩弄重力

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM