html head title Platform Game title style body margin 0 padding 0 player position absolute bottom 0 left 0 width 50px height 50px background color blue platform position absolute bottom 100px left 0 width 200px height 20px background color green style head body div id player div div id platform div script Set up variables var player document getElementById player var platform document getElementById platform var gravity 0 5 var jumpSpeed 10 var playerX 0 var playerY 200 var playerSpeedY 0 var platformX 0 Set up game loop setInterval function Update player position playerY playerSpeedY playerSpeedY gravity player style bottom playerY px player style left playerX px Check for collision with platform if playerY 120 playerX 120 platformX playerX platformX 200 playerY 120 playerSpeedY 0 Update platform position platformX 5 if platformX 200 platformX 500 platform style left platformX px 20 Handle key events document addEventListener keydown function event if event keyCode 32 Space key is pressed jump playerSpeedY jumpSpeed else if event keyCode 37 Left arrow key is pressed move left playerX 5 else if event keyCode 39 Right arrow key is pressed move right playerX 5 script body html
player style bottom playerY px player style left playerX px Check for collision with platform if playerY 120 playerX 120 platformX playerX platformX 200 playerY 120 playerSpeedY 0 Update platform position platformX 5 if platformX 200 platformX 500 platform style left platformX px 20 Handle key events document addEventListener keydown function event if event keyCode 32 Space key is pressed jump playerSpeedY jumpSpeed else if event keyCode 37 Left arrow key is pressed move left playerX 5 else if event keyCode 39 Right arrow key is pressed move right playerX 5 script body html