Skip to content

Commit 77a7902

Browse files
committed
Added ability to drop to bottom
1 parent ff7354b commit 77a7902

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

index.html

+33-11
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@
6868
// game constants
6969
//-------------------------------------------------------------------------
7070

71-
var KEY = { ESC: 27, SPACE: 32, LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40 },
72-
DIR = { UP: 0, RIGHT: 1, DOWN: 2, LEFT: 3, MIN: 0, MAX: 3 },
71+
var KEY = { ESC: 27, SPACE: 32, LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, SHIFT: 16 },
72+
// drop all the way to BOTTOM is an action, but MAX=3 to avoid reaching
73+
// it by rotation
74+
DIR = { UP: 0, RIGHT: 1, DOWN: 2, LEFT: 3, BOTTOM:4, MIN: 0, MAX: 3 },
7375
stats = new Stats(),
7476
canvas = get('canvas'),
7577
ctx = canvas.getContext('2d'),
@@ -216,11 +218,17 @@
216218
var handled = false;
217219
if (playing) {
218220
switch(ev.keyCode) {
219-
case KEY.LEFT: actions.push(DIR.LEFT); handled = true; break;
220-
case KEY.RIGHT: actions.push(DIR.RIGHT); handled = true; break;
221-
case KEY.UP: actions.push(DIR.UP); handled = true; break;
222-
case KEY.DOWN: actions.push(DIR.DOWN); handled = true; break;
223-
case KEY.ESC: lose(); handled = true; break;
221+
case KEY.LEFT: actions.push(DIR.LEFT); handled = true; break;
222+
case KEY.RIGHT: actions.push(DIR.RIGHT); handled = true; break;
223+
case KEY.UP: actions.push(DIR.UP); handled = true; break;
224+
case KEY.DOWN: actions.push(DIR.DOWN); handled = true; break;
225+
case KEY.SHIFT:
226+
// special care to only push once, even if it stays pressed
227+
if( actions[0] != DIR.BOTTOM ) {
228+
actions.push(DIR.BOTTOM); handled = true;
229+
}
230+
break;
231+
case KEY.ESC: lose(); handled = true; break;
224232
}
225233
}
226234
else if (ev.keyCode == KEY.SPACE) {
@@ -277,10 +285,11 @@
277285

278286
function handle(action) {
279287
switch(action) {
280-
case DIR.LEFT: move(DIR.LEFT); break;
281-
case DIR.RIGHT: move(DIR.RIGHT); break;
282-
case DIR.UP: rotate(); break;
283-
case DIR.DOWN: drop(); break;
288+
case DIR.LEFT: move(DIR.LEFT); break;
289+
case DIR.RIGHT: move(DIR.RIGHT); break;
290+
case DIR.UP: rotate(); break;
291+
case DIR.DOWN: drop(); break;
292+
case DIR.BOTTOM: dropMany(); break;
284293
}
285294
}
286295

@@ -324,6 +333,19 @@
324333
}
325334
}
326335

336+
function dropMany() {
337+
while( move(DIR.DOWN) ) { }
338+
addScore(10);
339+
dropPiece();
340+
removeLines();
341+
setCurrentPiece(next);
342+
setNextPiece(randomPiece());
343+
clearActions();
344+
if (occupied(current.type, current.x, current.y, current.dir)) {
345+
lose();
346+
}
347+
}
348+
327349
function dropPiece() {
328350
eachblock(current.type, current.x, current.y, current.dir, function(x, y) {
329351
setBlock(x, y, current.type);

0 commit comments

Comments
 (0)