Wednesday, December 24, 2014

Monkey - 2D Player jumping code example

The code below is an example where a rectangle can jump with gravity. The space bar is used to trigger the jump if on the ground.

Code below :

Import mojo

Global px:Float = 320
Global py:Float = 240
Global playerjump:Bool = False
Global pincy:Float = 0

Class MyGame Extends App
    Method OnCreate()
    Method OnUpdate()
        ' If the player is on the ground and the space bar is pressed
        If playerjump = False And KeyDown(KEY_SPACE) = True
            pincy = -3
            playerjump = True
        'If the player is in the jump
        If playerjump = True
            pincy += 0.1
            'if the player is going up
            If pincy <=0
                For Local i:Int = 0 Until Abs(pincy)
                    py -= 1
            ' if the player if going down
            If pincy > 0
                For Local i:Int = 0 Until pincy
                    py += 1
                    'if the player touches the ground
                    If py > 240 Then 
                        playerjump = False
                        py = 240
    Method OnRender()
        DrawRect px,py,32,32
        DrawText "Press space bar to jump the rectangle player",10,10

Function Main()
    New MyGame()


  1. Pincy is short for player increase y. It is the variable that moves the player y location. (Velocity) it gets set withg a force (jump start) that moves the player up. Then this force decreases over time so the player jumping up looks like he is going less hard up. This value pincy then decreases to the point that it gets negative so the player goes down.(gravity)

    1. Thank you! I have used this player jumping structure in my game, but have found that the player (sprite) jumps relatively unnaturally, he can also continue to jump if i hold down my jump button. Not sure why this happens.

    2. I think my replay got lost somehow. It did not show up. Trying again!

      In the code above I use a variable (isjumping) that is flagged to true when the player starts his jump. When he is back on the ground this variable is flagged to false.
      This variable can so be used to check(IF) when you press a button if he should not jump again so he does not jump in mid air.

      Sometimes you want to be able to jump when you are already jumping. This a double jump. But this should be done not immediatly at your first jump but a little bif after. (in a certain range of the value of pincy for instance)

  2. Yeah thank you, have now fixed the problem. What do the two loops do?

    1. The two loops in the code are to allow checks for the collision with the tiles one pixel at a time. With the gravity here the movement can be more then one pixel at a time. Because of this it could mean the player could fall through a block or jump though a block.
      We want the player to be able to detect a touch with something one pixel ahead of time. So it can be assigned the next situation.

      The loops make sure that if the incy is for instance 5 down it goes per pixel and every pixel it allows for checks for the collision. In this example >240(floor)

      I hope this makes sense...

  3. Is it possible to reduce how high the block jumps and make it fall quicker? Kind regards

    1. pincy is the variable that is used to set the jump strength. It is set to -3 in this example. If you set it to for instance -5 he will jump higher. (pincy = player increase y)

      In the code there is a part where the pincy variable is increased (decreasing the jump and turn into falling) The part in the code is : pincy += 0.1
      If you set that to for instance 0.5 he will fall faster. You might then also need to set the jump strenght higher also since this is connected.