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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="ko">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>HTML ์ง๋ ์ด ๊ฒ์</title>
|
| 7 |
+
<style>
|
| 8 |
+
/* ๊ธฐ๋ณธ ์คํ์ผ */
|
| 9 |
+
body {
|
| 10 |
+
background-color: #f0f0f0;
|
| 11 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 12 |
+
display: flex;
|
| 13 |
+
justify-content: center;
|
| 14 |
+
align-items: center;
|
| 15 |
+
height: 100vh;
|
| 16 |
+
margin: 0;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
/* ๊ฒ์ ์ปจํ
์ด๋ ์คํ์ผ */
|
| 20 |
+
#game-container {
|
| 21 |
+
background-color: #fff;
|
| 22 |
+
border-radius: 10px;
|
| 23 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 24 |
+
padding: 20px;
|
| 25 |
+
text-align: center;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
h1 {
|
| 29 |
+
color: #333;
|
| 30 |
+
margin-bottom: 10px;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/* ๊ฒ์ ์บ๋ฒ์ค ์คํ์ผ */
|
| 34 |
+
#gameCanvas {
|
| 35 |
+
background-color: #dcedc8; /* ์ฐํ ๋
น์ ๋ฐฐ๊ฒฝ */
|
| 36 |
+
border: 2px solid #333;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/* ์ ์ํ ์คํ์ผ */
|
| 40 |
+
#score-board {
|
| 41 |
+
font-size: 24px;
|
| 42 |
+
font-weight: bold;
|
| 43 |
+
color: #555;
|
| 44 |
+
margin-top: 10px;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
/* ๊ฒ์ ์ค๋ฒ ๋ฉ์์ง ์คํ์ผ */
|
| 48 |
+
#game-over {
|
| 49 |
+
position: absolute;
|
| 50 |
+
top: 50%;
|
| 51 |
+
left: 50%;
|
| 52 |
+
transform: translate(-50%, -50%);
|
| 53 |
+
background-color: rgba(0, 0, 0, 0.7);
|
| 54 |
+
color: white;
|
| 55 |
+
padding: 20px 40px;
|
| 56 |
+
border-radius: 10px;
|
| 57 |
+
display: none; /* ๊ธฐ๋ณธ์ ์ผ๋ก ์จ๊น */
|
| 58 |
+
text-align: center;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
#game-over h2 {
|
| 62 |
+
margin: 0 0 10px 0;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
#restart-button {
|
| 66 |
+
background-color: #4CAF50;
|
| 67 |
+
color: white;
|
| 68 |
+
border: none;
|
| 69 |
+
padding: 10px 20px;
|
| 70 |
+
font-size: 16px;
|
| 71 |
+
border-radius: 5px;
|
| 72 |
+
cursor: pointer;
|
| 73 |
+
transition: background-color 0.3s;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
#restart-button:hover {
|
| 77 |
+
background-color: #45a049;
|
| 78 |
+
}
|
| 79 |
+
</style>
|
| 80 |
+
</head>
|
| 81 |
+
<body>
|
| 82 |
+
|
| 83 |
+
<div id="game-container">
|
| 84 |
+
<h1>์ง๋ ์ด ๊ฒ์</h1>
|
| 85 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
| 86 |
+
<div id="score-board">์ ์: <span id="score">0</span></div>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
<div id="game-over">
|
| 90 |
+
<h2>๊ฒ์ ์ค๋ฒ!</h2>
|
| 91 |
+
<button id="restart-button">๋ค์ ์์</button>
|
| 92 |
+
</div>
|
| 93 |
+
|
| 94 |
+
<script>
|
| 95 |
+
// --- 1. ์ด๊ธฐ ์ค์ ---
|
| 96 |
+
const canvas = document.getElementById('gameCanvas');
|
| 97 |
+
const ctx = canvas.getContext('2d');
|
| 98 |
+
const scoreElement = document.getElementById('score');
|
| 99 |
+
const gameOverElement = document.getElementById('game-over');
|
| 100 |
+
const restartButton = document.getElementById('restart-button');
|
| 101 |
+
|
| 102 |
+
const BOX_SIZE = 20; // ๊ฐ ์นธ์ ํฌ๊ธฐ
|
| 103 |
+
const CANVAS_WIDTH = canvas.width;
|
| 104 |
+
const CANVAS_HEIGHT = canvas.height;
|
| 105 |
+
|
| 106 |
+
let snake; // ์ง๋ ์ด ๋ฐฐ์ด (x, y ์ขํ ๊ฐ์ฒด)
|
| 107 |
+
let food; // ๋จน์ด ๊ฐ์ฒด (x, y ์ขํ)
|
| 108 |
+
let score; // ์ ์
|
| 109 |
+
let direction; // ํ์ฌ ์ด๋ ๋ฐฉํฅ
|
| 110 |
+
let changingDirection; // ๋ฐฉํฅ ์ ํ ์ค์ธ์ง ํ์ธ (์ฐ์์ ์ธ ํค ์
๋ ฅ ๋ฐฉ์ง)
|
| 111 |
+
let gameLoop; // ๊ฒ์ ๋ฃจํ setInterval ID
|
| 112 |
+
|
| 113 |
+
// --- 2. ๊ฒ์ ์์ ๋ฐ ์ฌ์์ ---
|
| 114 |
+
function startGame() {
|
| 115 |
+
// ๊ฒ์ ์ํ ์ด๊ธฐํ
|
| 116 |
+
snake = [
|
| 117 |
+
{ x: 10 * BOX_SIZE, y: 10 * BOX_SIZE },
|
| 118 |
+
{ x: 9 * BOX_SIZE, y: 10 * BOX_SIZE },
|
| 119 |
+
{ x: 8 * BOX_SIZE, y: 10 * BOX_SIZE }
|
| 120 |
+
];
|
| 121 |
+
score = 0;
|
| 122 |
+
direction = 'RIGHT';
|
| 123 |
+
changingDirection = false;
|
| 124 |
+
|
| 125 |
+
// UI ์ด๊ธฐํ
|
| 126 |
+
scoreElement.textContent = score;
|
| 127 |
+
gameOverElement.style.display = 'none';
|
| 128 |
+
|
| 129 |
+
generateFood(); // ์ฒซ ๋จน์ด ์์ฑ
|
| 130 |
+
|
| 131 |
+
// ๊ธฐ์กด ๊ฒ์ ๋ฃจํ๊ฐ ์๋ค๋ฉด ์ค์ง
|
| 132 |
+
if (gameLoop) {
|
| 133 |
+
clearInterval(gameLoop);
|
| 134 |
+
}
|
| 135 |
+
// ์๋ก์ด ๊ฒ์ ๋ฃจํ ์์ (100ms ๋ง๋ค main ํจ์ ์คํ)
|
| 136 |
+
gameLoop = setInterval(main, 100);
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
// --- 3. ๊ฒ์ ๋ฉ์ธ ๋ฃจํ ---
|
| 140 |
+
function main() {
|
| 141 |
+
if (checkGameOver()) {
|
| 142 |
+
clearInterval(gameLoop);
|
| 143 |
+
gameOverElement.style.display = 'block';
|
| 144 |
+
return;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
changingDirection = false;
|
| 148 |
+
clearCanvas();
|
| 149 |
+
drawFood();
|
| 150 |
+
moveSnake();
|
| 151 |
+
drawSnake();
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
// --- 4. ๊ทธ๋ฆฌ๊ธฐ ํจ์๋ค ---
|
| 155 |
+
// ์บ๋ฒ์ค ์ง์ฐ๊ธฐ
|
| 156 |
+
function clearCanvas() {
|
| 157 |
+
ctx.fillStyle = '#dcedc8';
|
| 158 |
+
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// ์ง๋ ์ด ๊ทธ๋ฆฌ๊ธฐ
|
| 162 |
+
function drawSnake() {
|
| 163 |
+
snake.forEach((segment, index) => {
|
| 164 |
+
// ๋จธ๋ฆฌ ์์ ๋ค๋ฅด๊ฒ ํ๊ธฐ
|
| 165 |
+
ctx.fillStyle = (index === 0) ? '#1b5e20' : '#4caf50';
|
| 166 |
+
ctx.strokeStyle = '#388e3c';
|
| 167 |
+
ctx.fillRect(segment.x, segment.y, BOX_SIZE, BOX_SIZE);
|
| 168 |
+
ctx.strokeRect(segment.x, segment.y, BOX_SIZE, BOX_SIZE);
|
| 169 |
+
});
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
// ๋จน์ด ๊ทธ๋ฆฌ๊ธฐ
|
| 173 |
+
function drawFood() {
|
| 174 |
+
ctx.fillStyle = '#d32f2f'; // ๋ถ์์ ๋จน์ด
|
| 175 |
+
ctx.strokeStyle = '#c62828';
|
| 176 |
+
ctx.fillRect(food.x, food.y, BOX_SIZE, BOX_SIZE);
|
| 177 |
+
ctx.strokeRect(food.x, food.y, BOX_SIZE, BOX_SIZE);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
// --- 5. ๊ฒ์ ๋ก์ง ํจ์๋ค ---
|
| 181 |
+
// ๋จน์ด ์์ฑ (์ง๋ ์ด ๋ชธ ์๊ฐ ์๋ ๊ณณ์)
|
| 182 |
+
function generateFood() {
|
| 183 |
+
while (true) {
|
| 184 |
+
const foodX = Math.floor(Math.random() * (CANVAS_WIDTH / BOX_SIZE)) * BOX_SIZE;
|
| 185 |
+
const foodY = Math.floor(Math.random() * (CANVAS_HEIGHT / BOX_SIZE)) * BOX_SIZE;
|
| 186 |
+
|
| 187 |
+
let isFoodOnSnake = snake.some(segment => segment.x === foodX && segment.y === foodY);
|
| 188 |
+
|
| 189 |
+
if (!isFoodOnSnake) {
|
| 190 |
+
food = { x: foodX, y: foodY };
|
| 191 |
+
return;
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// ์ง๋ ์ด ์ด๋
|
| 197 |
+
function moveSnake() {
|
| 198 |
+
let dx = 0;
|
| 199 |
+
let dy = 0;
|
| 200 |
+
|
| 201 |
+
if (direction === 'RIGHT') dx = BOX_SIZE;
|
| 202 |
+
if (direction === 'LEFT') dx = -BOX_SIZE;
|
| 203 |
+
if (direction === 'UP') dy = -BOX_SIZE;
|
| 204 |
+
if (direction === 'DOWN') dy = BOX_SIZE;
|
| 205 |
+
|
| 206 |
+
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
|
| 207 |
+
snake.unshift(head); // ์๋ก์ด ๋จธ๋ฆฌ๋ฅผ ๋ฐฐ์ด ๋งจ ์์ ์ถ๊ฐ
|
| 208 |
+
|
| 209 |
+
// ๋จน์ด๋ฅผ ๋จน์๋์ง ํ์ธ
|
| 210 |
+
if (head.x === food.x && head.y === food.y) {
|
| 211 |
+
score += 10;
|
| 212 |
+
scoreElement.textContent = score;
|
| 213 |
+
generateFood();
|
| 214 |
+
} else {
|
| 215 |
+
snake.pop(); // ๋จน์ด๋ฅผ ๋จน์ง ์์๋ค๋ฉด ๊ผฌ๋ฆฌ ์ ๊ฑฐ
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
// ๊ฒ์ ์ค๋ฒ ์กฐ๊ฑด ํ์ธ
|
| 220 |
+
function checkGameOver() {
|
| 221 |
+
// 1. ๋ฒฝ ์ถฉ๋ ํ์ธ
|
| 222 |
+
const head = snake[0];
|
| 223 |
+
if (head.x < 0 || head.x >= CANVAS_WIDTH || head.y < 0 || head.y >= CANVAS_HEIGHT) {
|
| 224 |
+
return true;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
// 2. ์๊ธฐ ๋ชธ ์ถฉ๋ ํ์ธ
|
| 228 |
+
for (let i = 1; i < snake.length; i++) {
|
| 229 |
+
if (head.x === snake[i].x && head.y === snake[i].y) {
|
| 230 |
+
return true;
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
return false;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
// --- 6. ์ด๋ฒคํธ ๋ฆฌ์ค๋ ---
|
| 237 |
+
// ํค๋ณด๋ ์
๋ ฅ ์ฒ๋ฆฌ
|
| 238 |
+
function changeDirection(event) {
|
| 239 |
+
if (changingDirection) return;
|
| 240 |
+
changingDirection = true;
|
| 241 |
+
|
| 242 |
+
const keyPressed = event.key;
|
| 243 |
+
const goingUp = direction === 'UP';
|
| 244 |
+
const goingDown = direction === 'DOWN';
|
| 245 |
+
const goingRight = direction === 'RIGHT';
|
| 246 |
+
const goingLeft = direction === 'LEFT';
|
| 247 |
+
|
| 248 |
+
if ((keyPressed === 'ArrowLeft' || keyPressed.toLowerCase() === 'a') && !goingRight) {
|
| 249 |
+
direction = 'LEFT';
|
| 250 |
+
}
|
| 251 |
+
if ((keyPressed === 'ArrowUp' || keyPressed.toLowerCase() === 'w') && !goingDown) {
|
| 252 |
+
direction = 'UP';
|
| 253 |
+
}
|
| 254 |
+
if ((keyPressed === 'ArrowRight' || keyPressed.toLowerCase() === 'd') && !goingLeft) {
|
| 255 |
+
direction = 'RIGHT';
|
| 256 |
+
}
|
| 257 |
+
if ((keyPressed === 'ArrowDown' || keyPressed.toLowerCase() === 's') && !goingUp) {
|
| 258 |
+
direction = 'DOWN';
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
document.addEventListener('keydown', changeDirection);
|
| 263 |
+
restartButton.addEventListener('click', startGame);
|
| 264 |
+
|
| 265 |
+
// --- 7. ๊ฒ์ ์์ ---
|
| 266 |
+
startGame();
|
| 267 |
+
|
| 268 |
+
</script>
|
| 269 |
+
</body>
|
| 270 |
+
</html>
|