以下是完整的代码:

<!DOCTYPE html>
<html>

<head>
    <style>
        canvas {
            border: 1px solid black;
            display: block;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <canvas id="gameCanvas" width="800" height="600"></canvas>
    <script>
        const canvas = document.getElementById("gameCanvas");
        const context = canvas.getContext("2d");
        const mario = {
            x: 50,
            y: 0,
            width: 40,
            height: 40,
            speed: 5,
            jumpPower: 10,
            isJumping: false,
            yVelocity: 0,
        };
        const floor = {
            x: 0,
            y: canvas.height - 20,
            width: canvas.width,
            height: 20,
        };
        const blockWidth = 40;
        const blockHeight = 40;
        const blocks = [
            [200, canvas.height - 100],   // 方块1的位置
            [400, canvas.height - 150],   // 方块2的位置
            [600, canvas.height - 100],   // 方块3的位置
            [200, canvas.height - 100],   // 方块1的位置
            [400, canvas.height - 150],   // 方块2的位置
            [800, canvas.height - 500],   // 方块3的位置
            [200, canvas.height - 400],   // 方块1的位置
            [100, canvas.height - 250],   // 方块2的位置
            [700, canvas.height - 100],   // 方块3的位置
        ];
        const keys = {};
        document.addEventListener("keydown", (event) => {
            keys[event.key] = true;
        });
        document.addEventListener("keyup", (event) => {
            keys[event.key] = false;
        });
        function update() {
            // Movement
            if (keys["ArrowLeft"] && mario.x > 0) {
                mario.x -= mario.speed;
            }
            if (keys["ArrowRight"] && mario.x + mario.width < canvas.width) {
                mario.x += mario.speed;
            }
            // Jumping
            if (keys["ArrowUp"] && !mario.isJumping) {
                mario.isJumping = true;
                mario.yVelocity = -mario.jumpPower;
            }
            // Gravity
            if (mario.y < floor.y - mario.height || mario.isJumping) {
                mario.yVelocity += 0.5;
                mario.y += mario.yVelocity;
            }
            // Ground collision
            if (mario.y >= floor.y - mario.height) {
                mario.isJumping = false;
                mario.y = floor.y - mario.height;
                mario.yVelocity = 0;
            }
            // Check for collision with blocks
            blocks.forEach((block) => {
                if (
                    mario.y + mario.height >= block[1] &&          // Mario's bottom position is at or below block's top position
                    mario.y < block[1] + blockHeight &&            // Mario's top position is above block's bottom position
                    mario.x + mario.width > block[0] &&             // Mario's right side is to the right of block's left side
                    mario.x < block[0] + blockWidth                // Mario's left side is to the left of block's right side
                ) {
                    mario.isJumping = false;
                    mario.yVelocity = 0;                          // Reset Mario's vertical velocity
                    // Check collision direction
                    const topCollision = mario.y + mario.height - block[1];
                    const bottomCollision = block[1] + blockHeight - mario.y;
                    const leftCollision = mario.x + mario.width - block[0];
                    const rightCollision = block[0] + blockWidth - mario.x;
                    // Handle collision direction
                    if (
                        topCollision <= bottomCollision &&
                        topCollision <= leftCollision &&
                        topCollision <= rightCollision
                    ) {
                        mario.y = block[1] - mario.height;  // Set Mario's position to be on top of the block
                    } else if (
                        bottomCollision <= topCollision &&
                        bottomCollision <= leftCollision &&
                        bottomCollision <= rightCollision
                    ) {
                        mario.y = block[1] + blockHeight;
                        mario.yVelocity = 0;
                        mario.isJumping = true;  // Set Mario's position to be below the block
                    } else if (
                        leftCollision <= topCollision &&
                        leftCollision <= bottomCollision &&
                        leftCollision <= rightCollision
                    ) {
                        mario.x = block[0] - mario.width;   // Set Mario's position to the left of the block
                    } else if (
                        rightCollision <= topCollision &&
                        rightCollision <= bottomCollision &&
                        rightCollision <= leftCollision
                    ) {
                        mario.x = block[0] + blockWidth;   // Set Mario's position to the right of the block
                    }
                }
            });

        }
        function draw() {
            context.clearRect(0, 0, canvas.width, canvas.height);
            // Draw floor
            context.fillStyle = "gray";
            context.fillRect(floor.x, floor.y, floor.width, floor.height);
            // Draw blocks
            context.fillStyle = "brown";
            blocks.forEach((block) => {
                context.fillRect(block[0], block[1], blockWidth, blockHeight);
            });
            // Draw Mario
            context.fillStyle = "blue";
            context.fillRect(mario.x, mario.y, mario.width, mario.height);
            requestAnimationFrame(draw);
        }
        function gameLoop() {
            update();
            draw();
            requestAnimationFrame(gameLoop);
        }
        gameLoop();
    </script>
</body>

</html>
``
增加碰到地面会死 每秒随机生成方块 方块会下坠 马里奥的出生点设为屏幕顶端 并且刚开始随机生成10个方块 请给出完整代码!DOCTYPE htmlhtmlhead style canvas border 1px solid black; display block; margin 0 auto;

原文地址: https://www.cveoy.top/t/topic/iK5x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录