Sunghokim commited on
Commit
e485332
·
verified ·
1 Parent(s): aafbfd0

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +198 -19
index.html CHANGED
@@ -1,19 +1,198 @@
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
+ <script>
2
+ const gameArea = document.getElementById('gameArea');
3
+ const player = document.getElementById('player');
4
+ let score = 0;
5
+ let level = 1;
6
+ let isGameActive = true;
7
+ let playerX = 385;
8
+ let playerY = 285;
9
+ let playerSpeed = 5;
10
+ let enemies = [];
11
+ let balls = [];
12
+ let powerups = [];
13
+ let hasShield = false;
14
+ const keys = {};
15
+
16
+ class Enemy {
17
+ constructor(type) {
18
+ this.element = document.createElement('div');
19
+ this.element.className = `enemy ${type}`;
20
+ this.type = type;
21
+ this.x = Math.random() * 750;
22
+ this.y = type === 'bouncer' ? 550 : Math.random() * 550;
23
+ this.dx = (Math.random() - 0.5) * 4;
24
+ this.dy = type === 'bouncer' ? -10 : (Math.random() - 0.5) * 4;
25
+ gameArea.appendChild(this.element);
26
+ this.startShooting();
27
+ }
28
+
29
+ update() {
30
+ if (this.type === 'bouncer') {
31
+ this.dy += 0.5; // Gravity
32
+ if (this.y > 550) this.dy = -Math.abs(this.dy) * 0.8;
33
+ }
34
+
35
+ this.x += this.dx;
36
+ this.y += this.dy;
37
+
38
+ // Bounce off walls
39
+ if (this.x < 0 || this.x > 750) this.dx *= -1;
40
+ if (this.y < 0) this.dy = Math.abs(this.dy);
41
+
42
+ this.element.style.left = this.x + 'px';
43
+ this.element.style.top = this.y + 'px';
44
+ this.createTrail();
45
+ }
46
+
47
+ startShooting() {
48
+ setInterval(() => {
49
+ if (!isGameActive) return;
50
+ this.shoot();
51
+ }, 2000);
52
+ }
53
+
54
+ shoot() {
55
+ const types = ['fire-ball', 'ice-ball', 'thunder-ball'];
56
+ const type = types[Math.floor(Math.random() * types.length)];
57
+ const ball = this.createBall(type);
58
+ this.launchBall(ball);
59
+ }
60
+
61
+ createBall(type) {
62
+ const ball = document.createElement('div');
63
+ ball.className = `ball ${type}`;
64
+ ball.style.left = this.x + 'px';
65
+ ball.style.top = this.y + 'px';
66
+ gameArea.appendChild(ball);
67
+ return { element: ball, type };
68
+ }
69
+
70
+ launchBall(ball) {
71
+ const angle = Math.atan2(playerY - this.y, playerX - this.x);
72
+ const speed = ball.type === 'ice-ball' ? 7 :
73
+ ball.type === 'thunder-ball' ? 4 : 5;
74
+
75
+ balls.push({
76
+ ...ball,
77
+ x: this.x,
78
+ y: this.y,
79
+ dx: Math.cos(angle) * speed,
80
+ dy: Math.sin(angle) * speed
81
+ });
82
+ }
83
+
84
+ createTrail() {
85
+ const trail = document.createElement('div');
86
+ trail.className = 'trail';
87
+ trail.style.width = this.element.offsetWidth + 'px';
88
+ trail.style.height = this.element.offsetHeight + 'px';
89
+ trail.style.left = this.x + 'px';
90
+ trail.style.top = this.y + 'px';
91
+ trail.style.background = this.type === 'bouncer' ?
92
+ 'rgba(255,0,0,0.3)' : 'rgba(255,0,255,0.3)';
93
+ gameArea.appendChild(trail);
94
+ setTimeout(() => trail.remove(), 500);
95
+ }
96
+ }
97
+
98
+ function update() {
99
+ if (!isGameActive) return;
100
+
101
+ updatePlayer();
102
+ updateEnemies();
103
+ updateBalls();
104
+ updatePowerups();
105
+ checkCollisions();
106
+ updateScore();
107
+
108
+ requestAnimationFrame(update);
109
+ }
110
+
111
+ function updatePlayer() {
112
+ if (keys['ArrowLeft'] || keys['a']) playerX = Math.max(0, playerX - playerSpeed);
113
+ if (keys['ArrowRight'] || keys['d']) playerX = Math.min(770, playerX + playerSpeed);
114
+ if (keys['ArrowUp'] || keys['w']) playerY = Math.max(0, playerY - playerSpeed);
115
+ if (keys['ArrowDown'] || keys['s']) playerY = Math.min(570, playerY + playerSpeed);
116
+
117
+ player.style.left = playerX + 'px';
118
+ player.style.top = playerY + 'px';
119
+ }
120
+
121
+ function updateEnemies() {
122
+ enemies.forEach(enemy => enemy.update());
123
+ }
124
+
125
+ function updateBalls() {
126
+ balls.forEach((ball, index) => {
127
+ ball.x += ball.dx;
128
+ ball.y += ball.dy;
129
+ ball.element.style.left = ball.x + 'px';
130
+ ball.element.style.top = ball.y + 'px';
131
+
132
+ if (ball.x < -50 || ball.x > 850 || ball.y < -50 || ball.y > 650) {
133
+ ball.element.remove();
134
+ balls.splice(index, 1);
135
+ }
136
+ });
137
+ }
138
+
139
+ function updatePowerups() {
140
+ powerups.forEach(powerup => {
141
+ powerup.y += 2;
142
+ powerup.element.style.top = powerup.y + 'px';
143
+ if (powerup.y > 600) {
144
+ powerup.element.remove();
145
+ powerups = powerups.filter(p => p !== powerup);
146
+ }
147
+ });
148
+ }
149
+
150
+ function checkCollisions() {
151
+ const playerRect = player.getBoundingClientRect();
152
+
153
+ // Check collisions with enemies and balls
154
+ enemies.concat(balls).forEach(obj => {
155
+ const rect = obj.element.getBoundingClientRect();
156
+ if (isColliding(playerRect, rect)) {
157
+ if (hasShield) {
158
+ hasShield = false;
159
+ document.getElementById('shieldStatus').textContent = '❌';
160
+ } else {
161
+ gameOver();
162
+ }
163
+ }
164
+ });
165
+ }
166
+
167
+ function isColliding(rect1, rect2) {
168
+ return !(rect1.right < rect2.left ||
169
+ rect1.left > rect2.right ||
170
+ rect1.bottom < rect2.top ||
171
+ rect1.top > rect2.bottom);
172
+ }
173
+
174
+ function updateScore() {
175
+ score++;
176
+ document.getElementById('scoreValue').textContent = score;
177
+ if (score % 500 === 0) {
178
+ level++;
179
+ document.getElementById('levelValue').textContent = level;
180
+ enemies.push(new Enemy(Math.random() < 0.5 ? 'bouncer' : 'spinner'));
181
+ }
182
+ }
183
+
184
+ function gameOver() {
185
+ isGameActive = false;
186
+ document.getElementById('gameOver').style.display = 'flex';
187
+ document.getElementById('finalScore').textContent = score;
188
+ }
189
+
190
+ // Event Listeners
191
+ document.addEventListener('keydown', e => keys[e.key] = true);
192
+ document.addEventListener('keyup', e => keys[e.key] = false);
193
+
194
+ // Initialize game
195
+ enemies.push(new Enemy('bouncer'));
196
+ enemies.push(new Enemy('spinner'));
197
+ update();
198
+ </script>