createcodeinnovation commited on
Commit
09cc2b3
ยท
verified ยท
1 Parent(s): a3915f0

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +270 -19
index.html CHANGED
@@ -1,19 +1,270 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>