Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
726 lines (636 loc) · 26.8 KB
/
Copy pathindex.html
File metadata and controls
726 lines (636 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ASCII-Bird</title>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Courier New', monospace;
background-color: black;
color: white;
overflow: hidden;
touch-action: manipulation;
user-select: none;
-webkit-user-select: none;
}
#game-container {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
#game-screen {
white-space: pre;
line-height: 1;
font-size: 14px;
position: relative;
height: 100vh;
}
@media (min-width: 768px) {
#game-screen {
font-size: 16px;
}
}
#header {
position: absolute;
top: 0;
left: 0;
right: 0;
padding: 10px;
display: flex;
justify-content: space-between;
font-weight: bold;
z-index: 2;
text-shadow: 0 0 5px rgba(0, 255, 0, 0.7);
}
#title {
text-align: left;
}
#scores {
text-align: right;
}
#message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 1.5em;
font-weight: bold;
background-color: rgba(0, 0, 0, 0.8);
color: white;
border: 1px solid #ffffff;
padding: 25px;
border-radius: 5px;
z-index: 10;
width: 80%;
max-width: 300px;
}
/* CRT and Grain Effects */
.crt-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%),
linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06));
background-size: 100% 2px, 3px 100%;
pointer-events: none;
z-index: 5;
}
.crt-scanline {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: repeating-linear-gradient(
0deg,
rgba(0, 0, 0, 0.15),
rgba(0, 0, 0, 0.15) 1px,
transparent 1px,
transparent 2px
);
pointer-events: none;
z-index: 4;
}
.crt-flicker {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(18, 16, 16, 0.1);
opacity: 0;
pointer-events: none;
z-index: 3;
animation: flicker 0.15s infinite alternate;
}
.grain {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)'/%3E%3C/svg%3E");
opacity: 0.05;
pointer-events: none;
z-index: 2;
}
@keyframes flicker {
0% { opacity: 0.1; }
100% { opacity: 0; }
}
/* Game screen glow effect */
#game-screen {
color: rgba(180, 255, 180, 0.9);
text-shadow: 0 0 3px rgba(0, 255, 0, 0.5);
}
.start-button {
margin-top: 20px;
display: inline-block;
padding: 10px 30px;
background-color: transparent;
border: 1px solid #ffffff;
border-radius: 3px;
cursor: pointer;
transition: all 0.3s;
}
.start-button:hover {
background-color: rgba(255, 255, 255, 0.1);
}
/* Vignette Effect */
.vignette {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: radial-gradient(ellipse at center, rgba(0,0,0,0) 60%, rgba(0,0,0,0.5) 100%);
pointer-events: none;
z-index: 6;
}
/* CRT Scan Line Effect */
.crt-scanline {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: repeating-linear-gradient(
0deg,
rgba(0, 255, 0, 0.05),
rgba(0, 255, 0, 0.05) 1px,
transparent 1px,
transparent 2px
);
pointer-events: none;
z-index: 7;
}
/* Mute Button - Updated for mobile */
#mute-button {
position: absolute;
bottom: 10px;
right: 10px;
padding: 8px 15px;
background-color: transparent;
border: 1px solid #ffffff;
border-radius: 3px;
cursor: pointer;
font-family: 'Courier New', monospace;
font-weight: bold;
color: white;
text-shadow: 0 0 5px rgba(0, 255, 0, 0.7);
z-index: 10;
transition: all 0.3s;
font-size: 12px;
}
#mute-button:hover {
background-color: rgba(255, 255, 255, 0.1);
}
/* Adjust for larger screens */
@media (min-width: 768px) {
#mute-button {
bottom: 20px;
right: 20px;
padding: 10px 20px;
font-size: inherit;
}
}
</style>
</head>
<body>
<div id="game-container">
<div id="header">
<div id="title">ASCII-BIRD</div>
<div id="scores">SCORE: 0 | HIGH: 0</div>
</div>
<div id="game-screen"></div>
<div id="message">
CLICK TO START
<div class="start-button">PLAY</div>
</div>
<!-- CRT Effect Overlays -->
<div class="crt-overlay"></div>
<div class="crt-scanline"></div>
<div class="crt-flicker"></div>
<div class="grain"></div>
<div class="vignette"></div>
<!-- Mute Button -->
<button id="mute-button">MUTE</button>
</div>
<script>
// Game characters
const PLAYER_BIRD = ['>ᴗ<', '>(ᴗ)<']; // Bird animation frames - new cute birdie
const OBSTACLE_PIPE_TOP = ['╔═╗', '║░║', '║▒║', '║▓║', '║█║']; // Top pipe texture (varied)
const OBSTACLE_PIPE_BODY = ['║░║', '║▒║', '║▓║', '║█║', '║░║']; // Pipe body texture (varied)
const OBSTACLE_PIPE_CAP = '╚═╝'; // Bottom cap of top pipe
const OBSTACLE_PIPE_TOP_CAP = '╔═╗'; // Top cap of bottom pipe
// Improved ground with more variety
const TERRAIN_GROUND = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▆', '▅', '▄', '▃'];
const TERRAIN_PATTERN = ['~', '‿', '︵', '.', '˙', '⌣'];
const EMPTY_SKY = ' '; // Sky remains empty
// Game state
let canvasWidth, canvasHeight;
let player = {
position: { x: 0, y: 0 },
jumpVelocity: 0
};
let obstacles = [];
let tick = 0;
let playerScore = 0;
let bestScore = 0;
let isGameActive = false;
let isGameEnded = false;
let gameRefreshRate = 50; // Milliseconds between frames
let gameSpeed = 1; // Base game speed multiplier
// Web Audio API setup
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
let isMuted = false;
// Function to generate a bleep sound
function playBleep() {
if (isMuted) return;
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
// Configure the oscillator
oscillator.type = 'sine'; // Softer sound
oscillator.frequency.setValueAtTime(440, audioContext.currentTime); // Higher frequency for better audibility
// Configure the gain (volume)
gainNode.gain.setValueAtTime(0.5, audioContext.currentTime); // Increased volume
gainNode.gain.exponentialRampToValueAtTime(0.0001, audioContext.currentTime + 0.1); // Fade out quickly
// Connect the oscillator to the gain node and the gain node to the audio context's destination
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
// Start and stop the oscillator
oscillator.start();
oscillator.stop(audioContext.currentTime + 0.1); // Stop after 0.1 seconds
}
// Function to generate a blorp sound
function playBlorp() {
if (isMuted) return;
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
// Configure the oscillator
oscillator.type = 'triangle'; // Softer sound
oscillator.frequency.setValueAtTime(220, audioContext.currentTime); // Lower frequency for blorp
// Configure the gain (volume)
gainNode.gain.setValueAtTime(0.8, audioContext.currentTime); // Increased volume
gainNode.gain.exponentialRampToValueAtTime(0.0001, audioContext.currentTime + 0.3); // Fade out slowly
// Connect the oscillator to the gain node and the gain node to the audio context's destination
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
// Start and stop the oscillator
oscillator.start();
oscillator.stop(audioContext.currentTime + 0.3); // Stop after 0.3 seconds
}
// Safe localStorage wrapper function
function safeLocalStorage() {
let data = {};
try {
// Test if localStorage is available
localStorage.setItem('test', 'test');
localStorage.removeItem('test');
return {
getItem: (key) => localStorage.getItem(key),
setItem: (key, value) => localStorage.setItem(key, value)
};
} catch (e) {
// Fallback to memory storage if localStorage isn't available
console.warn('localStorage not available, using memory storage instead');
return {
getItem: (key) => data[key] || null,
setItem: (key, value) => { data[key] = value; }
};
}
}
// Initialize using the safe localStorage wrapper
const storage = safeLocalStorage();
bestScore = storage.getItem('asciibird-highscore')
? parseInt(storage.getItem('asciibird-highscore'))
: 0;
// DOM elements
const gameCanvas = document.getElementById('game-screen');
const gameMessage = document.getElementById('message');
const scoreDisplay = document.getElementById('scores');
const muteButton = document.getElementById('mute-button');
// Mute button functionality
muteButton.addEventListener('click', () => {
isMuted = !isMuted;
muteButton.textContent = isMuted ? 'UNMUTE' : 'MUTE';
});
// Initialize the game dimensions
function updateGameDimensions() {
resizeGameCanvas();
// If game is running, render the updated game world
if (isGameActive) {
renderGameWorld();
}
}
// Initialize the game world
function startNewGame() {
// Set up the game canvas dimensions
resizeGameCanvas();
// Position the player bird
player.position.x = Math.floor(canvasWidth / 4);
player.position.y = Math.floor(canvasHeight / 2);
player.jumpVelocity = 0;
// Clear any existing obstacles
obstacles = [];
// Reset game progress
playerScore = 0;
gameSpeed = 1;
gameRefreshRate = 50;
updateScoreDisplay();
// Hide the message
gameMessage.style.display = 'none';
// Begin the game cycle
if (!isGameActive) {
isGameActive = true;
isGameEnded = false;
runGameLoop();
}
}
// Handle screen size changes
function resizeGameCanvas() {
// Determine optimal text size for current screen
const fontSize = window.innerWidth < 768 ? 14 : 16;
canvasWidth = Math.floor(window.innerWidth / (fontSize * 0.6));
canvasHeight = Math.floor(window.innerHeight / fontSize) - 2; // Leave space for header
// Ensure minimum playable area
canvasWidth = Math.max(canvasWidth, 20);
canvasHeight = Math.max(canvasHeight, 10);
}
// Handle window resize
window.addEventListener('resize', () => {
updateGameDimensions();
});
// Render the game world
function renderGameWorld() {
let displayBuffer = '';
// Build the visual representation row by row
for (let y = 0; y < canvasHeight; y++) {
for (let x = 0; x < canvasWidth; x++) {
// Draw the ground at the bottom with improved texture
if (y === canvasHeight - 1) {
// Create more interesting ground texture with patterns
if ((x + tick) % 3 === 0) {
const patternIndex = Math.floor((x + tick/2) % TERRAIN_PATTERN.length);
displayBuffer += TERRAIN_PATTERN[patternIndex];
} else {
const groundIndex = Math.floor((x + tick/2) % TERRAIN_GROUND.length);
displayBuffer += TERRAIN_GROUND[groundIndex];
}
}
// Add some ground variation for the row above the ground
else if (y === canvasHeight - 2 && Math.random() < 0.2) {
const grassIndex = Math.floor(Math.random() * 2);
displayBuffer += ['.', '\''][grassIndex];
}
// Draw obstacles (pipes) with more detail
else if (isPipeAt(x, y)) {
// Get pipe details for rendering
const pipeDetails = getPipeDetailsAt(x, y);
displayBuffer += pipeDetails;
}
// Draw the player bird with animation
else if (x === player.position.x && y === Math.floor(player.position.y)) {
// Animate bird based on game tick and velocity
const birdFrame = (player.jumpVelocity < 0) ? 0 : 1;
displayBuffer += PLAYER_BIRD[birdFrame];
}
// Fill remaining space with sky (occasionally add stars or clouds)
else {
if (Math.random() < 0.0005) {
displayBuffer += ['·', '*', '°'][Math.floor(Math.random() * 3)];
} else {
displayBuffer += EMPTY_SKY;
}
}
}
// End of row
displayBuffer += '\n';
}
// Update the visual display
gameCanvas.textContent = displayBuffer;
}
// Get detailed pipe textures for a specific position
function getPipeDetailsAt(x, y) {
for (const obstacle of obstacles) {
if (x === obstacle.x) {
// Top pipe section
if (y < obstacle.gapStart) {
// If at the bottom of top pipe (cap)
if (y === obstacle.gapStart - 1) {
return OBSTACLE_PIPE_CAP;
}
// If at the top edge of the screen (top cap)
else if (y === 0) {
return OBSTACLE_PIPE_TOP[0];
}
// Pipe body with varied texture based on position
else {
const textureIndex = (y + x) % OBSTACLE_PIPE_BODY.length;
return OBSTACLE_PIPE_BODY[textureIndex];
}
}
// Bottom pipe section
else if (y > obstacle.gapStart + obstacle.gapSize) {
// If at the top of bottom pipe (cap)
if (y === obstacle.gapStart + obstacle.gapSize + 1) {
return OBSTACLE_PIPE_TOP_CAP;
}
// Pipe body with varied texture
else {
const textureIndex = (y + x) % OBSTACLE_PIPE_BODY.length;
return OBSTACLE_PIPE_BODY[textureIndex];
}
}
}
}
return EMPTY_SKY;
}
// Check if there's a pipe at the given position
function isPipeAt(x, y) {
for (const obstacle of obstacles) {
if (x === obstacle.x) {
// Position is within pipe if it's outside the gap
return y < obstacle.gapStart || y > obstacle.gapStart + obstacle.gapSize;
}
}
return false;
}
// Update game physics and state
function updateGameState() {
// Apply gravity to player
player.jumpVelocity += 0.5;
player.position.y += player.jumpVelocity / 5;
// Generate new obstacles with consistent but challenging gaps
const baseObstacleInterval = 45; // Base interval between pipes
const obstacleInterval = Math.round(baseObstacleInterval / gameSpeed);
if (tick % obstacleInterval === 0) {
// Set appropriate gap size based on speed (smaller as speed increases)
// But ensure minimum playable size
const minGapSize = Math.max(8 - Math.floor(gameSpeed), 5);
// Calculate appropriate gap position
let gapStart;
if (obstacles.length > 0) {
// Reference the last obstacle for position continuity
const lastObstacle = obstacles[obstacles.length - 1];
const lastGapCenter = lastObstacle.gapStart + (lastObstacle.gapSize / 2);
// Calculate max possible displacement based on physics and distance
// Maximum vertical movement possible given horizontal distance and jump physics
const horizontalDistance = canvasWidth - lastObstacle.x;
const maxVerticalTravel = Math.min(
horizontalDistance * 0.4, // Limit based on horizontal distance
canvasHeight / 3 // Hard limit to prevent impossible jumps
);
// Apply controlled randomness within physically possible range
const verticalShift = (Math.random() * (maxVerticalTravel * 2)) - maxVerticalTravel;
// Calculate new gap center position
let newGapCenter = lastGapCenter + verticalShift;
// Ensure gap is within bounds
newGapCenter = Math.max(
minGapSize / 2 + 1, // Keep gap away from top edge
Math.min(
newGapCenter,
canvasHeight - (minGapSize / 2) - 2 // Keep gap away from bottom edge
)
);
// Set the final gap start position
gapStart = Math.floor(newGapCenter - (minGapSize / 2));
} else {
// First obstacle - position near the middle with slight randomness
const middlePosition = Math.floor(canvasHeight / 2) - Math.floor(minGapSize / 2);
gapStart = middlePosition + Math.floor(Math.random() * 5) - 2; // Small random variance
}
// Final bounds check to avoid the gap being too close to edges
gapStart = Math.max(1, Math.min(gapStart, canvasHeight - minGapSize - 2));
// Add the new obstacle
obstacles.push({
x: canvasWidth,
gapStart: gapStart,
gapSize: minGapSize,
passed: false
});
}
// Move and process all obstacles
for (let i = 0; i < obstacles.length; i++) {
const obstacle = obstacles[i];
// Speed increases with gameSpeed but maintains consistent movement
obstacle.x -= Math.ceil(gameSpeed);
// Award points for successful navigation
if (!obstacle.passed && obstacle.x < player.position.x) {
obstacle.passed = true;
playerScore++;
updateScoreDisplay();
// More gradual speed increase
if (playerScore > 0 && playerScore % 50 === 0) {
// Very small speed increment
gameSpeed = Math.min(gameSpeed + 0.05, 1.5);
}
}
// Remove obstacles that have moved offscreen
if (obstacle.x < -1) { // Adjust to -1 to ensure smooth exit
obstacles.splice(i, 1);
i--;
}
}
// Detect collisions
if (detectCollision()) {
isGameActive = false;
isGameEnded = true;
// Play blorp sound on collision
playBlorp();
// Update high score if needed
if (playerScore > bestScore) {
bestScore = playerScore;
storage.setItem('asciibird-highscore', bestScore);
updateScoreDisplay();
}
// Show end-game message with minimalist button
gameMessage.innerHTML = 'GAME OVER<div class="start-button">PLAY AGAIN</div>';
gameMessage.style.display = 'block';
}
tick++;
}
// Detect player collisions with obstacles or boundaries
function detectCollision() {
const playerY = Math.floor(player.position.y);
// Check for ground collision
if (playerY >= canvasHeight - 1) {
return true;
}
// Check for ceiling collision
if (playerY < 0) {
return true;
}
// Check for obstacle collisions with better hit detection
for (const obstacle of obstacles) {
// Bird hitbox (slightly more forgiving)
if (obstacle.x === player.position.x) {
// Check if player is outside the gap (collision)
if (playerY < obstacle.gapStart || playerY > obstacle.gapStart + obstacle.gapSize) {
return true;
}
}
}
// No collisions detected
return false;
}
// Update the score display
function updateScoreDisplay() {
scoreDisplay.textContent = `SCORE: ${playerScore} | HIGH: ${bestScore}`;
}
// Main game loop
function runGameLoop() {
if (!isGameActive) return;
updateGameState();
renderGameWorld();
// Schedule next frame with consistent timing
setTimeout(() => {
requestAnimationFrame(runGameLoop);
}, gameRefreshRate);
}
// Handle input (click/tap/keyboard)
function handleInput() {
// Play the bleep sound
playBleep();
// Make the bird jump by giving it upward velocity
player.jumpVelocity = -3.5;
// Start the game if it's not active yet
if (!isGameActive && !isGameEnded) {
startNewGame();
}
// Restart the game if it ended
else if (isGameEnded) {
startNewGame();
}
}
// Add event listeners for different input methods
window.addEventListener('mousedown', handleInput);
window.addEventListener('touchstart', (e) => {
e.preventDefault(); // Prevent scrolling
handleInput();
});
window.addEventListener('keydown', (e) => {
// Allow using space, up arrow, or enter to jump
if (e.code === 'Space' || e.code === 'ArrowUp' || e.code === 'Enter') {
e.preventDefault();
handleInput();
}
});
// Initialize the game when page loads
window.addEventListener('load', () => {
updateGameDimensions();
renderGameWorld();
// Show ready message with minimalist button
gameMessage.innerHTML = 'CLICK TO START<div class="start-button">PLAY</div>';
gameMessage.style.display = 'block';
});
</script>
</body>
</html>