kimhyunwoo commited on
Commit
69c2f0c
·
verified ·
1 Parent(s): 51c0083

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +235 -18
index.html CHANGED
@@ -1,19 +1,236 @@
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>
3
+ <head>
4
+ <title>Emoji Bird</title>
5
+ <meta charset="utf-8">
6
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
7
+ <style>
8
+ body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #70c5ce; font-family: sans-serif; }
9
+ canvas { border: 1px solid #000; background-color: #70c5ce; }
10
+ #instructions { text-align: center; position: absolute; top: 20px; font-size: 16px; color: #fff; background-color: rgba(0,0,0,0.5); padding: 10px; border-radius: 5px;}
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <div id="instructions">
15
+ Click or Press Space to Flap<br>
16
+ <i class="fas fa-mouse"></i> / <i class="fas fa-keyboard"></i> (Space)
17
+ </div>
18
+ <canvas id="gameCanvas"></canvas>
19
+ <script>
20
+ const canvas = document.getElementById('gameCanvas');
21
+ const ctx = canvas.getContext('2d');
22
+ canvas.width = 288;
23
+ canvas.height = 512;
24
+
25
+ const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
26
+
27
+ function playSound(type) {
28
+ if (!audioCtx) return;
29
+ const oscillator = audioCtx.createOscillator();
30
+ const gainNode = audioCtx.createGain();
31
+ oscillator.connect(gainNode);
32
+ gainNode.connect(audioCtx.destination);
33
+ gainNode.gain.setValueAtTime(0.05, audioCtx.currentTime);
34
+
35
+ if (type === 'flap') {
36
+ oscillator.type = 'triangle';
37
+ oscillator.frequency.setValueAtTime(300, audioCtx.currentTime);
38
+ gainNode.gain.exponentialRampToValueAtTime(0.00001, audioCtx.currentTime + 0.15);
39
+ } else if (type === 'hit') {
40
+ oscillator.type = 'sawtooth';
41
+ oscillator.frequency.setValueAtTime(100, audioCtx.currentTime);
42
+ gainNode.gain.exponentialRampToValueAtTime(0.00001, audioCtx.currentTime + 0.3);
43
+ } else if (type === 'point') {
44
+ oscillator.type = 'sine';
45
+ oscillator.frequency.setValueAtTime(440, audioCtx.currentTime);
46
+ gainNode.gain.exponentialRampToValueAtTime(0.00001, audioCtx.currentTime + 0.1);
47
+ }
48
+ oscillator.start(audioCtx.currentTime);
49
+ oscillator.stop(audioCtx.currentTime + 0.3);
50
+ }
51
+
52
+ let bird = { x: 50, y: 150, size: 20, dy: 0, emoji: "🐦" };
53
+ const gravity = 0.25;
54
+ const flapStrength = -4.5;
55
+ let pipes = [];
56
+ const pipeWidth = 55;
57
+ const pipeGap = 110;
58
+ let score = 0;
59
+ let frame = 0;
60
+ let gameState = 'start'; // 'start', 'playing', 'over'
61
+
62
+ function drawBird() {
63
+ ctx.font = `${bird.size * 1.5}px Arial`;
64
+ ctx.textAlign = 'center';
65
+ ctx.textBaseline = 'middle';
66
+ ctx.fillText(bird.emoji, bird.x, bird.y);
67
+ }
68
+
69
+ function drawPipes() {
70
+ ctx.fillStyle = "#2E7D32"; // Darker green
71
+ pipes.forEach(p => {
72
+ ctx.fillRect(p.x, 0, pipeWidth, p.topHeight);
73
+ ctx.fillRect(p.x, p.topHeight + pipeGap, pipeWidth, canvas.height - (p.topHeight + pipeGap));
74
+ });
75
+ }
76
+
77
+ function updateBird() {
78
+ if (gameState === 'playing') {
79
+ bird.dy += gravity;
80
+ bird.y += bird.dy;
81
+ }
82
+ if ((bird.y + bird.size / 2 > canvas.height) || (bird.y - bird.size / 2 < 0)) {
83
+ if (gameState === 'playing') playSound('hit');
84
+ gameState = 'over';
85
+ }
86
+ }
87
+
88
+ function updatePipes() {
89
+ if (gameState !== 'playing') return;
90
+
91
+ if (frame % 100 === 0) { // Pipe generation frequency
92
+ const topHeight = Math.random() * (canvas.height - pipeGap - 150) + 75; // Ensure gap is not too high or low
93
+ pipes.push({ x: canvas.width, topHeight: topHeight, passed: false });
94
+ }
95
+
96
+ pipes.forEach(p => {
97
+ p.x -= 2; // Pipe speed
98
+ });
99
+
100
+ pipes = pipes.filter(p => p.x + pipeWidth > 0);
101
+
102
+ pipes.forEach(p => {
103
+ const birdLeft = bird.x - bird.size / 2;
104
+ const birdRight = bird.x + bird.size / 2;
105
+ const birdTop = bird.y - bird.size / 2;
106
+ const birdBottom = bird.y + bird.size / 2;
107
+
108
+ const pipeRight = p.x + pipeWidth;
109
+ const pipeBottomTop = p.topHeight; // Bottom of the top pipe
110
+ const pipeTopBottom = p.topHeight + pipeGap; // Top of the bottom pipe
111
+
112
+ if (birdRight > p.x && birdLeft < pipeRight) {
113
+ if (birdTop < pipeBottomTop || birdBottom > pipeTopBottom) {
114
+ if (gameState === 'playing') playSound('hit');
115
+ gameState = 'over';
116
+ }
117
+ }
118
+
119
+ if (!p.passed && p.x + pipeWidth < bird.x - bird.size / 2) {
120
+ score++;
121
+ p.passed = true;
122
+ playSound('point');
123
+ }
124
+ });
125
+ }
126
+
127
+ function resetGame() {
128
+ bird.y = 150;
129
+ bird.dy = 0;
130
+ pipes = [];
131
+ score = 0;
132
+ frame = 0;
133
+ gameState = 'start';
134
+ }
135
+
136
+ function drawScore() {
137
+ ctx.fillStyle = "#fff";
138
+ ctx.font = "24px 'Press Start 2P', sans-serif";
139
+ ctx.textAlign = "left";
140
+ ctx.fillText("Score: " + score, 10, 30);
141
+ }
142
+
143
+ function drawStartScreen() {
144
+ ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
145
+ ctx.fillRect(0, canvas.height / 2 - 60, canvas.width, 120);
146
+ ctx.fillStyle = "#fff";
147
+ ctx.font = "20px 'Press Start 2P', sans-serif";
148
+ ctx.textAlign = "center";
149
+ ctx.fillText("Emoji Bird", canvas.width / 2, canvas.height / 2 - 20);
150
+ ctx.font = "14px 'Press Start 2P', sans-serif";
151
+ ctx.fillText("Click or Space to Start", canvas.width / 2, canvas.height / 2 + 20);
152
+ ctx.font = "40px Arial";
153
+ ctx.fillText("🐦", canvas.width / 2, canvas.height / 3);
154
+ }
155
+
156
+ function drawGameOverScreen() {
157
+ ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
158
+ ctx.fillRect(0, canvas.height / 2 - 70, canvas.width, 160);
159
+ ctx.fillStyle = "red";
160
+ ctx.font = "28px 'Press Start 2P', sans-serif";
161
+ ctx.textAlign = "center";
162
+ ctx.fillText("Game Over!", canvas.width / 2, canvas.height / 2 - 30);
163
+ ctx.fillStyle = "#fff";
164
+ ctx.font = "18px 'Press Start 2P', sans-serif";
165
+ ctx.fillText("Score: " + score, canvas.width / 2, canvas.height / 2 + 10);
166
+ ctx.font = "12px 'Press Start 2P', sans-serif";
167
+ ctx.fillText("Click or Space to Restart", canvas.width / 2, canvas.height / 2 + 50);
168
+ ctx.font = "20px 'Font Awesome 6 Free'";
169
+ ctx.fillText('\uf071', canvas.width / 2, canvas.height / 2 + 80); // Font Awesome Warning Icon
170
+ }
171
+
172
+ function gameLoop() {
173
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
174
+
175
+ if (gameState === 'start') {
176
+ drawPipes(); // Draw static pipes for background
177
+ drawBird();
178
+ drawStartScreen();
179
+ } else if (gameState === 'playing') {
180
+ updateBird();
181
+ updatePipes();
182
+ drawPipes();
183
+ drawBird();
184
+ drawScore();
185
+ frame++;
186
+ } else if (gameState === 'over') {
187
+ drawPipes(); // Keep pipes visible
188
+ drawBird(); // Keep bird visible
189
+ drawScore(); // Show final score
190
+ drawGameOverScreen();
191
+ }
192
+ requestAnimationFrame(gameLoop);
193
+ }
194
+
195
+ function handleInput() {
196
+ if (gameState === 'start') {
197
+ gameState = 'playing';
198
+ bird.dy = flapStrength;
199
+ playSound('flap');
200
+ } else if (gameState === 'playing') {
201
+ bird.dy = flapStrength;
202
+ playSound('flap');
203
+ } else if (gameState === 'over') {
204
+ resetGame();
205
+ }
206
+ }
207
+
208
+ document.addEventListener('click', handleInput);
209
+ document.addEventListener('keydown', function(e) {
210
+ if (e.code === 'Space') {
211
+ e.preventDefault();
212
+ handleInput();
213
+ }
214
+ });
215
+
216
+ // Add a retro font via Google Fonts for better text rendering
217
+ const fontLink = document.createElement('link');
218
+ fontLink.href = 'https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap';
219
+ fontLink.rel = 'stylesheet';
220
+ document.head.appendChild(fontLink);
221
+
222
+ fontLink.onload = () => { // Start game loop after font is loaded to prevent FOUT
223
+ gameLoop();
224
+ };
225
+ if (document.fonts) { // Fallback if onload doesn't fire as expected in some browsers
226
+ document.fonts.load('1em "Press Start 2P"').then(() => {
227
+ if(gameState === 'start' && frame === 0) gameLoop(); // Ensure gameLoop is not called twice
228
+ });
229
+ } else {
230
+ gameLoop(); // For browsers not supporting document.fonts
231
+ }
232
+
233
+
234
+ </script>
235
+ </body>
236
+ </html>