跳到正文

zch150612

1793328

张宸珲

在洛谷打开
资料、奖项与版本

资料

编号
1793328
抓取

<html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>暗黑企鹅 无限平台冒险</title> <style> *{margin:0;padding:0} body{background:#222;display:flex;justify-content:center;align-items:center;height:100vh} canvas{border:3px solid #c02222;background:#87ceeb} .tip{position:absolute;top:8px;color:#fff;font-family:sans-serif} </style> </head> <body> <div class="tip">A/D 移动 | 空格 二段跳</div> <canvas id="gameCanvas" width="960" height="640"></canvas> <script> const canvas = document.getElementById("gameCanvas"); const ctx = canvas.getContext("2d");

// 企鹅图片资源 const playerImg = new Image(); playerImg.src = “https://p3-flow-image-sign.byteimg.com/tos-cn-i-a9rns2rl98/30011000929e4048990d402d40240711~tplv-a9rns2rl98-image.image”;

// ========== 游戏配置 ========== const CAMERA = { x: 0 }; const PLAYER = { x: 120, y: 400, w: 72, h: 82, vx: 0, vy: 0, speed: 5.2, gravity: 0.62, jumpPower: -14.5, onGround: false, jumpCount: 0, maxJump: 2, flip: 1 }; // 平台数组 let platforms = []; // 下一个平台生成坐标 let nextPlatformX = 0; // 按键状态 const keys = { left: false, right: false };

// ========== 初始化基础地面 ========== function initPlatforms() { platforms.push({x:0, y:560, w:450, h:80}); nextPlatformX = 450; }

// 随机生成新平台 function spawnPlatform() { // 随机间距、高度、宽度 const gap = 110 + Math.random() * 160; const width = 90 + Math.random() * 130; const y = 220 + Math.random() * 300; const newPlat = { x: nextPlatformX + gap, y: y, w: width, h: 26 }; platforms.push(newPlat); nextPlatformX = newPlat.x + width; }

// 碰撞检测 function isCollide(a, b) { return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y; }

// ========== 键盘监听 ========== window.addEventListener(“keydown”, e=>{ const key = e.key.toLowerCase(); if(key === “a”) keys.left = true; if(key === “d”) keys.right = true; if(e.code === “Space”) { if(PLAYER.jumpCount < PLAYER.maxJump) { PLAYER.vy = PLAYER.jumpPower; PLAYER.jumpCount++; PLAYER.onGround = false; } } }); window.addEventListener(“keyup”, e=>{ const key = e.key.toLowerCase(); if(key === “a”) keys.left = false; if(key === “d”) keys.right = false; });

// ========== 游戏主更新 ========== function update() { // 水平移动 PLAYER.vx = 0; if(keys.left) { PLAYER.vx = -PLAYER.speed; PLAYER.flip = -1; } if(keys.right) { PLAYER.vx = PLAYER.speed; PLAYER.flip = 1; }

代码
// 重力运算
PLAYER.vy += PLAYER.gravity;
PLAYER.x += PLAYER.vx;
PLAYER.y += PLAYER.vy;

PLAYER.onGround = false;
// 平台碰撞逻辑
for(let plat of platforms) {
    const playerBox = {
        x: PLAYER.x,
        y: PLAYER.y,
        w: PLAYER.w,
        h: PLAYER.h
    };
    if(isCollide(playerBox, plat)) {
        // 从上方落下踩平台
        if(PLAYER.vy > 0 && PLAYER.y + PLAYER.h - PLAYER.vy <= plat.y + 12) {
            PLAYER.y = plat.y - PLAYER.h;
            PLAYER.vy = 0;
            PLAYER.onGround = true;
            PLAYER.jumpCount = 0;
        }
    }
}

// 相机跟随玩家(向右滚动)
const targetCamX = PLAYER.x - canvas.width * 0.32;
if(targetCamX > CAMERA.x) {
    CAMERA.x += (targetCamX - CAMERA.x) * 0.08;
}

// 持续生成平台,直到超出可视区域右侧
while(nextPlatformX < CAMERA.x + canvas.width + 300) {
    spawnPlatform();
}
// 清理屏幕左侧很远的平台,释放内存
platforms = platforms.filter(p => p.x + p.w > CAMERA.x - 200);

// 掉落重置(掉出屏幕下方重新出生)
if(PLAYER.y > canvas.height + 100) {
    PLAYER.x = CAMERA.x + 120;
    PLAYER.y = 200;
    PLAYER.vy = 0;
}

render();
requestAnimationFrame(update);

}

// ========== 渲染绘制 ========== function render() { ctx.clearRect(0,0,canvas.width,canvas.height);

代码
ctx.save();
ctx.translate(-CAMERA.x, 0);

// 绘制所有平台
for(let p of platforms) {
    ctx.fillStyle="#547d32";
    ctx.fillRect(p.x, p.y, p.w, p.h);
    ctx.fillStyle="#78b84c";
    ctx.fillRect(p.x, p.y, p.w, 7);
}

// 绘制企鹅主角
ctx.save();
ctx.translate(PLAYER.x + PLAYER.w/2, PLAYER.y + PLAYER.h/2);
ctx.scale(PLAYER.flip, 1);
if(playerImg.complete) {
    ctx.drawImage(playerImg, -PLAYER.w/2, -PLAYER.h/2, PLAYER.w, PLAYER.h);
} else {
    // 图片加载备用色块
    ctx.fillStyle="#222";
    ctx.beginPath();
    ctx.ellipse(0,0,PLAYER.w/2,PLAYER.h/2,0,0,Math.PI*2);
    ctx.fill();
}
ctx.restore();
ctx.restore();

}

initPlatforms(); update();

</body> </html>

还没有收录这位用户的文章。