finish rotation and fastDrop
This commit is contained in:
parent
57a90b3083
commit
3c10c7db09
@ -8,6 +8,19 @@ boardContext.fillRect(0, 0, board.width, board.height);
|
|||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.src = '../img/PieceTetris.png';
|
img.src = '../img/PieceTetris.png';
|
||||||
|
|
||||||
|
let downInterval = setInterval(() => {
|
||||||
|
clearBoard();
|
||||||
|
moveDown();
|
||||||
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
let rightInterval;
|
||||||
|
|
||||||
|
let leftInterval;
|
||||||
|
|
||||||
|
let keyPress = [];
|
||||||
|
|
||||||
const templatePiece = [
|
const templatePiece = [
|
||||||
[ // ok
|
[ // ok
|
||||||
[3, 0],
|
[3, 0],
|
||||||
@ -49,7 +62,7 @@ const templatePiece = [
|
|||||||
[4, 0],
|
[4, 0],
|
||||||
[4, 1],
|
[4, 1],
|
||||||
[5, 0],
|
[5, 0],
|
||||||
[4, 1]
|
[4, 0]
|
||||||
],
|
],
|
||||||
[ // ok
|
[ // ok
|
||||||
[4, 0],
|
[4, 0],
|
||||||
@ -132,7 +145,6 @@ function currentPieceToBoard() {
|
|||||||
let piece = currentPiece[i];
|
let piece = currentPiece[i];
|
||||||
boardData[piece[0]][piece[1]] = color; // Copie du tableau color
|
boardData[piece[0]][piece[1]] = color; // Copie du tableau color
|
||||||
}
|
}
|
||||||
console.log("Hey");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,8 +161,21 @@ function spawnPiece() {
|
|||||||
|
|
||||||
|
|
||||||
function ifMoveTo(x, y) {
|
function ifMoveTo(x, y) {
|
||||||
for (let i = 0; i < currentPiece.length - 1; i++) {
|
return canMoveTo(x, y, currentPiece);
|
||||||
if (currentPiece[i][0] + x < 0 || currentPiece[i][0] + x >= 10 || currentPiece[i][1] + y >= 20 || ifPiece(currentPiece[i][0] + x, currentPiece[i][1] + y)) {
|
}
|
||||||
|
|
||||||
|
function canMoveTo(x, y, piece) {
|
||||||
|
for (let i = 0; i < piece.length - 1; i++) {
|
||||||
|
if (piece[i][0] + x < 0 || piece[i][0] + x >= 10 || piece[i][1] + y >= 20 || ifPiece(piece[i][0] + x, piece[i][1] + y)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ifRotate(piece) {
|
||||||
|
for (let i = 0; i < piece.length - 1; i++) {
|
||||||
|
if (piece[i][0] < 0 || piece[i][0] >= 10 || piece[i][1] >= 20 || ifPiece(piece[i][0], piece[i][1])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,6 +183,14 @@ function ifMoveTo(x, y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ifPiece(x, y) {
|
function ifPiece(x, y) {
|
||||||
|
if (x < 0 || x >= 10 || y >= 20) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (boardData[x][y][0] == 0 && boardData[x][y][1] == 0 && boardData[x][y][2] == 0) {
|
if (boardData[x][y][0] == 0 && boardData[x][y][1] == 0 && boardData[x][y][2] == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -170,13 +203,35 @@ function rotate() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const center = currentPiece[currentPiece.length - 1];
|
let currentPieceCopy = [];
|
||||||
for (let i = 0; i < currentPiece.length - 1; i++) {
|
|
||||||
const x = currentPiece[i][0] - center[0];
|
|
||||||
const y = currentPiece[i][1] - center[1];
|
|
||||||
|
|
||||||
currentPiece[i][0] = center[0] - y;
|
for (let i = 0; i < currentPiece.length; i++) {
|
||||||
currentPiece[i][1] = center[1] + x;
|
currentPieceCopy.push([currentPiece[i][0], currentPiece[i][1]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const center = currentPieceCopy[currentPieceCopy.length - 1];
|
||||||
|
for (let i = 0; i < currentPiece.length - 1; i++) {
|
||||||
|
const x = currentPieceCopy[i][0] - center[0];
|
||||||
|
const y = currentPieceCopy[i][1] - center[1];
|
||||||
|
|
||||||
|
currentPieceCopy[i][0] = center[0] - y;
|
||||||
|
currentPieceCopy[i][1] = center[1] + x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (ifRotate(currentPieceCopy)) {
|
||||||
|
currentPiece = currentPieceCopy;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (let x = -1; x <= 1; x++) {
|
||||||
|
for (let y = -1; y <= 1; y++) {
|
||||||
|
if (canMoveTo(x, y, currentPieceCopy)) {
|
||||||
|
moveToPiece(x, y, currentPieceCopy);
|
||||||
|
currentPiece = currentPieceCopy;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,9 +256,13 @@ function checkLine() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function moveTo(x, y) {
|
function moveTo(x, y) {
|
||||||
for (let i = 0; i < currentPiece.length; i++) {
|
moveToPiece(x, y, currentPiece);
|
||||||
currentPiece[i][0] += x;
|
}
|
||||||
currentPiece[i][1] += y;
|
|
||||||
|
function moveToPiece(x, y, piece) {
|
||||||
|
for (let i = 0; i < piece.length; i++) {
|
||||||
|
piece[i][0] += x;
|
||||||
|
piece[i][1] += y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,30 +291,83 @@ function moveRight() {
|
|||||||
moveTo(1, 0);
|
moveTo(1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fasteDrop() {
|
||||||
|
while (ifMoveTo(0, 1)) {
|
||||||
|
moveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
loadTetris();
|
loadTetris();
|
||||||
drawCurrentPiece();
|
drawCurrentPiece();
|
||||||
|
|
||||||
setInterval(() => {
|
|
||||||
clearBoard();
|
|
||||||
moveDown();
|
|
||||||
drawBoard();
|
|
||||||
drawCurrentPiece();
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
document.addEventListener('keydown', (event) => {
|
document.addEventListener('keydown', (event) => {
|
||||||
if (event.key == 'ArrowUp') {
|
if (event.key == 'ArrowUp' && !keyPress.includes('ArrowUp')) {
|
||||||
rotate();
|
rotate();
|
||||||
} else if (event.key == 'ArrowDown') {
|
} else if (event.key == 'ArrowDown' && !keyPress.includes('ArrowDown')) {
|
||||||
moveDown();
|
clearInterval(downInterval);
|
||||||
} else if (event.key == 'ArrowLeft') {
|
downInterval = setInterval(() => {
|
||||||
|
clearBoard();
|
||||||
|
moveDown();
|
||||||
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
}, 50);
|
||||||
|
} else if (event.key == 'ArrowLeft' && !keyPress.includes('ArrowLeft')) {
|
||||||
|
clearBoard();
|
||||||
moveLeft();
|
moveLeft();
|
||||||
} else if (event.key == 'ArrowRight') {
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
|
||||||
|
leftInterval = setInterval(() => {
|
||||||
|
clearBoard();
|
||||||
|
moveLeft();
|
||||||
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
}, 100);
|
||||||
|
} else if (event.key == 'ArrowRight' && !keyPress.includes('ArrowRight')) {
|
||||||
|
clearBoard();
|
||||||
moveRight();
|
moveRight();
|
||||||
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
|
||||||
|
rightInterval = setInterval(() => {
|
||||||
|
clearBoard();
|
||||||
|
moveRight();
|
||||||
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
}, 100);
|
||||||
|
} else if (event.key == ' ' && !keyPress.includes(' ')) {
|
||||||
|
fasteDrop();
|
||||||
}
|
}
|
||||||
|
|
||||||
clearBoard();
|
clearBoard();
|
||||||
drawBoard();
|
drawBoard();
|
||||||
drawCurrentPiece();
|
drawCurrentPiece();
|
||||||
|
|
||||||
|
console.log(event.key);
|
||||||
|
|
||||||
|
keyPress.push(event.key);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('keyup', (event) => {
|
||||||
|
if (event.key == 'ArrowDown') {
|
||||||
|
clearInterval(downInterval);
|
||||||
|
downInterval = setInterval(() => {
|
||||||
|
clearBoard();
|
||||||
|
moveDown();
|
||||||
|
drawBoard();
|
||||||
|
drawCurrentPiece();
|
||||||
|
}, 1000);
|
||||||
|
} else if (event.key == 'ArrowLeft') {
|
||||||
|
clearInterval(leftInterval);
|
||||||
|
} else if (event.key == 'ArrowRight') {
|
||||||
|
clearInterval(rightInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
keyPress = keyPress.filter((key) => {
|
||||||
|
return key != event.key;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user