fixed wasd in tetris

This commit is contained in:
Boof 2022-10-21 11:17:50 +02:00 committed by GitHub
parent b33c3b60cd
commit 6746bc5c73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View File

@ -7,10 +7,10 @@ document.body.onkeydown = function( e ) {
32: 'drop'
};
var keys = {
68: 'left',
65: 'right',
83: 'down',
87: 'rotate',
65: 'left2',
68: 'right2',
83: 'down2',
87: 'rotate2',
};
if ( typeof keys[ e.keyCode ] != 'undefined' ) {
keyPress( keys[ e.keyCode ] );

View File

@ -158,6 +158,33 @@ function keyPress( key ) {
break;
}
}
function keyPress( key ) {
switch ( key ) {
case 'left2':
if ( valid( -1 ) ) {
--currentX;
}
break;
case 'right2':
if ( valid( 1 ) ) {
++currentX;
}
break;
case 'down2':
if ( valid( 0, 1 ) ) {
++currentY;
}
break;
case 'rotate2':
var rotated = rotate( current );
if ( valid( 0, 0, rotated ) ) {
current = rotated;
}
tick();
break;
}
}
// checks if the resulting position of current shape will be feasible
function valid( offsetX, offsetY, newCurrent ) {