Spaces:
Running
Running
File size: 11,333 Bytes
4ff2cc7 b440dae 4ff2cc7 b440dae 4ff2cc7 b440dae 4ff2cc7 b440dae 4ff2cc7 b440dae 4ff2cc7 b440dae 4ff2cc7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tetris Game by MOUSE(VIDraft-mouse1.hf.space)</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #1a1a1a;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
font-family: Arial, sans-serif;
padding: 20px;
}
.title {
color: #00f0f0;
font-size: 2.5rem;
margin-bottom: 2rem;
text-align: center;
text-shadow: 3px 3px 6px rgba(0,0,0,0.5);
}
.game-container {
display: flex;
gap: 20px;
}
#game-board {
border: 2px solid #333;
background: #000;
}
.side-panel {
display: flex;
flex-direction: column;
gap: 20px;
color: white;
}
.next-piece {
width: 150px;
height: 150px;
border: 2px solid #333;
background: #000;
}
.score-board {
padding: 10px;
background: #333;
border-radius: 5px;
}
.controls {
background: #333;
padding: 10px;
border-radius: 5px;
}
.controls p {
margin: 5px 0;
font-size: 14px;
}
.title strong {
color: #f0a000;
}
</style>
</head>
<body>
<h1 class="title">Tetris Game <strong>by MOUSE(VIDraft-mouse1.hf.space)</strong></h1>
<div class="game-container">
<canvas id="game-board" width="300" height="600"></canvas>
<div class="side-panel">
<div class="next-piece">
<canvas id="next-piece-canvas" width="150" height="150"></canvas>
</div>
<div class="score-board">
<h3>Score: <span id="score">0</span></h3>
<h3>Lines: <span id="lines">0</span></h3>
<h3>Level: <span id="level">1</span></h3>
</div>
<div class="controls">
<h3>Controls:</h3>
<p>β β : Move Left/Right</p>
<p>β : Soft Drop</p>
<p>Space : Hard Drop</p>
<p>β : Rotate</p>
</div>
</div>
</div>
<!-- μ΄ν script λΆλΆμ λμΌνλ―λ‘ μλ΅νμ§ μκ³ κ·Έλλ‘ ν¬ν¨ -->
<script>
const canvas = document.getElementById('game-board');
const ctx = canvas.getContext('2d');
const nextPieceCanvas = document.getElementById('next-piece-canvas');
const nextPieceCtx = nextPieceCanvas.getContext('2d');
const BLOCK_SIZE = 30;
const BOARD_WIDTH = 10;
const BOARD_HEIGHT = 20;
let board = Array(BOARD_HEIGHT).fill().map(() => Array(BOARD_WIDTH).fill(0));
let score = 0;
let lines = 0;
let level = 1;
let gameLoop;
let currentPiece;
let nextPiece;
const SHAPES = [
[[1, 1, 1, 1]], // I
[[1, 1], [1, 1]], // O
[[0, 1, 1], [1, 1, 0]], // S
[[1, 1, 0], [0, 1, 1]], // Z
[[1, 0, 0], [1, 1, 1]], // L
[[0, 0, 1], [1, 1, 1]], // J
[[0, 1, 0], [1, 1, 1]] // T
];
const COLORS = [
'#00f0f0', // cyan
'#f0f000', // yellow
'#00f000', // green
'#f00000', // red
'#f0a000', // orange
'#0000f0', // blue
'#a000f0' // purple
];
class Piece {
constructor(shape = null) {
this.shape = shape || SHAPES[Math.floor(Math.random() * SHAPES.length)];
this.color = COLORS[SHAPES.findIndex(s => s === this.shape)];
this.x = Math.floor((BOARD_WIDTH - this.shape[0].length) / 2);
this.y = 0;
}
}
function drawBlock(ctx, x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1);
}
function drawBoard() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw board
for(let y = 0; y < BOARD_HEIGHT; y++) {
for(let x = 0; x < BOARD_WIDTH; x++) {
if(board[y][x]) {
drawBlock(ctx, x, y, board[y][x]);
}
}
}
// Draw current piece
if(currentPiece) {
for(let y = 0; y < currentPiece.shape.length; y++) {
for(let x = 0; x < currentPiece.shape[y].length; x++) {
if(currentPiece.shape[y][x]) {
drawBlock(ctx, currentPiece.x + x, currentPiece.y + y, currentPiece.color);
}
}
}
}
}
function drawNextPiece() {
nextPieceCtx.clearRect(0, 0, nextPieceCanvas.width, nextPieceCanvas.height);
const offsetX = (nextPieceCanvas.width - nextPiece.shape[0].length * BLOCK_SIZE) / 2;
const offsetY = (nextPieceCanvas.height - nextPiece.shape.length * BLOCK_SIZE) / 2;
for(let y = 0; y < nextPiece.shape.length; y++) {
for(let x = 0; x < nextPiece.shape[y].length; x++) {
if(nextPiece.shape[y][x]) {
nextPieceCtx.fillStyle = nextPiece.color;
nextPieceCtx.fillRect(
offsetX + x * BLOCK_SIZE,
offsetY + y * BLOCK_SIZE,
BLOCK_SIZE - 1,
BLOCK_SIZE - 1
);
}
}
}
}
function isValidMove(piece, offsetX, offsetY, newShape = null) {
const shape = newShape || piece.shape;
for(let y = 0; y < shape.length; y++) {
for(let x = 0; x < shape[y].length; x++) {
if(shape[y][x]) {
const newX = piece.x + x + offsetX;
const newY = piece.y + y + offsetY;
if(newX < 0 || newX >= BOARD_WIDTH || newY >= BOARD_HEIGHT) {
return false;
}
if(newY >= 0 && board[newY][newX]) {
return false;
}
}
}
}
return true;
}
function rotatePiece() {
const newShape = currentPiece.shape[0].map((_, i) =>
currentPiece.shape.map(row => row[i]).reverse()
);
if(isValidMove(currentPiece, 0, 0, newShape)) {
currentPiece.shape = newShape;
}
}
function mergePiece() {
for(let y = 0; y < currentPiece.shape.length; y++) {
for(let x = 0; x < currentPiece.shape[y].length; x++) {
if(currentPiece.shape[y][x]) {
board[currentPiece.y + y][currentPiece.x + x] = currentPiece.color;
}
}
}
}
function checkLines() {
let linesCleared = 0;
for(let y = BOARD_HEIGHT - 1; y >= 0; y--) {
if(board[y].every(cell => cell !== 0)) {
board.splice(y, 1);
board.unshift(Array(BOARD_WIDTH).fill(0));
linesCleared++;
y++;
}
}
if(linesCleared > 0) {
lines += linesCleared;
score += linesCleared * 100 * level;
level = Math.floor(lines / 10) + 1;
document.getElementById('score').textContent = score;
document.getElementById('lines').textContent = lines;
document.getElementById('level').textContent = level;
}
}
function gameOver() {
clearInterval(gameLoop);
alert(`Game Over! Score: ${score}`);
resetGame();
}
function resetGame() {
board = Array(BOARD_HEIGHT).fill().map(() => Array(BOARD_WIDTH).fill(0));
score = 0;
lines = 0;
level = 1;
document.getElementById('score').textContent = score;
document.getElementById('lines').textContent = lines;
document.getElementById('level').textContent = level;
startGame();
}
function update() {
if(isValidMove(currentPiece, 0, 1)) {
currentPiece.y++;
} else {
mergePiece();
checkLines();
currentPiece = nextPiece;
nextPiece = new Piece();
drawNextPiece();
if(!isValidMove(currentPiece, 0, 0)) {
gameOver();
}
}
drawBoard();
}
function startGame() {
currentPiece = new Piece();
nextPiece = new Piece();
drawNextPiece();
if(gameLoop) clearInterval(gameLoop);
gameLoop = setInterval(() => {
update();
}, 1000 / level);
}
document.addEventListener('keydown', (e) => {
switch(e.keyCode) {
case 37: // Left
if(isValidMove(currentPiece, -1, 0)) {
currentPiece.x--;
drawBoard();
}
break;
case 39: // Right
if(isValidMove(currentPiece, 1, 0)) {
currentPiece.x++;
drawBoard();
}
break;
case 40: // Down
if(isValidMove(currentPiece, 0, 1)) {
currentPiece.y++;
drawBoard();
}
break;
case 38: // Up (Rotate)
rotatePiece();
drawBoard();
break;
case 32: // Space (Hard Drop)
while(isValidMove(currentPiece, 0, 1)) {
currentPiece.y++;
}
update();
break;
}
});
startGame();
</script>
</body>
</html> |