-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoodlejump.js
262 lines (243 loc) · 5.81 KB
/
doodlejump.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
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
// board
let board;
let boardWidth = 360;
let boardHeight = 576;
let context;
let fps = 60;
let leftBtn;
let rightBtn;
let resetBtn;
// doodler
let doodlerWidth = 46;
let doodlerHeight = 46;
let doodlerX = boardWidth / 2 - doodlerWidth / 2;
let doodlerY = boardHeight * (7 / 8) - doodlerHeight;
let doodlerLeftImage;
let doodlerRightImage;
let doodler = {
img: null,
x: doodlerX,
y: doodlerY,
width: doodlerWidth,
height: doodlerHeight,
};
// physics
let velocityX = 0;
let velocityY = 0; // doodler jump speed
let initialVelocityY = -8; // starting velocity Y
let gravity = 0.4;
// platforms
let platformArray = [];
let platformWidth = 60;
let platformHeight = 18;
let platformImg;
// score
let score = 0;
let maxScore = 0;
let gameOver = false;
window.onload = function () {
// draw the board
board = document.getElementById("board");
board.height = boardHeight;
board.width = boardWidth;
context = board.getContext("2d");
leftBtn = document.getElementById("left");
rightBtn = document.getElementById("right");
resetBtn = document.getElementById("reset");
// draw the doodler
/*
context.fillStyle = "green";
context.fillRect(doodler.x, doodler.y, doodler.width, doodler.height);
*/
//load images
doodlerRightImage = new Image();
doodlerRightImage.src = "doodler-right.png";
doodler.img = doodlerRightImage;
doodlerRightImage.onload = function () {
context.drawImage(
doodler.img,
doodler.x,
doodler.y,
doodler.width,
doodler.height
);
};
doodlerLeftImage = new Image();
doodlerLeftImage.src = "doodler-left.png";
platformImg = new Image();
platformImg.src = "platform.png";
velocityY = initialVelocityY;
placePlatforms();
setInterval(update, 1000 / fps);
document.addEventListener("keydown", moveDoodler);
leftBtn.onclick = function () {
velocityX = -4;
doodler.img = doodlerLeftImage;
};
rightBtn.onclick = function () {
velocityX = 4;
doodler.img = doodlerRightImage;
};
resetBtn.onclick = function () {
if (gameOver) {
doodler = {
img: doodlerRightImage,
x: doodlerX,
y: doodlerY,
width: doodlerWidth,
height: doodlerHeight,
};
velocityX = 0;
velocityY = initialVelocityY;
score = 0;
maxScore = 0;
gameOver = false;
placePlatforms();
}
};
};
function update() {
if (gameOver) {
return;
}
context.clearRect(0, 0, board.width, board.height);
// doodler
doodler.x += velocityX;
if (doodler.x > boardWidth) {
doodler.x = 0;
} else if (doodler.x + doodler.width < 0) {
doodler.x = boardWidth;
}
context.drawImage(
doodler.img,
doodler.x,
doodler.y,
doodler.width,
doodler.height
);
velocityY += gravity;
doodler.y += velocityY;
if (doodler.y - 20 > board.height) {
gameOver = true;
}
// platform
for (let i = 0; i < platformArray.length; i++) {
let platform = platformArray[i];
if (velocityY < 0 && doodler.y < boardHeight * (3 / 4)) {
platform.y -= initialVelocityY; // slide platform down
}
if (detectCollision(doodler, platform) && velocityY >= 0) {
velocityY = initialVelocityY; // jump
}
context.drawImage(
platform.img,
platform.x,
platform.y,
platform.width,
platform.height
);
}
// create platforms and add new ones
while (platformArray.length > 0 && platformArray[0].y >= boardHeight) {
platformArray.shift(); // remove first element form the array
newPlatform(); // replace first element with new one to create new platform
}
// score
updateScore();
context.fillStyle = "black";
context.font = "16px sans-serif";
context.fillText(score, 5, 20);
if (gameOver) {
context.fillText(
"Game Over: Press 'Space' to Restart",
boardWidth / 7,
(boardHeight * 7) / 8
);
}
}
function moveDoodler(e) {
if (e.code == "ArrowRight" || e.code == "KeyD") {
velocityX = 4;
doodler.img = doodlerRightImage;
} else if (e.code == "ArrowLeft" || e.code == "KeyA") {
velocityX = -4;
doodler.img = doodlerLeftImage;
} else if (e.code == "Space" && gameOver) {
// reset
doodler = {
img: doodlerRightImage,
x: doodlerX,
y: doodlerY,
width: doodlerWidth,
height: doodlerHeight,
};
velocityX = 0;
velocityY = initialVelocityY;
score = 0;
maxScore = 0;
gameOver = false;
placePlatforms();
}
}
function placePlatforms() {
platformArray = [];
//starting platform
let platform = {
img: platformImg,
x: boardWidth / 2,
y: boardHeight - 75,
width: platformWidth,
height: platformHeight,
};
platformArray.push(platform);
// platform = {
// img: platformImg,
// x: boardWidth / 2,
// y: boardHeight - 150,
// width: platformWidth,
// height: platformHeight,
// };
// platformArray.push(platform);
for (let i = 0; i < 6; i++) {
let randomX = Math.floor((Math.random() * boardWidth * 3) / 4);
let platform = {
img: platformImg,
x: randomX,
y: boardHeight - 75 * i - 170,
width: platformWidth,
height: platformHeight,
};
platformArray.push(platform);
}
}
function newPlatform() {
let randomX = Math.floor(Math.random() * boardWidth * (3 / 4));
let platform = {
img: platformImg,
x: randomX,
y: -platformHeight,
width: platformWidth,
height: platformHeight,
};
platformArray.push(platform);
}
function detectCollision(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
function updateScore() {
let points = Math.floor(50 * Math.random());
if (velocityY < 0) {
// negative going up
maxScore += points;
if (score < maxScore) {
score = maxScore;
}
} else if (velocityY >= 0) {
maxScore -= points;
}
}