arirajuns commited on
Commit
944af14
·
verified ·
1 Parent(s): abc71b9

Add 2 files

Browse files
Files changed (2) hide show
  1. README.md +6 -4
  2. index.html +443 -19
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Space 2
3
- emoji: 🔥
4
  colorFrom: blue
5
- colorTo: green
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: space-2
3
+ emoji: 🐳
4
  colorFrom: blue
5
+ colorTo: red
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
index.html CHANGED
@@ -1,19 +1,443 @@
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="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Spring Jump - Hooke's Law Physics Game</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.18.0/matter.min.js"></script>
9
+ <style>
10
+ #gameCanvas {
11
+ background: linear-gradient(to bottom, #87CEEB, #E0F7FA);
12
+ border-radius: 10px;
13
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
14
+ }
15
+
16
+ .spring {
17
+ transition: height 0.1s ease-out;
18
+ }
19
+
20
+ .platform {
21
+ background: linear-gradient(to right, #4CAF50, #8BC34A);
22
+ border-radius: 5px;
23
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
24
+ }
25
+
26
+ .character {
27
+ background: linear-gradient(to bottom, #FF5722, #FF9800);
28
+ border-radius: 50% 50% 40% 40%;
29
+ position: relative;
30
+ }
31
+
32
+ .character::before {
33
+ content: "";
34
+ position: absolute;
35
+ width: 10px;
36
+ height: 10px;
37
+ background: #333;
38
+ border-radius: 50%;
39
+ top: 25%;
40
+ left: 30%;
41
+ }
42
+
43
+ .character::after {
44
+ content: "";
45
+ position: absolute;
46
+ width: 10px;
47
+ height: 10px;
48
+ background: #333;
49
+ border-radius: 50%;
50
+ top: 25%;
51
+ right: 30%;
52
+ }
53
+
54
+ .score-display {
55
+ background: rgba(255, 255, 255, 0.8);
56
+ border-radius: 20px;
57
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
58
+ }
59
+
60
+ .instructions {
61
+ background: rgba(255, 255, 255, 0.9);
62
+ border-radius: 10px;
63
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
64
+ }
65
+ </style>
66
+ </head>
67
+ <body class="bg-gray-100 min-h-screen flex flex-col items-center justify-center p-4">
68
+ <div class="w-full max-w-4xl">
69
+ <h1 class="text-4xl font-bold text-center text-orange-600 mb-2">Spring Jump</h1>
70
+ <p class="text-center text-gray-700 mb-6">Experience Hooke's Law in action! Hold space to compress the spring and release to jump.</p>
71
+
72
+ <div class="relative">
73
+ <canvas id="gameCanvas" width="800" height="500" class="w-full border-2 border-gray-300"></canvas>
74
+
75
+ <div class="instructions absolute top-4 left-4 p-3 max-w-xs">
76
+ <h3 class="font-bold text-gray-800 mb-2">How to Play:</h3>
77
+ <ul class="text-sm text-gray-700 list-disc pl-4">
78
+ <li>Hold <span class="font-mono bg-gray-200 px-1">SPACE</span> to compress spring</li>
79
+ <li>Release to jump</li>
80
+ <li>Longer compression = higher jump</li>
81
+ <li>Reach platforms to score points</li>
82
+ </ul>
83
+ </div>
84
+
85
+ <div class="score-display absolute top-4 right-4 px-4 py-2">
86
+ <p class="text-lg font-bold text-gray-800">Score: <span id="score">0</span></p>
87
+ <p class="text-sm text-gray-600">High Score: <span id="highScore">0</span></p>
88
+ </div>
89
+ </div>
90
+
91
+ <div class="mt-6 flex justify-between items-center">
92
+ <div class="flex items-center">
93
+ <div class="w-8 h-8 rounded-full bg-orange-500 mr-2"></div>
94
+ <span class="text-gray-700">Character</span>
95
+ </div>
96
+ <div class="flex items-center">
97
+ <div class="w-8 h-3 rounded bg-green-500 mr-2"></div>
98
+ <span class="text-gray-700">Platforms</span>
99
+ </div>
100
+ <div class="flex items-center">
101
+ <div class="w-2 h-8 bg-gray-800 mr-2 spring-icon"></div>
102
+ <span class="text-gray-700">Spring</span>
103
+ </div>
104
+ </div>
105
+
106
+ <div class="mt-6 bg-white p-4 rounded-lg shadow">
107
+ <h3 class="font-bold text-gray-800 mb-2">Physics Explained:</h3>
108
+ <p class="text-gray-700 mb-2">This game demonstrates <span class="font-bold">Hooke's Law</span> (F = -kx) where:</p>
109
+ <ul class="text-sm text-gray-700 list-disc pl-4 mb-3">
110
+ <li>F = Force applied by the spring</li>
111
+ <li>k = Spring constant (stiffness)</li>
112
+ <li>x = Displacement from equilibrium</li>
113
+ </ul>
114
+ <p class="text-gray-700">The more you compress the spring (increase x), the more force it applies, resulting in higher jumps!</p>
115
+ </div>
116
+ </div>
117
+
118
+ <script>
119
+ // Game setup
120
+ const canvas = document.getElementById('gameCanvas');
121
+ const ctx = canvas.getContext('2d');
122
+ const scoreElement = document.getElementById('score');
123
+ const highScoreElement = document.getElementById('highScore');
124
+
125
+ // Matter.js modules
126
+ const { Engine, Render, Runner, Bodies, Composite, Body, Events, Mouse, MouseConstraint } = Matter;
127
+
128
+ // Create engine
129
+ const engine = Engine.create({
130
+ gravity: { x: 0, y: 1 }
131
+ });
132
+
133
+ // Game variables
134
+ let score = 0;
135
+ let highScore = localStorage.getItem('highScore') || 0;
136
+ let isCompressing = false;
137
+ let compressionStartTime = 0;
138
+ let maxCompression = 0;
139
+ const springConstant = 0.002;
140
+ const characterSize = 30;
141
+ let character, spring, ground;
142
+ let platforms = [];
143
+ let gameStarted = false;
144
+
145
+ // Initialize game
146
+ function initGame() {
147
+ // Clear previous bodies
148
+ Composite.clear(engine.world);
149
+
150
+ // Create ground
151
+ ground = Bodies.rectangle(canvas.width / 2, canvas.height - 10, canvas.width, 20, {
152
+ isStatic: true,
153
+ render: {
154
+ fillStyle: '#795548'
155
+ }
156
+ });
157
+
158
+ // Create character
159
+ character = Bodies.circle(canvas.width / 2, canvas.height - 60, characterSize, {
160
+ restitution: 0.3,
161
+ friction: 0.1,
162
+ render: {
163
+ fillStyle: '#FF5722'
164
+ }
165
+ });
166
+
167
+ // Create spring (visual only, physics handled separately)
168
+ spring = {
169
+ x: canvas.width / 2,
170
+ y: canvas.height - 40,
171
+ width: 5,
172
+ height: 20,
173
+ naturalHeight: 20,
174
+ compressedHeight: 5
175
+ };
176
+
177
+ // Create initial platforms
178
+ createPlatforms();
179
+
180
+ // Add all bodies to the world
181
+ Composite.add(engine.world, [ground, character, ...platforms]);
182
+
183
+ // Reset score
184
+ score = 0;
185
+ scoreElement.textContent = score;
186
+ highScoreElement.textContent = highScore;
187
+
188
+ gameStarted = true;
189
+ }
190
+
191
+ // Create platforms at varying heights
192
+ function createPlatforms() {
193
+ platforms = [];
194
+
195
+ // Starting platform
196
+ platforms.push(
197
+ Bodies.rectangle(canvas.width / 2, canvas.height - 30, 200, 20, {
198
+ isStatic: true,
199
+ render: {
200
+ fillStyle: '#4CAF50'
201
+ },
202
+ label: 'platform'
203
+ })
204
+ );
205
+
206
+ // Additional platforms
207
+ const platformCount = 8;
208
+ const minHeight = canvas.height / 2;
209
+ const maxHeight = 100;
210
+
211
+ for (let i = 0; i < platformCount; i++) {
212
+ const width = 100 + Math.random() * 100;
213
+ const x = 50 + Math.random() * (canvas.width - 100);
214
+ const y = minHeight - (i * ((minHeight - maxHeight) / platformCount));
215
+
216
+ platforms.push(
217
+ Bodies.rectangle(x, y, width, 15, {
218
+ isStatic: true,
219
+ render: {
220
+ fillStyle: '#8BC34A'
221
+ },
222
+ label: 'platform'
223
+ })
224
+ );
225
+ }
226
+ }
227
+
228
+ // Handle spring compression and jumping
229
+ function handleSpring() {
230
+ if (!gameStarted) return;
231
+
232
+ const characterPos = character.position;
233
+
234
+ // Check if character is on ground or platform
235
+ const isOnSurface = platforms.some(platform => {
236
+ return (
237
+ characterPos.y + characterSize >= platform.position.y - 10 &&
238
+ characterPos.y + characterSize <= platform.position.y + 10 &&
239
+ characterPos.x >= platform.position.x - platform.bounds.max.x + platform.position.x &&
240
+ characterPos.x <= platform.position.x + platform.bounds.max.x - platform.position.x
241
+ );
242
+ });
243
+
244
+ if (isOnSurface) {
245
+ // Position spring under character
246
+ spring.x = characterPos.x;
247
+ spring.y = characterPos.y + characterSize;
248
+
249
+ // Handle space key press for spring compression
250
+ if (isCompressing) {
251
+ const compressionTime = Date.now() - compressionStartTime;
252
+ const compressionRatio = Math.min(compressionTime / 1000, 1); // Max 1 second compression
253
+
254
+ // Calculate compressed height (visual)
255
+ spring.height = spring.naturalHeight - (compressionRatio * (spring.naturalHeight - spring.compressedHeight));
256
+
257
+ // Track max compression for jump force
258
+ maxCompression = Math.max(maxCompression, compressionRatio);
259
+ } else {
260
+ // Reset spring when not compressing
261
+ spring.height = spring.naturalHeight;
262
+ }
263
+
264
+ // Apply jump force when space is released after compression
265
+ if (!isCompressing && maxCompression > 0) {
266
+ // Calculate jump force based on Hooke's Law (F = -kx)
267
+ const jumpForce = -springConstant * (maxCompression * 1000);
268
+
269
+ // Apply impulse to character
270
+ Body.applyForce(character, character.position, {
271
+ x: 0,
272
+ y: jumpForce
273
+ });
274
+
275
+ maxCompression = 0;
276
+ }
277
+ } else {
278
+ // Hide spring when character is in the air
279
+ spring.height = 0;
280
+ }
281
+ }
282
+
283
+ // Check for platform collisions to score points
284
+ function checkPlatformCollisions() {
285
+ if (!gameStarted) return;
286
+
287
+ platforms.forEach(platform => {
288
+ if (
289
+ character.position.y + characterSize >= platform.position.y - 15 &&
290
+ character.position.y + characterSize <= platform.position.y + 5 &&
291
+ character.position.x >= platform.position.x - (platform.bounds.max.x - platform.position.x) &&
292
+ character.position.x <= platform.position.x + (platform.bounds.max.x - platform.position.x) &&
293
+ character.velocity.y > 0 // Only count when falling onto platform
294
+ ) {
295
+ // Only score if this is a new platform (not the current standing one)
296
+ if (!platform.scored) {
297
+ score += 10;
298
+ scoreElement.textContent = score;
299
+
300
+ if (score > highScore) {
301
+ highScore = score;
302
+ highScoreElement.textContent = highScore;
303
+ localStorage.setItem('highScore', highScore);
304
+ }
305
+
306
+ platform.scored = true;
307
+
308
+ // Mark other platforms as not scored
309
+ platforms.forEach(p => {
310
+ if (p !== platform) p.scored = false;
311
+ });
312
+ }
313
+ }
314
+ });
315
+ }
316
+
317
+ // Game loop
318
+ function gameLoop() {
319
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
320
+
321
+ // Draw ground
322
+ ctx.fillStyle = '#795548';
323
+ ctx.fillRect(0, canvas.height - 10, canvas.width, 20);
324
+
325
+ // Draw platforms
326
+ platforms.forEach(platform => {
327
+ ctx.fillStyle = platform.render.fillStyle;
328
+ ctx.fillRect(
329
+ platform.position.x - (platform.bounds.max.x - platform.position.x),
330
+ platform.position.y - (platform.bounds.max.y - platform.position.y),
331
+ platform.bounds.max.x - platform.bounds.min.x,
332
+ platform.bounds.max.y - platform.bounds.min.y
333
+ );
334
+ });
335
+
336
+ // Draw spring
337
+ if (spring.height > 0) {
338
+ ctx.fillStyle = '#333';
339
+ ctx.fillRect(
340
+ spring.x - spring.width / 2,
341
+ spring.y,
342
+ spring.width,
343
+ spring.height
344
+ );
345
+
346
+ // Draw spring coils
347
+ const coils = 4;
348
+ const coilHeight = spring.height / coils;
349
+
350
+ for (let i = 0; i < coils; i++) {
351
+ ctx.beginPath();
352
+ ctx.arc(
353
+ spring.x,
354
+ spring.y + (i * coilHeight) + (coilHeight / 2),
355
+ spring.width * 1.5,
356
+ 0,
357
+ Math.PI,
358
+ true
359
+ );
360
+ ctx.strokeStyle = '#333';
361
+ ctx.lineWidth = 2;
362
+ ctx.stroke();
363
+ }
364
+ }
365
+
366
+ // Draw character
367
+ ctx.fillStyle = character.render.fillStyle;
368
+ ctx.beginPath();
369
+ ctx.arc(character.position.x, character.position.y, characterSize, 0, Math.PI * 2);
370
+ ctx.fill();
371
+
372
+ // Draw character face
373
+ ctx.fillStyle = '#333';
374
+ ctx.beginPath();
375
+ ctx.arc(character.position.x - 10, character.position.y - 5, 5, 0, Math.PI * 2); // Left eye
376
+ ctx.arc(character.position.x + 10, character.position.y - 5, 5, 0, Math.PI * 2); // Right eye
377
+ ctx.fill();
378
+
379
+ // Draw mouth (smile when jumping, straight when compressing)
380
+ ctx.beginPath();
381
+ if (isCompressing) {
382
+ ctx.moveTo(character.position.x - 10, character.position.y + 5);
383
+ ctx.lineTo(character.position.x + 10, character.position.y + 5);
384
+ } else {
385
+ ctx.arc(character.position.x, character.position.y + 5, 10, 0, Math.PI);
386
+ }
387
+ ctx.strokeStyle = '#333';
388
+ ctx.lineWidth = 2;
389
+ ctx.stroke();
390
+
391
+ // Handle spring mechanics
392
+ handleSpring();
393
+
394
+ // Check for scoring
395
+ checkPlatformCollisions();
396
+
397
+ // Game over if character falls off screen
398
+ if (character.position.y > canvas.height + 100) {
399
+ gameStarted = false;
400
+ setTimeout(initGame, 1000);
401
+ }
402
+
403
+ requestAnimationFrame(gameLoop);
404
+ }
405
+
406
+ // Event listeners
407
+ window.addEventListener('keydown', (e) => {
408
+ if (e.code === 'Space') {
409
+ if (!isCompressing && gameStarted) {
410
+ isCompressing = true;
411
+ compressionStartTime = Date.now();
412
+ }
413
+ }
414
+ });
415
+
416
+ window.addEventListener('keyup', (e) => {
417
+ if (e.code === 'Space') {
418
+ isCompressing = false;
419
+ }
420
+ });
421
+
422
+ // Touch support for mobile
423
+ canvas.addEventListener('touchstart', () => {
424
+ if (!isCompressing && gameStarted) {
425
+ isCompressing = true;
426
+ compressionStartTime = Date.now();
427
+ }
428
+ });
429
+
430
+ canvas.addEventListener('touchend', () => {
431
+ isCompressing = false;
432
+ });
433
+
434
+ // Start the game
435
+ initGame();
436
+ gameLoop();
437
+
438
+ // Start Matter.js engine
439
+ const runner = Runner.create();
440
+ Runner.run(runner, engine);
441
+ </script>
442
+ <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=arirajuns/space-2" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
443
+ </html>