forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravity_fun.js
230 lines (205 loc) · 5.03 KB
/
gravity_fun.js
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
/*
@title: gravity_fun
@author: phantomeniasll
@tags: ['retro']
@addedOn: 2022-12-22
*/
const player = "p";
const pipe = "l";
setLegend(
[ player, bitmap`
0000000000000000
0000000000000000
0000000000000000
000LLLLLLLLLL000
000LLLLLLLLLL000
000LL111111LL000
000LL111111LL000
000LL111111LL000
000LL111111LL000
000LL111111LL000
000LL111111LL000
000LLLLLLLLLL000
000LLLLLLLLLL000
0000000000000000
0000000000000000
0000000000000000`],
[ pipe, bitmap`
LLCC33333333CCCC
LLCC33333333CCCC
LCCC33333333CCCC
LCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC
CCCC33333333CCCC`],
);
let level = map`
p.......................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................`;
function generateLevel(xDim, yDim,xPlayer, yPlayer) {
var lvl = "";
for(var x = 0; x <xDim; x++){
var row = "";
for(var y=0; y < yDim; y++){
if(x+1 < xPlayer+1 && y < yPlayer && y != 0 && x != 0){
row += "p";}
else{
row += ".";}
}
lvl += row + "\n"
}
return lvl;
}
var height = 80;
var width = 80;
level = generateLevel(height,width,3,3);
const currentLevel = level;
setMap(currentLevel);
setSolids([]);
setPushables({
[player]: []
});
//CONSTANTS
var openingWidth = 10;
var gravity = 0.3;
var jumpStrength = 1.8;
var updateTimems = 30;
// START - PLAYER MOVEMENT CONTROLS
onInput("w", () => {
velocity = -jumpStrength;
});
// END - PLAYER MOVEMENT CONTROLS
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
var velocity = 0;
var pipes = [Math.round(width/3),Math.round(2*width/3),width-1];
var openings = [30,40,20];
var frontPipeIndex = 0;
var score = 0;
clearText();
addText("Score:"+score);
function drawPipes(){
for(var i=0;i<pipes.length;i++)
{
if(pipes[i]<width){
for(var y=0;y<height;y++){
if(Math.abs(y-openings[i]) > openingWidth)
addSprite(pipes[i],y,pipe);
}
}
}
}
function unDrawPipe(x) {
for(var y=0; y<height; y++){
clearTile(0,y);
}
}
function unDrawPipes() {
var tiles = getAll(pipe);
tiles.forEach((p)=>{clearTile(p.x,p.y);})
}
function movePipes(){
var pipe_sprites = getAll(pipe);
var respawnFrontPipe = false;
pipe_sprites.forEach(p => {
if(p.x == 0){
respawnFrontPipe = true;
}
else{
p.x -= 1;
}
});
if(respawnFrontPipe){
score += 1;
clearText();
addText("Score:"+score);
unDrawPipe(0);
openings[frontPipeIndex] = randomIntFromInterval(20, height-20);
for(var y=0;y<height;y++){
if(Math.abs(y-openings[frontPipeIndex]) > openingWidth)
addSprite(width-1,y,pipe);
}
frontPipeIndex++;
frontPipeIndex %= pipes.length;
}
}
drawPipes();
var physicUpdate = () => {
let die = false;
getAll(player).forEach(p => {
p.y += Math.floor(velocity)
if(p.y == 0 || p.y == width -3 )
{
die=true;
}
});
movePipes();
if(tilesWith(player,pipe).length != 0 || die){
score = 0;
clearText();
addText("Score:"+score);
setMap(currentLevel);
velocity = 0;
for(var i=0; i<3;i++)
{
openings[i] = randomIntFromInterval(20, height-20);
}
frontPipeIndex = 0;
unDrawPipes();
drawPipes();
}
velocity += gravity;
setTimeout(physicUpdate,updateTimems);
}
physicUpdate();
afterInput(() => {
});