建站难度 · 进阶#游戏#网页#交互
像素风网页小游戏
单文件 HTML 生成可玩的像素小游戏:操作、计分、难度曲线与移动端按键。
难度 进阶:需要准备素材或背景,产出要自己筛一遍才能用
适用工具ClaudeGeminiChatGPT
请生成一个单文件像素风网页游戏(index.html,Canvas 绘制,像素画全部用代码画,不用外部图片)。
游戏类型:【如 跳跃闯关 / 打砖块 / 贪吃蛇变体】。
要求:
1. 核心玩法一句话可说清,10 秒内能上手;有明确的失败条件与重新开始。
2. 难度曲线:随得分提高速度或密度,封顶值写死,避免后期不可玩。
3. 操作:键盘(方向键 / WASD)+ 移动端触控按钮(屏幕底部虚拟按键),两者都要可用。
4. 视觉:限定调色板(给出具体色值 5~8 色),像素颗粒感用 image-rendering: pixelated 保证;标题画面 3 秒无操作自动进入演示模式。
5. 音效可选:用 WebAudio 合成简单音效,提供静音开关。
6. 计分与最高分(localStorage 保存),死亡时显示本局得分与最高分。
输出完整可玩代码。文中【】为可替换变量,使用前请替换成你的具体内容。
变量填充:填一填直接用
已识别 1 个【】占位变量,填写后一键生成成品提示词;留空的变量所在行会自动删除,不会留下占位符。
示例输出
下面是按这条提示词填完后的实际产出,可直接对照判断该填成什么样。
产出的样子
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>像素跳跳球</title>
<style>
html,body { margin:0; height:100%; background:#1a1c2c; display:flex;
align-items:center; justify-content:center; font-family:monospace;
touch-action:manipulation; user-select:none; }
#wrap { position:relative; }
canvas { image-rendering:pixelated; display:block; }
#pad { position:fixed; bottom:18px; left:0; right:0; display:flex;
justify-content:center; gap:18px; }
.key { width:64px; height:64px; background:#29366f; color:#f4f4f4;
border:2px solid #4a5aa8; border-radius:10px; font-size:24px;
display:flex; align-items:center; justify-content:center; }
.key:active { background:#4a5aa8; }
@media (hover:hover) { #pad { display:none; } } /* 桌面端隐藏虚拟按键 */
</style>
</head>
<body>
<div id="wrap"><canvas id="cv" width="240" height="320" style="width:min(90vw,360px)"></canvas></div>
<div id="pad"><div class="key" data-k="left">◀</div><div class="key" data-k="jump">▲</div><div class="key" data-k="right">▶</div></div>
<script>
const W = 240, H = 320, C = document.getElementById('cv').getContext('2d');
// 限定调色板(7 色)
const P = { bg:'#1a1c2c', ground:'#333c57', plat:'#5d7ac4', player:'#f4a259',
spike:'#e63e6d', coin:'#ffd76f', text:'#f4f4f4' };
let state = 'title', demo = true, lastT = 0, speed = 1.6, score = 0;
const best = () => +(localStorage.getItem('pp_best') || 0);
let p = { x:60, y:200, vy:0, w:18, h:18, onGround:false };
let plats = [], spikes = [], coins = [];
const keys = {};
addEventListener('keydown', e => { keys[e.key] = 1;
if (e.key === ' ') { demo = false; if (state !== 'play') reset(); } });
addEventListener('keyup', e => keys[e.key] = 0);
document.querySelectorAll('.key').forEach(k => {
const act = v => { keys['touch_' + k.dataset.k] = v; demo = false;
if (k.dataset.k === 'jump' && state !== 'play') reset(); };
k.addEventListener('touchstart', () => act(1));
k.addEventListener('touchend', () => act(0));
});
const leftDown = () => keys.ArrowLeft || keys.a || keys.touch_left;
const rightDown = () => keys.ArrowRight || keys.d || keys.touch_right;
const jumpDown = () => keys.ArrowUp || keys.w || keys[' '] || keys.touch_jump;
function reset() {
state = 'play'; speed = 1.6; score = 0; demo = false;
p = { x:60, y:180, vy:0, w:18, h:18, onGround:false };
plats = []; spikes = []; coins = [];
addRow(300);
}
function addRow(y) {
const gap = Math.random() * (W - 110) + 10;
const wid = Math.max(52, 110 - speed * 12);
plats.push({ x: Math.random() < .5 ? gap : 0, y, w: wid, h: 10 });
if (Math.random() < .45) spikes.push({ x: W + 10, y: y - 10, w: 12, h: 10 });
if (Math.random() < .6) coins.push({ x: W * .5 + Math.random()*60, y: y - 24 });
}
function die() {
state = 'dead';
if (score > best()) localStorage.setItem('pp_best', score);
}
function step() {
if (state === 'title' && demo && performance.now() - lastT > 3000) startDemo();
if (state !== 'play') return;
// 难度曲线:速度随得分提高,封顶 3.2
speed = Math.min(3.2, 1.6 + score * 0.06);
if (leftDown()) p.x -= 1.8; if (rightDown()) p.x += 1.8;
p.x = Math.max(0, Math.min(W - p.w, p.x));
p.vy += 0.32; p.y += p.vy;
p.onGround = false;
for (const pl of plats) {
if (p.x + p.w > pl.x && p.x < pl.x + pl.w &&
p.y + p.h > pl.y && p.y + p.h < pl.y + pl.h + 9 && p.vy > 0) {
p.y = pl.y - p.h; p.vy = 0; p.onGround = true;
}
}
if (jumpDown() && p.onGround) p.vy = -6.4;
// 世界下滚:所有物件上移
for (const a of [...plats, ...spikes, ...coins]) a.y -= speed;
plats = plats.filter(a => a.y > -14);
spikes = spikes.filter(a => a.y > -14);
coins = coins.filter(a => a.y > -14);
if (plats.length && plats[plats.length-1].y < 150) addRow(320);
for (const s of spikes)
if (p.x + p.w > s.x && p.x < s.x + s.w && p.y + p.h > s.y && p.y < s.y + s.h) return die();
for (const c of coins)
if (!c.got && Math.abs(p.x - c.x) < 16 && Math.abs(p.y - c.y) < 16) { c.got = 1; score++; }
if (p.y > H) return die();
}
function draw() {
C.fillStyle = P.bg; C.fillRect(0, 0, W, H);
C.fillStyle = P.plat; plats.forEach(a => C.fillRect(a.x, a.y, a.w, a.h));
C.fillStyle = P.spike; spikes.forEach(a => C.fillRect(a.x, a.y, a.w, a.h));
C.fillStyle = P.coin; coins.forEach(a => { if (!a.got) C.fillRect(a.x, a.y, 10, 10); });
C.fillStyle = P.player; C.fillRect(p.x, p.y, p.w, p.h);
C.fillStyle = P.text; C.font = '8px monospace';
C.fillText('SCORE ' + score, 8, 14);
C.fillText('BEST ' + Math.max(best(), score), 150, 14);
if (state === 'title') {
C.font = '16px monospace'; C.fillText('PIXEL JUMP', 62, 140);
C.font = '8px monospace'; C.fillText('SPACE / TAP TO START', 62, 165);
}
if (state === 'dead') {
C.font = '12px monospace'; C.fillText('GAME OVER', 82, 150);
C.font = '8px monospace';
C.fillText('SCORE ' + score + ' BEST ' + best(), 70, 168);
C.fillText('TAP TO RETRY', 90, 184);
}
}
function startDemo() { // 3 秒无操作自动演示:AI 自动跳
demo = true; reset(); demo = true;
setInterval(() => { if (demo && state === 'play' && p.onGround && Math.random() < .12) p.vy = -6.4; }, 120);
}
let loop = () => { step(); draw(); requestAnimationFrame(loop); };
loop();
</script>
</body>
</html>
```
**实现说明**
- 玩法一句话:左右移动躲避尖刺、收集金币,掉出屏幕底部即失败;10 秒可上手。
- 难度曲线:速度 = 1.6 + 得分 × 0.06,写死封顶 3.2,后期可玩。
- 操作双通道:键盘(方向键/WASD/空格)+ 移动端底部虚拟按键(hover 检测自动隐藏)。
- 调色板 7 色写死,canvas 用 image-rendering: pixelated 保证像素颗粒感;标题 3 秒无操作进入 AI 演示模式。
- 最高分 localStorage 持久化,死亡页同时显示本局与最高分。核心玩法 10 秒上手、难度曲线有写死的封顶值、键盘与触控双通道、限定调色板 + pixelated、3 秒无操作进演示模式、最高分持久化,六条约束逐条对应实现说明。
什么时候会翻车:代码画布逻辑模型一遍写不对的概率不低,尤其碰撞判定和触控事件;WebAudio 音效要用户首次点击后才能响,模型常忽略这个限制。
想一次试多种填法做 A/B 对比?把这条提示词粘进Prompt 变量模板填充,把要换的部分写成 {{变量}},会自动生成表单并批量渲染出多个版本。
使用技巧
- 「像素画用代码画」是成败点,要求外部素材基本都跑不起来。
- 先玩核心循环,好玩再加音效和皮肤,别让模型一次全做。
这条提示词收录在 1 套精选合集里
合集按真实工作流排好了前后顺序,点进合集可以看「它前面该做什么、后面接什么」。
同类提示词
动手做:配套工具
这条提示词是成品,下面几个工具能把它变成你要的那一版。
