Skip to content

Commit 62970b5

Browse files
[Sprig App] Flappy bird with animated bird, gravity and sound (#2807)
* Sprig App - Flappy bird with animated bird, gravity and sound * Update Flappy-bird-with-animated-bird,-gravity-and-sound.js --------- Co-authored-by: Mare Cosmin <147330889+Cosmin-Mare@users.noreply.github.com>
1 parent 073ab9a commit 62970b5

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
First time? Check out the tutorial game:
3+
https://sprig.hackclub.com/gallery/getting_started
4+
5+
@title: Flappy Bird with Animated Bird, Gravity and Sound
6+
@tags: []
7+
@author: Yug Khandelwal
8+
@addedOn: 2025-01-22
9+
*/
10+
11+
const flapUp = "u";
12+
const flapDown = "d"
13+
const obstacle = "o";
14+
const background = "b";
15+
const gameOverMelody = tune`
16+
37.5: E4-37.5 + F4-37.5,
17+
37.5: G4-37.5 + A4-37.5,
18+
37.5: B4-37.5 + C5-37.5,
19+
37.5: D5-37.5,
20+
37.5: D5-37.5,
21+
37.5: D5-37.5 + C5-37.5 + B4-37.5 + A4-37.5,
22+
37.5: G4-37.5,
23+
37.5: G4-37.5 + A4-37.5,
24+
37.5: B4-37.5 + C5-37.5 + D5-37.5,
25+
37.5: E5-37.5 + F5-37.5,
26+
37.5: F5-37.5,
27+
37.5: F5-37.5 + E5-37.5,
28+
37.5: E5-37.5 + D5-37.5 + C5-37.5,
29+
37.5: C5-37.5 + B4-37.5 + A4-37.5,
30+
37.5: G4-37.5 + F4-37.5,
31+
37.5: F4-37.5 + E4-37.5,
32+
37.5: E4-37.5,
33+
37.5: E4-37.5 + F4-37.5,
34+
37.5: F4-37.5 + G4-37.5,
35+
37.5: G4-37.5 + A4-37.5 + B4-37.5 + C5-37.5,
36+
37.5: C5-37.5,
37+
37.5: B4-37.5 + A4-37.5 + G4-37.5 + F4-37.5 + E4-37.5,
38+
37.5: E4-37.5,
39+
37.5: F4-37.5,
40+
37.5: F4-37.5,
41+
37.5: F4-37.5 + G4-37.5 + A4-37.5,
42+
37.5: A4-37.5 + B4-37.5 + C5-37.5,
43+
37.5: C5-37.5 + D5-37.5,
44+
37.5: D5-37.5 + E5-37.5 + F5-37.5 + G5-37.5,
45+
37.5: G5-37.5 + A5-37.5,
46+
37.5: G5-37.5 + F5-37.5 + E5-37.5 + D5-37.5,
47+
37.5: C5-37.5 + B4-37.5`;
48+
const keyTune = tune`
49+
500: G4-500,
50+
15500`;
51+
52+
const bird = () => getFirst("u") || getFirst("d");
53+
54+
setLegend(
55+
[flapUp, bitmap`
56+
................
57+
................
58+
00..............
59+
220..00000......
60+
62200666600.....
61+
062206666060....
62+
0662206660250...
63+
.066606660250...
64+
..060666600000..
65+
...006660999990.
66+
....0666099990..
67+
.....006600000..
68+
.......000......
69+
................
70+
................
71+
................`],
72+
[flapDown, bitmap`
73+
................
74+
................
75+
................
76+
....00000.......
77+
..00666600......
78+
.0206666060.....
79+
022206660250....
80+
026606660250....
81+
2260666600000...
82+
26606660999990..
83+
6000666099990...
84+
60..006600000...
85+
0.....000.......
86+
................
87+
................
88+
................`],
89+
[obstacle, bitmap`
90+
LLLLLLLLLLLLLLLL
91+
L333L333333L333L
92+
L333L333333L333L
93+
L333L333333L333L
94+
LLLLLLLLLLLLLLLL
95+
L3333333L333333L
96+
L3333333L333333L
97+
L3333333L333333L
98+
LLLLLLLLLLLLLLLL
99+
L333L333333L333L
100+
L333L333333L333L
101+
L333L333333L333L
102+
LLLLLLLLLLLLLLLL
103+
L3333333L333333L
104+
L3333333L333333L
105+
L3333333L333333L`],
106+
[background, bitmap`
107+
7777777777777777
108+
7777777777777777
109+
7777777777777777
110+
7777777777777777
111+
7777777777777777
112+
7777777777777777
113+
7777777777777777
114+
7777777777777777
115+
7777777777777777
116+
7777777777777777
117+
7777777777777777
118+
7777777777777777
119+
7777777777777777
120+
7777777777777777
121+
7777777777777777
122+
7777777777777777`]
123+
);
124+
let level = 0;
125+
const levels = [
126+
map`
127+
.......o..
128+
.......o..
129+
.......o..
130+
.......o..
131+
u.........
132+
.......o..
133+
.......o..
134+
.......o..`
135+
]
136+
137+
setMap(levels[level]);
138+
setBackground(background);
139+
setPushables({
140+
[flapUp]: [],
141+
[flapDown]: [],
142+
});
143+
var isGameOver = false;
144+
var speed = 250;
145+
var gap = 4;
146+
var count = 0;
147+
var score = 0;
148+
onInput("s", () => {
149+
const bird = () => getFirst("u") || getFirst("d");
150+
if (!isGameOver) {
151+
playTune(keyTune);
152+
bird().y += 1;
153+
}
154+
});
155+
onInput("w", () => {
156+
const bird = () => getFirst("u") || getFirst("d");
157+
if (!isGameOver) {
158+
playTune(keyTune);
159+
bird().y -= 1;
160+
}
161+
});
162+
163+
function generateObstacle() {
164+
gap = Math.floor(Math.random() * 8);
165+
for (let y = 0; y < 8; y++) {
166+
if (y != gap) {
167+
addSprite(7, y, obstacle);
168+
}
169+
}
170+
score++;
171+
}
172+
173+
function gameLoop() {
174+
addText(`Score: ${score}`, { x: 10, y: 1, color: color`2` })
175+
176+
if (count % 4 == 0) {
177+
if (bird().y == 8) {} else {
178+
bird().y += 1;
179+
};
180+
}
181+
getAll(obstacle).forEach((o) => {
182+
if (o.x == 0) {
183+
o.remove();
184+
} else {
185+
o.x -= 1;
186+
};
187+
});
188+
if (getAll(obstacle).length == 0) {
189+
generateObstacle();
190+
}
191+
bird().type = [flapUp, flapDown][count % 2];
192+
if (getFirst(obstacle).x == bird().x && bird().y != gap) {
193+
gameOver();
194+
}
195+
count += 1;
196+
197+
198+
speed -= (250 - speed);
199+
if (!isGameOver) {
200+
setTimeout(gameLoop, speed);
201+
}
202+
}
203+
204+
function gameOver() {
205+
clearText();
206+
isGameOver = true;
207+
setMap(map`
208+
..........
209+
..........
210+
..........
211+
..........
212+
..........
213+
..........
214+
..........
215+
..........`);
216+
playTune(gameOverMelody);
217+
addText("Game over!", { x: 5, y: 7, color: color`2` });
218+
addText(`Score: ${score}`, { x: 5, y: 8, color: color`2` });
219+
}
220+
gameLoop();

0 commit comments

Comments
 (0)