platformer – How to control the jump strength of how long the player presses the jump button?

You basically have 2 options. You can apply a constant decreasing force to the character while the player presses the space button and stop immediately when he releases it or apply a higher force of gravity when the player releases the space and returns to gravity normal when the jump reaches the top. For example, the first Super Mario Bros. used the latter.

The first option is in my opinion much more intuitive and gives you a nicer curve, but the last one can be a little more responsive and gives you better control of the jump height.

The first is also harder to understand from a game design perspective, because instead of setting a maximum jump height, you only set the minimum.

The implementation of both is quite easy, for the first, you will have to store the current jump force somewhere. Bytecode

jumpForce = 10
jumpDampening = 0.8 // This determines how much the jump force weakens each image.

// Later
if (jumpKeyDown) {
player.velocityY + = jumpForce
jumpForce * = jumpDampening
} other {
jumpForce = 10
}

// apply gravity

The other must also detect when the jump reaches its peak:

normalGravity = -9.81
stopGravity = -30
JumpForce = 30

// Later
if (playerIsOnGround && jumpKeyDown) {
player.velocityY + = jumpForce
}

// Apply gravity depending on whether the jump has reached the top or not
if (! jumpKeyDown && player.velocityY> 0) {
player.velocityY + = stoppingGravity
} other {
player.velocityY + = normalGravity
}

Side note:

If you want to get a "tighter" jump, always apply the highest severity when the player does not hold the jump button. This will allow the character to fall more quickly to the ground, which will give more control.