Player

%%html
<style>
    #canvas {
        margin: 0;
        border: 1px solid black;
    }
</style>
<canvas id="canvas"></canvas>
<script>
    let canvas = document.getElementById('canvas');
    let c = canvas.getContext('2d');
    canvas.width = 770;
    canvas.height = 300;

    const gravity = 1.5;

    class Player {
        constructor() {
            this.position = {
                x: 100,
                y: 100
            };
            this.velocity = {
                x: 0,
                y: 0
            };
            this.width = 30;
            this.height = 30;
        }

        draw() {
            c.fillStyle = 'red';
            c.fillRect(this.position.x, this.position.y, this.width, this.height);
        }

        update() {
            this.draw();
            this.position.y += this.velocity.y;
            this.position.x += this.velocity.x;

            if (this.position.y + this.height + this.velocity.y <= canvas.height)
                this.velocity.y += gravity;
            else
                this.velocity.y = 0;
        }
    }

    player = new Player();
    const keys = {
        right: {
            pressed: false
        },
        left: {
            pressed: false
        }
    };

    function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);
        player.update();

        if (keys.right.pressed) {
            player.velocity.x = 15;
        } else if (keys.left.pressed) {
            player.velocity.x = -15;
        } else {
            player.velocity.x = 0;
        }
    }

    animate();

    addEventListener('keydown', ({ keyCode }) => {
        switch (keyCode) {
            case 65:
                console.log('left');
                keys.left.pressed = true;
                break;
            case 83:
                console.log('down');
                break;
            case 68:
                console.log('right');
                keys.right.pressed = true;
                break;
            case 87:
                console.log('up');
                player.velocity.y -= 20;
                break;
        }
    });

    addEventListener('keyup', ({ keyCode }) => {
        switch (keyCode) {
            case 65:
                console.log('left');
                keys.left.pressed = false;w
                break;
            case 83:
                console.log('down');
                break;
            case 68:
                console.log('right');
                keys.right.pressed = false;
                break;
            case 87:
                console.log('up');
                player.velocity.y -= 20;
                break;
        }
    });
</script>

Block

%%html
<style>
    #canvas2 {
        margin: 0;
        border: 1px solid black;
    }
</style>
<canvas id="canvas2"></canvas>
<script>
    const canvas2 = document.getElementById('canvas2');
    const c2 = canvas2.getContext('2d');
    canvas2.width = 770;
    canvas2.height = 300;
    const gravity2 = 1.5;
    
    class Player2 {
        constructor() {
            this.position = {
                x: 100,
                y: 100
            };
            this.velocity = {
                x: 0,
                y: 0
            };
            this.width = 30;
            this.height = 30;
        }
        
        draw() {
            c2.fillStyle = 'red';
            c2.fillRect(this.position.x, this.position.y, this.width, this.height);
        }
        
        update() {
            this.draw();
            this.position.y += this.velocity.y;
            this.position.x += this.velocity.x;
            
            if (this.position.y + this.height + this.velocity.y <= canvas2.height)
                this.velocity.y += gravity2;
            else
                this.velocity.y = 0;
        }
    }
    
    class BlockObject {
        constructor(image1) {
            this.position = {
                x: 200,
                y: 100
            };
            this.image1 = image1;
            this.width = 158;
            this.height = 79;
        }
        
        draw() {
            c2.drawImage(this.image1, this.position.x, this.position.y);
        }
    }

    let image1 = new Image();
    image1.onload = function() {
        const player2 = new Player2();
        const blockObject = new BlockObject(image1);
        const keys2 = {
            right: {
                pressed: false
            },
            left: {
                pressed: false
            }
        };

        function animate2() {
            requestAnimationFrame(animate2);
            c2.clearRect(0, 0, canvas2.width, canvas2.height);
            player2.update();
            blockObject.draw();
            if (keys2.right.pressed) {
                player2.velocity.x = 5;
            } else if (keys2.left.pressed) {
                player2.velocity.x = -5;
            } else {
                player2.velocity.x = 0;
            }
            if (
                player2.position.y + player2.height <= blockObject.position.y &&
                player2.position.y + player2.height + player2.velocity.y >= blockObject.position.y &&
                player2.position.x + player2.width >= blockObject.position.x &&
                player2.position.x <= blockObject.position.x + blockObject.width
            ) {
                player2.velocity.y = 0;
            }
        }

        animate2();

        addEventListener('keydown', ({ keyCode }) => {
            switch (keyCode) {
                case 65:
                    console.log('left');
                    keys2.left.pressed = true;
                    break;
                case 83:
                    console.log('down');
                    break;
                case 68:
                    console.log('right');
                    keys2.right.pressed = true;
                    break;
                case 87:
                    console.log('up');
                    player2.velocity.y -= 20;
                    break;
            }
        });

        addEventListener('keyup', ({ keyCode }) => {
            switch (keyCode) {
                case 65:
                    console.log('left');
                    keys2.left.pressed = false;
                    break;
                case 83:
                    console.log('down');
                    break;
                case 68:
                    console.log('right');
                    keys2.right.pressed = false;
                    break;
                case 87: 
                    console.log('up');
                    player2.velocity.y -= 20;
                    break;
            }
        });
    };

    image1.src = 'https://samayass.github.io/samayaCSA/images/box.png';

</script>