UXInteract / task_005 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
6.37 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dark Tetris</title>
<style>
body { font-family: system-ui, sans-serif; background: #050505; color: #e0e0e0; text-align: center; margin: 0; padding: 16px; }
h1 { color: #bb86fc; text-shadow: 0 0 8px #bb86fc; }
#hud { margin: 8px; font-size: 16px; }
#tetris { background: #0d0d0d; border: 2px solid #bb86fc; display: block; margin: 12px auto;
box-shadow: 0 0 20px rgba(187,134,252,0.5); }
p { color: #888; }
button { padding: 8px 16px; font-size: 15px; border: none; border-radius: 6px;
background: #bb86fc; color: #050505; cursor: pointer; }
</style>
</head>
<body>
<h1>Dark Tetris</h1>
<div id="hud">Score: <span id="score">0</span> · Lines: <span id="lines">0</span></div>
<canvas id="tetris" width="220" height="484"></canvas>
<p>← → Move, ↑ Rotate, ↓ Soft drop (dark theme)</p>
<button id="restart">Restart</button>
<script>
const COLS = 10, ROWS = 22, CELL = 22; // dark-themed tetris grid
const canvas = document.getElementById('tetris');
const ctx = canvas.getContext('2d');
const scoreEl = document.getElementById('score');
const linesEl = document.getElementById('lines');
const SHAPES = {
I: [[1, 1, 1, 1]],
O: [[1, 1], [1, 1]],
T: [[0, 1, 0], [1, 1, 1]],
L: [[1, 0], [1, 0], [1, 1]],
J: [[0, 1], [0, 1], [1, 1]],
S: [[0, 1, 1], [1, 1, 0]],
Z: [[1, 1, 0], [0, 1, 1]],
};
const COLORS = ['#bb86fc', '#03dac6', '#cf6679', '#ffb74d', '#4fc3f7', '#aed581', '#ff8a65'];
const KEYS = Object.keys(SHAPES);
let grid, piece, score, lines, gameOver, stepRemainder;
let deterministicMode = false;
function emptyGrid() {
return Array.from({ length: ROWS }, () => Array(COLS).fill(0));
}
function newPiece() {
const k = KEYS[Math.floor(Math.random() * KEYS.length)];
const shape = SHAPES[k];
return {
shape,
color: COLORS[KEYS.indexOf(k)],
x: Math.floor((COLS - shape[0].length) / 2),
y: 0,
};
}
// rotate the current block clockwise
function rotate(shape) {
return shape[0].map((_, i) => shape.map(row => row[i]).reverse());
}
function collides(shape, ox, oy) {
for (let r = 0; r < shape.length; r++) {
for (let c = 0; c < shape[r].length; c++) {
if (!shape[r][c]) continue;
const nx = ox + c, ny = oy + r;
if (nx < 0 || nx >= COLS || ny >= ROWS) return true;
if (ny >= 0 && grid[ny][nx]) return true;
}
}
return false;
}
function merge() {
piece.shape.forEach((row, r) => row.forEach((v, c) => {
if (v && piece.y + r >= 0) grid[piece.y + r][piece.x + c] = piece.color;
}));
}
function clearLines() {
let cleared = 0;
for (let r = ROWS - 1; r >= 0; r--) {
if (grid[r].every(v => v)) {
grid.splice(r, 1);
grid.unshift(Array(COLS).fill(0));
cleared++;
r++;
}
}
if (cleared) {
lines += cleared;
score += [0, 100, 300, 500, 800][cleared];
scoreEl.textContent = score;
linesEl.textContent = lines;
}
}
function drop() {
if (gameOver) return;
if (!collides(piece.shape, piece.x, piece.y + 1)) {
piece.y++;
} else {
merge();
clearLines();
piece = newPiece();
if (collides(piece.shape, piece.x, piece.y)) gameOver = true;
}
}
function draw() {
ctx.fillStyle = '#0d0d0d';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
if (grid[r][c]) drawCell(c, r, typeof grid[r][c] === 'string' ? grid[r][c] : '#bb86fc');
}
}
if (piece) {
piece.shape.forEach((row, r) => row.forEach((v, c) => {
if (v) drawCell(piece.x + c, piece.y + r, piece.color);
}));
}
if (gameOver) {
ctx.fillStyle = '#fff';
ctx.font = '22px sans-serif';
ctx.fillText('Game Over', 70, canvas.height / 2);
}
}
function drawCell(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * CELL + 1, y * CELL + 1, CELL - 2, CELL - 2);
}
document.addEventListener('keydown', (e) => {
if (gameOver) return;
if (e.key === 'ArrowLeft' && !collides(piece.shape, piece.x - 1, piece.y)) piece.x--;
else if (e.key === 'ArrowRight' && !collides(piece.shape, piece.x + 1, piece.y)) piece.x++;
else if (e.key === 'ArrowDown') drop();
else if (e.key === 'ArrowUp') {
const r = rotate(piece.shape);
if (!collides(r, piece.x, piece.y)) piece.shape = r;
}
draw();
});
function loop(time) {
if (!gameOver && !deterministicMode) {
if (!loop.last) loop.last = time;
if (time - loop.last > 500) { drop(); loop.last = time; }
draw();
}
requestAnimationFrame(loop);
}
function start(deterministic = false) {
deterministicMode = deterministic;
grid = emptyGrid();
piece = newPiece();
score = 0; lines = 0; gameOver = false;
stepRemainder = 0;
scoreEl.textContent = '0';
linesEl.textContent = '0';
loop.last = 0;
draw();
}
document.getElementById('restart').addEventListener('click', () => start(false));
start(false);
requestAnimationFrame(loop);
window.gameTest = {
reset() { start(true); return this.state(); },
setBoard(rows) { deterministicMode=true; if(rows.length!==ROWS||rows.some(r=>r.length!==COLS)) throw new Error('invalid board'); grid=rows.map(r=>[...r]); stepRemainder=0; draw(); },
setPiece(next) { deterministicMode=true; piece={shape:next.shape.map(r=>[...r]),x:next.x,y:next.y,color:next.color||'#fff'}; stepRemainder=0; draw(); },
move(dx) { if(!gameOver&&!collides(piece.shape,piece.x+dx,piece.y)) piece.x+=dx; draw(); return this.state(); },
rotate() { const r=rotate(piece.shape); if(!gameOver&&!collides(r,piece.x,piece.y)) piece.shape=r; draw(); return this.state(); },
drop() { drop(); draw(); return this.state(); },
step(ms) { if(Number.isFinite(ms)&&ms>0){stepRemainder+=ms;while(stepRemainder>=500&&!gameOver){drop();stepRemainder-=500;}} draw(); return this.state(); },
state() { return { rows:ROWS, cols:COLS, board:grid.map(r=>[...r]), piece:{...piece,shape:piece.shape.map(r=>[...r])}, score, lines, gameOver }; }
};
</script>
</body>
</html>