<!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: Math.random() * canvas.width,   // 随机生成Mario的x位置
            y: 0,   // Mario的出生点设为屏幕顶端
            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 = [];
        for (let i = 0; i < 10; i++) {
            const blockX = Math.random() * (canvas.width - blockWidth);   // 随机生成方块的x位置
            const blockY = Math.random() * (canvas.height - blockHeight);   // 随机生成方块的y位置
            blocks.push([blockX, blockY]);   // 将方块的位置加入blocks数组
        }
        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
                    }
                }
            });
<pre><code>    }
    function draw() {
        context.clearRect(0, 0, canvas.width, canvas.height);
        // Draw floor
        context.fillStyle = &quot;gray&quot;;
        context.fillRect(floor.x, floor.y, floor.width, floor.height);
        // Draw blocks
        context.fillStyle = &quot;brown&quot;;
        blocks.forEach((block) =&gt; {
            context.fillRect(block[0], block[1], blockWidth, blockHeight);
        });
        // Draw Mario
        context.fillStyle = &quot;blue&quot;;
        context.fillRect(mario.x, mario.y, mario.width, mario.height);
        requestAnimationFrame(draw);
    }
    function gameLoop() {
        update();
        draw();
        requestAnimationFrame(gameLoop);
    }
    gameLoop();
&lt;/script&gt;
</code></pre>
</body>
</html
请不要联系以前的代码!增加碰到地面会死 每秒随机生成方块 方块会下坠 马里奥的出生点设为屏幕顶端 并且刚开始随机生成10个方块 请给出完整代码!DOCTYPE htmlhtmlhead style canvas border 1px solid black; display block; margin 0 au

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

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