1
+ /*
2
+ First time? Check out the tutorial game:
3
+ https://sprig.hackclub.com/gallery/getting_started
4
+
5
+ @title : Sprig Dash
6
+ @author :
7
+ @tags : []
8
+ @addedOn : 2024-00-00
9
+ */
10
+
11
+ const player = "p" ;
12
+ const bg = "b" ;
13
+ const enemy = "e" ;
14
+ const bird = "i" ;
15
+
16
+ setLegend (
17
+ [ player , bitmap `
18
+ 6666666666666666
19
+ 6666666666666666
20
+ 6666666666666666
21
+ 666FFFFFFFFFF666
22
+ 666FFFFFFFFFF666
23
+ 666FFFFFFFFFF666
24
+ 666FFF9999FFF666
25
+ 666FFF9999FFF666
26
+ 666FFF9999FFF666
27
+ 666FFF9999FFF666
28
+ 666FFFFFFFFFF666
29
+ 666FFFFFFFFFF666
30
+ 666FFFFFFFFFF666
31
+ 6666666666666666
32
+ 6666666666666666
33
+ 6666666666666666` ] ,
34
+ [ bg , bitmap `
35
+ 7777777777777777
36
+ 7777777777777777
37
+ 7777777777777777
38
+ 7777777777777777
39
+ 7777777777777777
40
+ 7777777777777777
41
+ 7777777777777777
42
+ 7777777777777777
43
+ 7777777777777777
44
+ 7777777777777777
45
+ 7777777777777777
46
+ 7777777777777777
47
+ 7777777777777777
48
+ 7777777777777777
49
+ 7777777777777777
50
+ 7777777777777777` ] ,
51
+ [ enemy , bitmap `
52
+ ................
53
+ ................
54
+ ................
55
+ ................
56
+ ................
57
+ ................
58
+ ................
59
+ ................
60
+ .......55.......
61
+ ......5555......
62
+ .....555555.....
63
+ ....55555555....
64
+ ...5555555555...
65
+ ..555555555555..
66
+ .55555555555555.
67
+ 5555555555555555` ] ,
68
+ [ bird , bitmap `
69
+ 333..........333
70
+ .333........333.
71
+ ..333......333..
72
+ ...333....333...
73
+ ....333..333....
74
+ .....333333.....
75
+ ......3333......
76
+ .......33.......
77
+ ................
78
+ ................
79
+ ................
80
+ ................
81
+ ................
82
+ ................
83
+ ................
84
+ ................` ]
85
+ )
86
+
87
+ setBackground ( bg )
88
+
89
+ setSolids ( [ player ] )
90
+
91
+ let level = 0
92
+ const levels = [
93
+ map `
94
+ .........................
95
+ .........................
96
+ .........................
97
+ .........................
98
+ .........................
99
+ .........................
100
+ .........................
101
+ .........................
102
+ ....p...e....e....e......`
103
+ ]
104
+
105
+ setMap ( levels [ level ] )
106
+
107
+ setPushables ( {
108
+ [ player ] : [ player ]
109
+ } )
110
+
111
+ const jumpHeight = 3 ;
112
+ const moveSpeed = 1 ;
113
+ let gameRunning = false ;
114
+ let score = 0 ;
115
+ let coolDown = false ;
116
+
117
+ displayScore ( ) ;
118
+ setTimeout ( ( ) => {
119
+ gameRunning = true ;
120
+ } , 3000 ) ;
121
+
122
+ onInput ( "j" , ( ) => {
123
+ if ( ! coolDown && gameRunning ) {
124
+ getFirst ( player ) . y -= jumpHeight
125
+ coolDown = true ;
126
+ }
127
+ } )
128
+
129
+ afterInput ( ( ) => {
130
+ setTimeout ( ( ) => {
131
+ if ( gameRunning ) {
132
+ getFirst ( player ) . y += jumpHeight
133
+ coolDown = false ;
134
+ }
135
+ } , 300 ) ;
136
+ } )
137
+
138
+
139
+ setInterval ( ( ) => {
140
+ if ( gameRunning ) {
141
+ generateEnemy ( ) ;
142
+ }
143
+ } , 1500 )
144
+
145
+ setInterval ( ( ) => {
146
+ if ( score >= 5 ) {
147
+ generateEnemyBird ( ) ;
148
+ }
149
+ } , 2000 )
150
+
151
+ setInterval ( ( ) => {
152
+ if ( gameRunning ) {
153
+ moveEnemies ( ) ;
154
+ }
155
+ } , 150 ) ;
156
+
157
+ setInterval ( ( ) => {
158
+ if ( gameRunning ) {
159
+ checkOffScreen ( ) ;
160
+ }
161
+ } , 10 )
162
+
163
+ setInterval ( ( ) => {
164
+ if ( gameRunning ) {
165
+ checkCollision ( ) ;
166
+ }
167
+ } , 10 )
168
+
169
+ function checkOffScreen ( ) {
170
+ const enemies = getAll ( enemy ) ;
171
+ const birds = getAll ( bird ) ;
172
+ enemies . forEach ( e => {
173
+ if ( e . x == 0 ) {
174
+ score ++ ;
175
+ e . remove ( )
176
+ }
177
+ } ) ;
178
+
179
+ birds . forEach ( b => {
180
+ if ( b . x == 0 ) {
181
+ score ++ ;
182
+ b . remove ( )
183
+ }
184
+ } ) ;
185
+ displayScore ( ) ;
186
+ }
187
+
188
+ function moveEnemies ( ) {
189
+ const enemies = getAll ( enemy ) ;
190
+ const birds = getAll ( bird ) ;
191
+ enemies . forEach ( e => {
192
+ e . x -= moveSpeed ;
193
+ } ) ;
194
+
195
+ birds . forEach ( b => {
196
+ b . x -= moveSpeed ;
197
+ } ) ;
198
+ }
199
+
200
+ function deleteEnemies ( ) {
201
+ const enemies = getAll ( enemy ) ;
202
+ const birds = getAll ( bird ) ;
203
+
204
+ enemies . forEach ( e => {
205
+ e . remove ( ) ;
206
+ } )
207
+
208
+ birds . forEach ( b => {
209
+ b . remove ( ) ;
210
+ } )
211
+ }
212
+
213
+ function generateEnemy ( ) {
214
+ let amount = Math . floor ( Math . random ( ) * ( 2 - 1 + 1 ) + 1 ) ;
215
+
216
+ for ( let i = 0 ; i < amount ; i ++ ) {
217
+ addSprite ( 24 - i , 8 , enemy ) ;
218
+ }
219
+ }
220
+
221
+ function generateEnemyBird ( ) {
222
+ addSprite ( 24 , 6 , bird ) ;
223
+ }
224
+
225
+ function checkCollision ( ) {
226
+ const enemies = getAll ( enemy ) ;
227
+ const birds = getAll ( bird ) ;
228
+ const playerObj = getFirst ( player ) ;
229
+ let removed = false ;
230
+
231
+ enemies . forEach ( enemy => {
232
+ if ( getTile ( enemy . x , enemy . y ) . length > 1 ) {
233
+ let a = getTile ( enemy . x , enemy . y ) [ 0 ] [ '_type' ]
234
+ let b = getTile ( enemy . x , enemy . y ) [ 1 ] [ '_type' ]
235
+
236
+ if ( a == player || b == player ) {
237
+ playerObj . remove ( ) ;
238
+ onGameOver ( ) ;
239
+ removed = true ;
240
+ return ;
241
+ }
242
+ }
243
+ } ) ;
244
+
245
+ if ( ! removed ) {
246
+ birds . forEach ( bird => {
247
+ if ( getTile ( bird . x , bird . y ) . length > 1 ) {
248
+ let a = getTile ( bird . x , bird . y ) [ 0 ] [ '_type' ]
249
+ let b = getTile ( bird . x , bird . y ) [ 1 ] [ '_type' ]
250
+
251
+ if ( a == player || b == player ) {
252
+ playerObj . remove ( ) ;
253
+ onGameOver ( ) ;
254
+ return ;
255
+ }
256
+ }
257
+ } ) ;
258
+ }
259
+ }
260
+
261
+ function onGameOver ( ) {
262
+ gameRunning = false ;
263
+ deleteEnemies ( ) ;
264
+ displayGameOver ( ) ;
265
+ }
266
+
267
+ function displayScore ( ) {
268
+ clearText ( ) ;
269
+ addText ( `${ score } ` , {
270
+ x : 1 ,
271
+ y : 5 ,
272
+ color : color `2`
273
+ } )
274
+ }
275
+
276
+ function displayGameOver ( ) {
277
+ clearText ( )
278
+ addText ( "Game Over" , {
279
+ x : 5 ,
280
+ y : 6 ,
281
+ color : color `3`
282
+ } )
283
+
284
+ addText ( `Score: ${ score } ` , {
285
+ x : 5 ,
286
+ y : 8 ,
287
+ color : color `2`
288
+ } )
289
+ }
0 commit comments