Tetris.html(1)
Tetris.html(1)
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width = device-width, initial-scale = 1" />
<title>Tetris</title>
<style>
body {
text-align: center;
background: #fff;
}
#my-canvas {
background: #fff;
margin-left: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<div>
<button onclick="state.playing = !state.playing">시작 / 정지</button>
</div>
<canvas id="my-canvas"></canvas>
<script>
const delay = (n) => new Promise((r) => setTimeout(r, n));
const createGrid = (width, height) =>
[...Array(height)].map(() => Array(width).fill(0));
const state = {
score: 0,
level: 1,
time: 0,
playing: true,
pos: { x: 4, y: 0 },
oldCoords: null,
newCoords: null,
over: false,
lock: false,
winOrLose: "진행 중",
};
const tetrominos = [
[
[0, 1, 0],
[1, 1, 1],
[0, 0, 0],
], // T
[
[2, 0, 0],
[2, 2, 2],
[0, 0, 0],
], // L
[
[0, 0, 3],
[3, 3, 3],
[0, 0, 0],
], // J
[
[4, 4, 0],
[4, 4, 0],
[0, 0, 0],
], // O
[
[0, 5, 5],
[5, 5, 0],
[0, 0, 0],
], // S
[
[6, 6, 0],
[0, 6, 6],
[0, 0, 0],
], // Z
[
[0, 0, 0, 0],
[7, 7, 7, 7],
[0, 0, 0, 0],
], // I
];
const tetrominoColors = [
"white",
"purple",
"cyan",
"blue",
"yellow",
"orange",
"green",
"red",
];
let curTetromino;
let curTetrominoColor;
let nextTetromino;
let nextTetrominoColor;
class LocalStorageManager {
constructor() {
this.bestScoreKey = "tetrisBestScore";
this.storage = window.localStorage;
}
getBestScore() {
return this.storage.getItem(this.bestScoreKey) || 0;
}
setBestScore(score) {
this.storage.setItem(this.bestScoreKey, score);
}
setScore(score) {
this.setBestScore(Math.max(score, this.getBestScore()));
}
}
function setupCanvas() {
ctx.scale(2, 2);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "black";
ctx.strokeRect(8, 8, 280, 462);
// Next Tetromino //
ctx.strokeRect(300, 8, 161, 70);
ctx.fillStyle = "black";
ctx.font = "21px Arial";
createTetromino();
drawTetromino();
renderWell();
}
function updateScore() {
ctx.font = "21px Arial";
ctx.fillStyle = "white";
ctx.fillRect(301, 166, 159, 22);
ctx.fillStyle = "black";
ctx.fillText(state.score, 310, 185);
storage.setScore(state.score);
ctx.fillStyle = "white";
ctx.fillRect(301, 226, 159, 22);
ctx.fillStyle = "black";
ctx.fillText(storage.getBestScore(), 310, 245);
function drawTetromino() {
const coords = setCoords(curTetromino, state.pos);
coords.forEach(
({ x, y, z }) => z && drawTile(x, y, tetrominoColors[z])
);
}
function deleteTetromino() {
const coords = setCoords(curTetromino, state.pos);
coords.forEach(({ x, y, z }) => z && drawTile(x, y, "white"));
}
function drawNextTetromino() {
ctx.fillStyle = "white";
ctx.fillRect(301, 9, 158, 68);
function createTetromino() {
const randomTetrimino = Math.floor(Math.random() * tetrominos.length);
const randomNextTetrimino = Math.floor(
Math.random() * tetrominos.length
);
if (!nextTetromino) {
nextTetromino = tetrominos[randomTetrimino];
nextTetrominoColor = tetrominoColors[randomTetrimino];
}
curTetromino = nextTetromino;
curTetrominoColor = nextTetrominoColor;
nextTetromino = tetrominos[randomNextTetrimino];
nextTetrominoColor = tetrominoColors[randomNextTetrimino];
drawNextTetromino();
}
return true;
};
requestAnimationFrame(freeFall);
};
setupCanvas();
requestAnimationFrame(freeFall);
</script>
</body>
</html>