Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable height jump demo
#1
I ran across some javascript code (link at top of code below) that shows how to do a dead simple variable height jump. It's much simpler than the methods I have been using over the years.

Click the mouse inside the window to initiate a jump.
The longer the mouse button is held the higher the jump.

Code: (Select All)
'
' Variable height jump demo
'
' Based on code found at: https://jsfiddle.net/LyM87/
'
' Click the mouse inside the window.
' The longer the mouse button is held the higher the jump.
' Press ESC to exit the program.
'
TYPE BoxTYPE
    positionX AS SINGLE ' x location of box
    positionY AS SINGLE ' y location of box
    velocityX AS SINGLE ' horizontal velocity of box
    velocityY AS SINGLE ' vertical velocity of box
    onGround AS INTEGER ' box on ground (-1 yes, 0 no)
END TYPE

DIM Box AS BoxTYPE '      the box
DIM gravity AS SINGLE '   amount of gravity

Box.positionX = 100 '                                   set initial start values
Box.positionY = 175
Box.velocityX = 4
Box.velocityY = 0
Box.onGround = -1
gravity = .5
SCREEN _NEWIMAGE(200, 200, 32) '                        graphics csreen
DO '                                                    begin main loop

    '+---------------------------------+
    '| Initiate a variable height jump |
    '+---------------------------------+

    WHILE _MOUSEINPUT: WEND '                           get latest mouse update
    IF _MOUSEBUTTON(1) THEN '                           is mouse button down? (start jump)
        IF Box.onGround THEN '                          yes, is box on the ground?
            Box.velocityY = -12 '                       yes, add vertical velocity
            Box.onGround = 0 '                          box is now off the ground
        END IF
    ELSE '                                              no, mouse button has been released (end jump)
        IF Box.velocityY < -6 THEN '                    is vertical velocity above -6?
            Box.velocityY = -6 '                        yes, limit vertical velocity to -6
        END IF
    END IF

    '+---------------------+
    '| Update box position |
    '+---------------------+

    Box.velocityY = Box.velocityY + gravity '           add positive value to vertical velocity
    Box.positionY = Box.positionY + Box.velocityY '     add vertical velocity to box current y location
    Box.positionX = Box.positionX + Box.velocityX '     add horizontal velocity to box current x location
    IF Box.positionY > 175 THEN '                       has box gone below ground?
        Box.positionY = 175 '                           yes, place box on ground
        Box.velocityY = 0 '                             remove vertical velocity
        Box.onGround = -1 '                             box is now on the ground
    END IF
    IF Box.positionX < 10 OR Box.positionX > 190 THEN ' has box reached either side of screen?
        Box.velocityX = -Box.velocityX '                yes, reverse horizontal velocity
    END IF

    '+----------------------------+
    '| Render screen with changes |
    '+----------------------------+

    _LIMIT 30 '                                         30 frames per second
    CLS '                                               clear screen
    LINE (0, 175)-(200, 175) '                          draw ground line
    LINE (Box.positionX - 10, Box.positionY - 20)-(Box.positionX + 10, Box.positionY), , B ' draw box
    _DISPLAY '                                          update screen with changes
LOOP UNTIL _KEYDOWN(27) '                               leave when ESC pressed
SYSTEM '                                                return to OS
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply




Users browsing this thread: 1 Guest(s)