UXInteract / task_041 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
9.56 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gold Miner</title>
<style>
body { font-family: system-ui, sans-serif; background: #3b2410; color: #ffe8b0; text-align: center; margin: 0; padding: 12px; }
h1 { color: #ffcc44; }
#hud { font-size: 16px; margin: 6px; }
#game { background: #6b4a23; border: 3px solid #ffcc44; display: block; margin: 10px auto; max-width:100%;height:auto; }
.menu, .controls { margin: 10px; }
button { padding: 8px 16px; font-size: 15px; border: none; border-radius: 6px;
background: #ffcc44; color: #3b2410; cursor: pointer; margin: 0 4px; }
#tip { color: #d9b98a; }
</style>
</head>
<body>
<h1>Gold Miner</h1>
<div class="menu" data-testid="mode-menu">
<label for="mode">Game mode</label>
<select id="mode" data-testid="game-mode">
<option value="single">Single player</option>
<option value="double">Two players</option>
</select>
<button id="start" data-testid="start-game">Start</button>
</div>
<section id="level" hidden>
<div id="hud">
Player A Score: <span id="scoreA">0</span> ·
Player B Score: <span id="scoreB">0</span> ·
Target: <span id="targetScore">800</span>
</div>
<canvas id="game" width="640" height="380"></canvas>
<div class="controls">
<button id="dropA">Player A Drop Hook (S)</button>
<button id="dropB">Player B Drop Hook (↓)</button>
<button id="reset">Reset</button>
</div>
<p id="tip"></p>
</section>
<script>
const level = document.getElementById('level');
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const TARGET = 800; // reach 800 score to win
let mode = 'single'; // single / double
let scores = { A: 0, B: 0 };
let items = [];
let hooks = [];
let testMode = false;
let explosions = 0;
let effects = [];
let testTarget = TARGET;
// item types with value, weight and how fast the hook reels them in (speed)
const TYPES = {
diamond: { value: 800, weight: 1, color: '#7ad7ff', r: 8 }, // diamond: lightest, most valuable
pig: { value: 3, weight: 1, color: '#ff9bd0', r: 12, moves: true }, // pig runs sideways, slow
bomb: { value: 2, weight: 2, color: '#222', r: 12 }, // bomb: explodes
goldS: { value: 50, weight: 3, color: '#ffd000', r: 10 }, // small gold
goldL: { value: 200, weight: 6, color: '#ffb000', r: 18 }, // large gold (heavier, slower)
stone: { value: 8, weight: 9, color: '#888', r: 16 }, // stone / rock: heaviest
};
function makeItems() {
items = [
// at least 3 diamonds
{ type: 'diamond', x: 80, y: 250 },
{ type: 'diamond', x: 300, y: 320 },
{ type: 'diamond', x: 540, y: 270 },
{ type: 'diamond', x: 430, y: 340 },
{ type: 'pig', x: 200, y: 300, dir: 1 },
{ type: 'bomb', x: 360, y: 230 },
{ type: 'goldS', x: 130, y: 330 },
{ type: 'goldL', x: 480, y: 320 },
{ type: 'stone', x: 250, y: 360 },
{ type: 'stone', x: 580, y: 350, def: {r:22, weight:12, value:15} },
].map(it => ({ ...it, def: {...TYPES[it.type], ...it.def}, grabbed: false }));
}
function makeHooks() {
hooks = [
{ player: 'A', baseX: mode === 'double' ? 200 : 320, baseY: 40,
angle: 0, dir: 1, state: 'swing', len: 0, holding: null },
];
if (mode === 'double') {
hooks.push({ player: 'B', baseX: 440, baseY: 40,
angle: 0, dir: 1, state: 'swing', len: 0, holding: null });
}
}
function startGame(m) {
mode = m;
scores = { A: 0, B: 0 };
effects = [];
explosions = 0;
testTarget = TARGET;
document.getElementById('tip').textContent = 'Player A uses S and Player B uses ArrowDown to control when the hook drops.';
makeItems();
makeHooks();
level.hidden = false;
document.getElementById('dropB').hidden = mode !== 'double';
updateHud();
draw();
}
function drop(player) {
const hook = hooks.find(h => h.player === player);
if (hook && hook.state === 'swing') hook.state = 'extend';
}
function explode(x, y) {
effects.push({x, y, frames: 16});
}
function tipEaster(player) {
// victory / win easter egg shown when a player reaches the target
const tip = document.getElementById('tip');
tip.textContent = '🎉🏆 Player ' + player + ' wins! Victory Easter egg unlocked! 🎉';
}
function updateHud() {
document.getElementById('scoreA').textContent = scores.A;
document.getElementById('scoreB').textContent = scores.B;
document.getElementById('targetScore').textContent = testTarget;
}
function update() {
effects = effects.map(effect => ({...effect, frames: effect.frames-1})).filter(effect => effect.frames > 0);
for (const item of items) {
if (item.def.moves && !item.grabbed) {
item.x += item.dir * 0.6; // pig moves slowly sideways
if (item.x < 40 || item.x > canvas.width - 40) item.dir *= -1;
}
}
for (const hook of hooks) {
const tipX = hook.baseX + Math.sin(hook.angle) * (40 + hook.len);
const tipY = hook.baseY + Math.cos(hook.angle) * (40 + hook.len);
if (hook.state === 'swing') {
hook.angle += hook.dir * 0.03;
if (hook.angle > 1.1) hook.dir = -1;
if (hook.angle < -1.1) hook.dir = 1;
} else if (hook.state === 'extend') {
hook.len += 5;
if (!hook.holding) {
for (const item of items) {
if (item.grabbed) continue;
if (Math.hypot(item.x - tipX, item.y - tipY) < item.def.r + 6) {
item.grabbed = true;
hook.holding = item;
if (item.type === 'bomb') { explode(tipX, tipY); explosions++; hook.fastReturn = true; }
hook.state = 'reel';
break;
}
}
}
// reached screen edge without grabbing -> return back
if (tipX < 0 || tipX > canvas.width || tipY > canvas.height) hook.state = 'reel';
} else if (hook.state === 'reel') {
const speed = hook.fastReturn ? 30 : hook.holding ? Math.max(2, 12 - hook.holding.def.weight) : 12; // heavier = slower; bombs return fast
hook.len -= speed;
if (hook.holding) { hook.holding.x = tipX; hook.holding.y = tipY; }
if (hook.len <= 0) {
hook.len = 0;
if (hook.holding) {
scores[hook.player] += hook.holding.def.value;
items = items.filter(i => i !== hook.holding);
hook.holding = null;
hook.fastReturn = false;
updateHud();
if (scores[hook.player] >= (typeof testTarget === 'number' ? testTarget : TARGET)) tipEaster(hook.player);
}
hook.state = 'swing';
}
}
}
}
function draw() {
ctx.fillStyle = '#6b4a23';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#c79a5b';
ctx.fillRect(0, 0, canvas.width, 60); // surface where hooks hang
for (const item of items) {
ctx.fillStyle = item.def.color;
ctx.beginPath();
ctx.arc(item.x, item.y, item.def.r, 0, Math.PI * 2);
ctx.fill();
}
for (const hook of hooks) {
const tipX = hook.baseX + Math.sin(hook.angle) * (40 + hook.len);
const tipY = hook.baseY + Math.cos(hook.angle) * (40 + hook.len);
ctx.strokeStyle = '#eee';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(hook.baseX, hook.baseY);
ctx.lineTo(tipX, tipY);
ctx.stroke();
ctx.fillStyle = '#ddd';
ctx.fillRect(tipX - 5, tipY - 5, 10, 10);
}
for (const effect of effects) {
ctx.fillStyle = 'rgba(255,80,0,0.8)';
ctx.beginPath();
ctx.arc(effect.x, effect.y, 36 * effect.frames / 16, 0, Math.PI * 2);
ctx.fill();
}
}
function loop() {
if (!level.hidden) {
if (!testMode) update();
draw();
}
requestAnimationFrame(loop);
}
document.addEventListener('keydown', (e) => {
if (level.hidden) return;
if (e.key === 's' || e.key === 'S') drop('A');
if (e.key === 'ArrowDown') { e.preventDefault(); drop('B'); }
});
document.getElementById('start').addEventListener('click', () => {
testMode = false;
startGame(document.getElementById('mode').value);
});
document.getElementById('dropA').addEventListener('click', () => drop('A'));
document.getElementById('dropB').addEventListener('click', () => drop('B'));
document.getElementById('reset').addEventListener('click', () => startGame(mode));
requestAnimationFrame(loop);
window.gameTest = {
reset(config={}) {
testMode=true; explosions=0; startGame(config.mode||'single'); testTarget=config.targetScore||TARGET;
if(config.items) items=config.items.map(it=>({dir:1,...it,def:{...(TYPES[it.type]||{}),...(it.def||{})},grabbed:false}));
updateHud(); draw();
return this.state();
},
step(ms) { for(let i=0;i<Math.max(1,Math.round(ms/16));i++) update(); draw(); return this.state(); },
dropClaw(player='A') { drop(player); return this.state(); },
state() { return { scores:{...scores}, target:testTarget, won:Object.values(scores).some(x=>x>=testTarget), explosions, items:items.map(i=>({type:i.type,x:i.x,y:i.y,value:i.def.value,weight:i.def.weight,grabbed:i.grabbed})), hooks:hooks.map(h=>({player:h.player,state:h.state,len:h.len,holding:h.holding?.type||null,fastReturn:!!h.fastReturn})) }; }
};
</script>
</body>
</html>