testfile1 / index.html
createcodeinnovation's picture
Update index.html
09cc2b3 verified
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML ์ง€๋ ์ด ๊ฒŒ์ž„</title>
<style>
/* ๊ธฐ๋ณธ ์Šคํƒ€์ผ */
body {
background-color: #f0f0f0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
/* ๊ฒŒ์ž„ ์ปจํ…Œ์ด๋„ˆ ์Šคํƒ€์ผ */
#game-container {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
}
h1 {
color: #333;
margin-bottom: 10px;
}
/* ๊ฒŒ์ž„ ์บ”๋ฒ„์Šค ์Šคํƒ€์ผ */
#gameCanvas {
background-color: #dcedc8; /* ์—ฐํ•œ ๋…น์ƒ‰ ๋ฐฐ๊ฒฝ */
border: 2px solid #333;
}
/* ์ ์ˆ˜ํŒ ์Šคํƒ€์ผ */
#score-board {
font-size: 24px;
font-weight: bold;
color: #555;
margin-top: 10px;
}
/* ๊ฒŒ์ž„ ์˜ค๋ฒ„ ๋ฉ”์‹œ์ง€ ์Šคํƒ€์ผ */
#game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px 40px;
border-radius: 10px;
display: none; /* ๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆจ๊น€ */
text-align: center;
}
#game-over h2 {
margin: 0 0 10px 0;
}
#restart-button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
#restart-button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="game-container">
<h1>์ง€๋ ์ด ๊ฒŒ์ž„</h1>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="score-board">์ ์ˆ˜: <span id="score">0</span></div>
</div>
<div id="game-over">
<h2>๊ฒŒ์ž„ ์˜ค๋ฒ„!</h2>
<button id="restart-button">๋‹ค์‹œ ์‹œ์ž‘</button>
</div>
<script>
// --- 1. ์ดˆ๊ธฐ ์„ค์ • ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('game-over');
const restartButton = document.getElementById('restart-button');
const BOX_SIZE = 20; // ๊ฐ ์นธ์˜ ํฌ๊ธฐ
const CANVAS_WIDTH = canvas.width;
const CANVAS_HEIGHT = canvas.height;
let snake; // ์ง€๋ ์ด ๋ฐฐ์—ด (x, y ์ขŒํ‘œ ๊ฐ์ฒด)
let food; // ๋จน์ด ๊ฐ์ฒด (x, y ์ขŒํ‘œ)
let score; // ์ ์ˆ˜
let direction; // ํ˜„์žฌ ์ด๋™ ๋ฐฉํ–ฅ
let changingDirection; // ๋ฐฉํ–ฅ ์ „ํ™˜ ์ค‘์ธ์ง€ ํ™•์ธ (์—ฐ์†์ ์ธ ํ‚ค ์ž…๋ ฅ ๋ฐฉ์ง€)
let gameLoop; // ๊ฒŒ์ž„ ๋ฃจํ”„ setInterval ID
// --- 2. ๊ฒŒ์ž„ ์‹œ์ž‘ ๋ฐ ์žฌ์‹œ์ž‘ ---
function startGame() {
// ๊ฒŒ์ž„ ์ƒํƒœ ์ดˆ๊ธฐํ™”
snake = [
{ x: 10 * BOX_SIZE, y: 10 * BOX_SIZE },
{ x: 9 * BOX_SIZE, y: 10 * BOX_SIZE },
{ x: 8 * BOX_SIZE, y: 10 * BOX_SIZE }
];
score = 0;
direction = 'RIGHT';
changingDirection = false;
// UI ์ดˆ๊ธฐํ™”
scoreElement.textContent = score;
gameOverElement.style.display = 'none';
generateFood(); // ์ฒซ ๋จน์ด ์ƒ์„ฑ
// ๊ธฐ์กด ๊ฒŒ์ž„ ๋ฃจํ”„๊ฐ€ ์žˆ๋‹ค๋ฉด ์ค‘์ง€
if (gameLoop) {
clearInterval(gameLoop);
}
// ์ƒˆ๋กœ์šด ๊ฒŒ์ž„ ๋ฃจํ”„ ์‹œ์ž‘ (100ms ๋งˆ๋‹ค main ํ•จ์ˆ˜ ์‹คํ–‰)
gameLoop = setInterval(main, 100);
}
// --- 3. ๊ฒŒ์ž„ ๋ฉ”์ธ ๋ฃจํ”„ ---
function main() {
if (checkGameOver()) {
clearInterval(gameLoop);
gameOverElement.style.display = 'block';
return;
}
changingDirection = false;
clearCanvas();
drawFood();
moveSnake();
drawSnake();
}
// --- 4. ๊ทธ๋ฆฌ๊ธฐ ํ•จ์ˆ˜๋“ค ---
// ์บ”๋ฒ„์Šค ์ง€์šฐ๊ธฐ
function clearCanvas() {
ctx.fillStyle = '#dcedc8';
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
}
// ์ง€๋ ์ด ๊ทธ๋ฆฌ๊ธฐ
function drawSnake() {
snake.forEach((segment, index) => {
// ๋จธ๋ฆฌ ์ƒ‰์ƒ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ
ctx.fillStyle = (index === 0) ? '#1b5e20' : '#4caf50';
ctx.strokeStyle = '#388e3c';
ctx.fillRect(segment.x, segment.y, BOX_SIZE, BOX_SIZE);
ctx.strokeRect(segment.x, segment.y, BOX_SIZE, BOX_SIZE);
});
}
// ๋จน์ด ๊ทธ๋ฆฌ๊ธฐ
function drawFood() {
ctx.fillStyle = '#d32f2f'; // ๋ถ‰์€์ƒ‰ ๋จน์ด
ctx.strokeStyle = '#c62828';
ctx.fillRect(food.x, food.y, BOX_SIZE, BOX_SIZE);
ctx.strokeRect(food.x, food.y, BOX_SIZE, BOX_SIZE);
}
// --- 5. ๊ฒŒ์ž„ ๋กœ์ง ํ•จ์ˆ˜๋“ค ---
// ๋จน์ด ์ƒ์„ฑ (์ง€๋ ์ด ๋ชธ ์œ„๊ฐ€ ์•„๋‹Œ ๊ณณ์—)
function generateFood() {
while (true) {
const foodX = Math.floor(Math.random() * (CANVAS_WIDTH / BOX_SIZE)) * BOX_SIZE;
const foodY = Math.floor(Math.random() * (CANVAS_HEIGHT / BOX_SIZE)) * BOX_SIZE;
let isFoodOnSnake = snake.some(segment => segment.x === foodX && segment.y === foodY);
if (!isFoodOnSnake) {
food = { x: foodX, y: foodY };
return;
}
}
}
// ์ง€๋ ์ด ์ด๋™
function moveSnake() {
let dx = 0;
let dy = 0;
if (direction === 'RIGHT') dx = BOX_SIZE;
if (direction === 'LEFT') dx = -BOX_SIZE;
if (direction === 'UP') dy = -BOX_SIZE;
if (direction === 'DOWN') dy = BOX_SIZE;
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
snake.unshift(head); // ์ƒˆ๋กœ์šด ๋จธ๋ฆฌ๋ฅผ ๋ฐฐ์—ด ๋งจ ์•ž์— ์ถ”๊ฐ€
// ๋จน์ด๋ฅผ ๋จน์—ˆ๋Š”์ง€ ํ™•์ธ
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
generateFood();
} else {
snake.pop(); // ๋จน์ด๋ฅผ ๋จน์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ผฌ๋ฆฌ ์ œ๊ฑฐ
}
}
// ๊ฒŒ์ž„ ์˜ค๋ฒ„ ์กฐ๊ฑด ํ™•์ธ
function checkGameOver() {
// 1. ๋ฒฝ ์ถฉ๋Œ ํ™•์ธ
const head = snake[0];
if (head.x < 0 || head.x >= CANVAS_WIDTH || head.y < 0 || head.y >= CANVAS_HEIGHT) {
return true;
}
// 2. ์ž๊ธฐ ๋ชธ ์ถฉ๋Œ ํ™•์ธ
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
return true;
}
}
return false;
}
// --- 6. ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ---
// ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ์ฒ˜๋ฆฌ
function changeDirection(event) {
if (changingDirection) return;
changingDirection = true;
const keyPressed = event.key;
const goingUp = direction === 'UP';
const goingDown = direction === 'DOWN';
const goingRight = direction === 'RIGHT';
const goingLeft = direction === 'LEFT';
if ((keyPressed === 'ArrowLeft' || keyPressed.toLowerCase() === 'a') && !goingRight) {
direction = 'LEFT';
}
if ((keyPressed === 'ArrowUp' || keyPressed.toLowerCase() === 'w') && !goingDown) {
direction = 'UP';
}
if ((keyPressed === 'ArrowRight' || keyPressed.toLowerCase() === 'd') && !goingLeft) {
direction = 'RIGHT';
}
if ((keyPressed === 'ArrowDown' || keyPressed.toLowerCase() === 's') && !goingUp) {
direction = 'DOWN';
}
}
document.addEventListener('keydown', changeDirection);
restartButton.addEventListener('click', startGame);
// --- 7. ๊ฒŒ์ž„ ์‹œ์ž‘ ---
startGame();
</script>
</body>
</html>