/
doodle-jump.html
273 lines (240 loc) · 8.05 KB
/
doodle-jump.html
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
263
264
265
266
267
268
269
270
271
272
273
<html>
<body>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function JumpGame(OUTER_DIV_ID,
WIN_WIDTH, WIN_HEIGHT, TOTAL_BOARD_HEIGHT,
BLOCK_WIDTH, BLOCK_HEIGHT, DUDE_WIDTH, DUDE_HEIGHT) {
// derived from parameters
var HALF_BLOCK_WIDTH = BLOCK_WIDTH / 2;
var HALF_WIN_WIDTH = WIN_WIDTH / 2;
var INNER_DIV_ID = OUTER_DIV_ID + '_inner';
// not passed in
var SAD_FACE_IMG_URL = 'http://2.bp.blogspot.com/_xI_VEnvwTzw/SKEitYFtr4I/AAAAAAAAA7E/WzF3gz7MqsU/s400/080807a.jpg';
var HEIGHT_TIL_SCROLL = 150;
var SECOND_HEIGHT_TIL_SCROLL = 225;
var THIRD_HEIGHT_TIL_SCROLL = 260;
var MAX_ACCEL = 8;
var PUSH_ACCEL = 6;
var JUMP_ACCEL = 16;
var ROW_HEIGHT = 12;
var DRAW_FREQ = 50
var outerDiv = $('#' + OUTER_DIV_ID);
var gameDiv = $('#' + INNER_DIV_ID);
var blocks = [];
function prepend_divs_to_body() {
if (!gameDiv.length) {
gameDiv = $('<div id="' + INNER_DIV_ID + '" style="position: absolute; width: ' + WIN_WIDTH +
'px; height: ' + TOTAL_BOARD_HEIGHT + 'px; background-color: #aaf"></div>');
}
outerDiv = $('<div id="' + OUTER_DIV_ID + '" style="overflow: hidden; position: absolute; width: ' + WIN_WIDTH +
'px; height: ' + WIN_HEIGHT + 'px; border: 1px solid #f00;"></div>');
outerDiv.append(gameDiv);
$('body').prepend(outerDiv);
}
function initialize_blocks() {
var b = makeBlock(0, 0);
blocks.push(b);
gameDiv.append(b);
var curY = 0;
while (curY < TOTAL_BOARD_HEIGHT) {
var steps = Math.floor(Math.random() * 9);
curY += steps * ROW_HEIGHT;
var b = makeBlock(
Math.floor(Math.random() * (WIN_WIDTH - BLOCK_WIDTH)) - HALF_WIN_WIDTH + BLOCK_WIDTH / 2,
curY);
blocks.push(b);
gameDiv.append(b);
}
}
function adjustForBlocks(dude) {
var dude_left = dude.x - DUDE_WIDTH / 2;
var dude_right = dude.x + DUDE_WIDTH / 2;
for (var i = lowest_block_index; i < blocks.length; i++) {
var this_block = blocks[i];
var block_top = this_block.y + BLOCK_HEIGHT;
if (block_top > current_bottom + WIN_HEIGHT) {
break;
}
if (dude.y > block_top - 9 && dude.y < block_top
&& dude_left < this_block.x + HALF_BLOCK_WIDTH
&& dude_right > this_block.x - HALF_BLOCK_WIDTH)
{
this_block.hit();
dude.jump(block_top);
if (this_block.hp < 1) {
blocks.splice(i, 1);
this_block.remove();
}
break;
}
}
}
function setY(yPx) {
this.y = yPx;
this.css('top', (TOTAL_BOARD_HEIGHT - this.height() - yPx) + 'px');
}
function setX(xPx) {
this.x = xPx;
this.css('left', (WIN_WIDTH - this.width()) / 2 + xPx + 'px');
}
function makeBlock(x, y) {
var block = $('<div id="win" style="position: absolute; width: 60px; height: 12px; background-color: #afa"></div>');
block.hp = 2;
block.hit = function() {
this.hp -= 1;
if (this.hp == 1) {
this.css('background-color', '');
this.css('border', '1px solid #afa');
}
};
block.setY = setY;
block.setX = setX;
block.setX(x);
block.setY(y);
return block;
}
function makeDude() {
var dude = $('<div id="win" style="position: absolute; width: 24px; height: 24px; background-color: #faa"></div>');
dude.x = 0;
dude.y = 0;
dude.accel = 0;
dude.jump_accel = 0;
dude.setY = setY;
dude.setX = setX;
dude.moveDown = function() {
if (this.y > 0) {
this.y -= 1;
this.setY(this.y);
}
};
dude.move = function() {
this.moveDown();
if (this.jump_accel > -6) {
this.jump_accel -= 1;
}
if (this.jump_accel <= 0) {
adjustForBlocks(this);
}
if (this.y > current_bottom && this.x > -HALF_WIN_WIDTH && this.x < HALF_WIN_WIDTH) {
this.y += this.jump_accel;
if (this.y > current_bottom + HEIGHT_TIL_SCROLL && this.jump_accel > 0) {
current_bottom += this.jump_accel / 2;
if (this.y > current_bottom + SECOND_HEIGHT_TIL_SCROLL) {
current_bottom += this.jump_accel / 2;
if (this.y > current_bottom + THIRD_HEIGHT_TIL_SCROLL) {
current_bottom += this.jump_accel / 2;
}
}
outerDiv.scrollTop(TOTAL_BOARD_HEIGHT - current_bottom - WIN_HEIGHT);
// moveAllBlocks(-this.jump_accel);
}
this.setY(this.y);
}
else { // game over
game_over = true;
gameDiv.remove();
// outerDiv.scrollTop(0);
var sad_img = $('<img src="' + SAD_FACE_IMG_URL + '" style="position: absolute; z-index: 5;" />');
sad_img.click(function() {
sad_img.remove();
new JumpGame(OUTER_DIV_ID, WIN_WIDTH, WIN_HEIGHT,
TOTAL_BOARD_HEIGHT,
BLOCK_WIDTH, BLOCK_HEIGHT,
DUDE_WIDTH, DUDE_HEIGHT);
});
outerDiv.prepend(sad_img);
return;
}
this.setX(this.x + this.accel);
if (this.accel < 0) {
this.accel += 1;
}
else if (this.accel > 0) {
this.accel -= 1;
}
};
dude.jump = function(y) {
this.setY(y);
this.jump_accel = JUMP_ACCEL;
};
dude.pushLeft = function() {
if (this.accel > -MAX_ACCEL) {
this.accel -= PUSH_ACCEL;
}
};
dude.pushRight = function() {
if (this.accel < MAX_ACCEL) {
this.accel += PUSH_ACCEL;
}
};
return dude;
}
var callback = function(e) {
if (!e && window.event) {
e = window.event;
}
if (e.keyCode == 37) {
dude.pushLeft();
}
else if (e.keyCode == 39) {
dude.pushRight();
}
};
function timefunc() {
dude.move();
if (!game_over) {
window.setTimeout(timefunc, DRAW_FREQ);
}
}
this.timefunc = function() {
dude.move();
return !game_over;
}
if (window.addEventListener){
window.addEventListener('keydown', callback, true);
} else if (window.attachEvent){
// mainDiv.attachEvent('keydown', callback);
outerDiv.onkeydown = callback;
} else {
alert("can't attach keydown listener");
}
prepend_divs_to_body();
initialize_blocks();
outerDiv.scrollTop(TOTAL_BOARD_HEIGHT);
var last_block_y = 0;
var current_bottom = 0;
var lowest_block_index = 0;
var game_over;
var dude = makeDude();
dude.setY(ROW_HEIGHT * 4);
dude.setX(-20);
gameDiv.append(dude);
timefunc();
}
var game = new JumpGame('win', 300, 300, 20000, 60, 12, 24, 24);
/*
function go() {
if (game.timefunc()) {
window.setTimeout(go, game.DRAW_FREQ);
}
}
go();
*/
/*
function moveAllBlocks(dy) {
for (var i = 0, len = blocks.length; i < len; i++) {
var b = blocks[i];
if (b.y < -BLOCK_HEIGHT) {
b.setX(Math.floor(Math.random() * (WIN_WIDTH - BLOCK_WIDTH)) - HALF_WIN_WIDTH + HALF_BLOCK_WIDTH);
b.setY(Math.floor(win.height() - 5 + Math.floor(Math.random() * 10)));
}
else {
b.setY(blocks[i].y + dy);
}
}
}
*/
</script>
</body>
</html>