-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic Doodle Jump Game
225 lines (190 loc) · 5.42 KB
/
Basic Doodle Jump Game
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
<!DOCTYPE html>
<html>
<head>
<title>Basic Doodle Jump HTML Game</title>
<meta charset="UTF-8">
<style>
html, body {
height: 100%;
margin: 0;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas width="375" height="667" id="game"></canvas>
<script>
const canvas = document.getElementById('game');
const context = canvas.getContext('2d');
// width and height of each platform and where platforms start
const platformWidth = 65;
const platformHeight = 20;
const platformStart = canvas.height - 50;
// player physics
const gravity = 0.33;
const drag = 0.3;
const bounceVelocity = -12.5;
// minimum and maximum vertical space between each platform
let minPlatformSpace = 15;
let maxPlatformSpace = 20;
// information about each platform. the first platform starts in the
// bottom middle of the screen
let platforms = [{
x: canvas.width / 2 - platformWidth / 2,
y: platformStart
}];
// get a random number between the min (inclusive) and max (exclusive)
function random(min, max) {
return Math.random() * (max - min) + min;
}
// fill the initial screen with platforms
let y = platformStart;
while (y > 0) {
// the next platform can be placed above the previous one with a space
// somewhere between the min and max space
y -= platformHeight + random(minPlatformSpace, maxPlatformSpace);
// a platform can be placed anywhere 25px from the left edge of the canvas
// and 25px from the right edge of the canvas (taking into account platform
// width).
// however the first few platforms cannot be placed in the center so
// that the player will bounce up and down without going up the screen
// until they are ready to move
let x;
do {
x = random(25, canvas.width - 25 - platformWidth);
} while (
y > canvas.height / 2 &&
x > canvas.width / 2 - platformWidth * 1.5 &&
x < canvas.width / 2 + platformWidth / 2
);
platforms.push({ x, y });
}
// the doodle jumper
const doodle = {
width: 40,
height: 60,
x: canvas.width / 2 - 20,
y: platformStart - 60,
// velocity
dx: 0,
dy: 0
};
// keep track of player direction and actions
let playerDir = 0;
let keydown = false;
let prevDoodleY = doodle.y;
// game loop
function loop() {
requestAnimationFrame(loop);
context.clearRect(0,0,canvas.width,canvas.height);
// apply gravity to doodle
doodle.dy += gravity;
// if doodle reaches the middle of the screen, move the platforms down
// instead of doodle up to make it look like doodle is going up
if (doodle.y < canvas.height / 2 && doodle.dy < 0) {
platforms.forEach(function(platform) {
platform.y += -doodle.dy;
});
// add more platforms to the top of the screen as doodle moves up
while (platforms[platforms.length - 1].y > 0) {
platforms.push({
x: random(25, canvas.width - 25 - platformWidth),
y: platforms[platforms.length - 1].y - (platformHeight + random(minPlatformSpace, maxPlatformSpace))
})
// add a bit to the min/max platform space as the player goes up
minPlatformSpace += 0.5;
maxPlatformSpace += 0.5;
// cap max space
maxPlatformSpace = Math.min(maxPlatformSpace, canvas.height / 2);
}
}
else {
doodle.y += doodle.dy;
}
// only apply drag to horizontal movement if key is not pressed
if (!keydown) {
if (playerDir < 0) {
doodle.dx += drag;
// don't let dx go above 0
if (doodle.dx > 0) {
doodle.dx = 0;
playerDir = 0;
}
}
else if (playerDir > 0) {
doodle.dx -= drag;
if (doodle.dx < 0) {
doodle.dx = 0;
playerDir = 0;
}
}
}
doodle.x += doodle.dx;
// make doodle wrap the screen
if (doodle.x + doodle.width < 0) {
doodle.x = canvas.width;
}
else if (doodle.x > canvas.width) {
doodle.x = -doodle.width;
}
// draw platforms
context.fillStyle = 'green';
platforms.forEach(function(platform) {
context.fillRect(platform.x, platform.y, platformWidth, platformHeight);
// make doodle jump if it collides with a platform from above
if (
// doodle is falling
doodle.dy > 0 &&
// doodle was previous above the platform
prevDoodleY + doodle.height <= platform.y &&
// doodle collides with platform
// (Axis Aligned Bounding Box [AABB] collision check)
doodle.x < platform.x + platformWidth &&
doodle.x + doodle.width > platform.x &&
doodle.y < platform.y + platformHeight &&
doodle.y + doodle.height > platform.y
) {
// reset doodle position so it's on top of the platform
doodle.y = platform.y - doodle.height;
doodle.dy = bounceVelocity;
}
});
// draw doodle
context.fillStyle = 'yellow';
context.fillRect(doodle.x, doodle.y, doodle.width, doodle.height);
prevDoodleY = doodle.y;
// remove any platforms that have gone offscreen
platforms = platforms.filter(function(platform) {
return platform.y < canvas.height;
})
}
// listen to keyboard events to move doodle
document.addEventListener('keydown', function(e) {
// left arrow key
if (e.which === 37) {
keydown = true;
playerDir = -1;
doodle.dx = -3;
}
// right arrow key
else if (e.which === 39) {
keydown = true;
playerDir = 1;
doodle.dx = 3;
}
});
document.addEventListener('keyup', function(e) {
keydown = false;
});
// start the game
requestAnimationFrame(loop);
</script>
</body>
</html>