changement dans les pièce et début des rajouts de l'historique

This commit is contained in:
flo 2025-04-01 21:26:04 +02:00
parent 3c10c7db09
commit dc22b86f1d

View File

@ -9,10 +9,9 @@ const img = new Image();
img.src = '../img/PieceTetris.png'; img.src = '../img/PieceTetris.png';
let downInterval = setInterval(() => { let downInterval = setInterval(() => {
clearBoard();
moveDown(); moveDown();
drawBoard();
drawCurrentPiece(); refresh();
}, 1000); }, 1000);
let rightInterval; let rightInterval;
@ -30,10 +29,10 @@ const templatePiece = [
[4.5, 0.5] [4.5, 0.5]
], ],
[ [
[3, 0], [3, 1],
[4, 0], [4, 1],
[5, 0],
[5, 1], [5, 1],
[5, 0],
[4, 1] [4, 1]
], ],
[ // ok [ // ok
@ -73,6 +72,16 @@ const templatePiece = [
] ]
]; ];
const MOVELEFT = 1;
const MOVERIGHT = 2;
const MOVEDOWN = 3;
const ROTATE = 4;
const MOVEDOWNFAST = 5;
const RESPAWN = 6;
const history = [];
const templateColor = [ const templateColor = [
[255, 0, 0, 200], [255, 0, 0, 200],
[0, 255, 0, 200], [0, 255, 0, 200],
@ -133,6 +142,34 @@ function clearBoard() {
boardContext.fillRect(0, 0, board.width, board.height); boardContext.fillRect(0, 0, board.width, board.height);
} }
function drawGrid() {
boardContext.strokeStyle = 'gray'; // Couleur des lignes du quadrillage
boardContext.lineWidth = 0.5; // Épaisseur des lignes
// Dessiner les lignes verticales
for (let x = 0; x <= board.width; x += board.width / 10) {
boardContext.beginPath();
boardContext.moveTo(x, 0);
boardContext.lineTo(x, board.height);
boardContext.stroke();
}
// Dessiner les lignes horizontales
for (let y = 0; y <= board.height; y += board.height / 20) {
boardContext.beginPath();
boardContext.moveTo(0, y);
boardContext.lineTo(board.width, y);
boardContext.stroke();
}
}
function refresh() {
clearBoard();
drawGrid();
drawBoard();
drawCurrentPiece();
}
function loadTetris() { function loadTetris() {
currentPieceToBoard(); currentPieceToBoard();
checkLine(); checkLine();
@ -157,6 +194,8 @@ function spawnPiece() {
} }
color = templateColor[random]; color = templateColor[random];
history.push([RESPAWN, random]);
} }
@ -221,6 +260,8 @@ function rotate() {
if (ifRotate(currentPieceCopy)) { if (ifRotate(currentPieceCopy)) {
currentPiece = currentPieceCopy; currentPiece = currentPieceCopy;
history.push([ROTATE, 0]);
return;
} }
else { else {
for (let x = -1; x <= 1; x++) { for (let x = -1; x <= 1; x++) {
@ -228,6 +269,8 @@ function rotate() {
if (canMoveTo(x, y, currentPieceCopy)) { if (canMoveTo(x, y, currentPieceCopy)) {
moveToPiece(x, y, currentPieceCopy); moveToPiece(x, y, currentPieceCopy);
currentPiece = currentPieceCopy; currentPiece = currentPieceCopy;
history.push([ROTATE, 0]);
return; return;
} }
} }
@ -269,32 +312,45 @@ function moveToPiece(x, y, piece) {
function moveDown() { function moveDown() {
if (!ifMoveTo(0, 1) || currentPiece.length == 1) { if (!ifMoveTo(0, 1) || currentPiece.length == 1) {
loadTetris(); loadTetris();
return; return false;
} }
moveTo(0, 1); moveTo(0, 1);
history.push([MOVEDOWN, 0]);
return true;
} }
function moveLeft() { function moveLeft() {
if (!ifMoveTo(-1, 0) || currentPiece.length == 1) { if (!ifMoveTo(-1, 0) || currentPiece.length == 1) {
return; return false;
} }
moveTo(-1, 0); moveTo(-1, 0);
history.push([MOVELEFT, 0]);
return true;
} }
function moveRight() { function moveRight() {
if (!ifMoveTo(1, 0) || currentPiece.length == 1) { if (!ifMoveTo(1, 0) || currentPiece.length == 1) {
return; return false;
} }
moveTo(1, 0); moveTo(1, 0);
history.push([MOVERIGHT, 0]);
return true;
} }
function fasteDrop() { function fasteDrop() {
while (ifMoveTo(0, 1)) { while (ifMoveTo(0, 1)) {
moveDown(); moveDown();
} }
loadTetris();
history.push([MOVEDOWNFAST, 0]);
} }
img.onload = () => { img.onload = () => {
@ -309,42 +365,30 @@ img.onload = () => {
} else if (event.key == 'ArrowDown' && !keyPress.includes('ArrowDown')) { } else if (event.key == 'ArrowDown' && !keyPress.includes('ArrowDown')) {
clearInterval(downInterval); clearInterval(downInterval);
downInterval = setInterval(() => { downInterval = setInterval(() => {
clearBoard();
moveDown(); moveDown();
drawBoard();
drawCurrentPiece(); refresh();
}, 50); }, 50);
} else if (event.key == 'ArrowLeft' && !keyPress.includes('ArrowLeft')) { } else if (event.key == 'ArrowLeft' && !keyPress.includes('ArrowLeft')) {
clearBoard();
moveLeft(); moveLeft();
drawBoard();
drawCurrentPiece();
leftInterval = setInterval(() => { leftInterval = setInterval(() => {
clearBoard();
moveLeft(); moveLeft();
drawBoard();
drawCurrentPiece(); refresh();
}, 100); }, 100);
} else if (event.key == 'ArrowRight' && !keyPress.includes('ArrowRight')) { } else if (event.key == 'ArrowRight' && !keyPress.includes('ArrowRight')) {
clearBoard();
moveRight(); moveRight();
drawBoard();
drawCurrentPiece();
rightInterval = setInterval(() => { rightInterval = setInterval(() => {
clearBoard();
moveRight(); moveRight();
drawBoard(); refresh();
drawCurrentPiece();
}, 100); }, 100);
} else if (event.key == ' ' && !keyPress.includes(' ')) { } else if (event.key == ' ' && !keyPress.includes(' ')) {
fasteDrop(); fasteDrop();
} }
clearBoard(); refresh();
drawBoard();
drawCurrentPiece();
console.log(event.key); console.log(event.key);
@ -355,10 +399,9 @@ img.onload = () => {
if (event.key == 'ArrowDown') { if (event.key == 'ArrowDown') {
clearInterval(downInterval); clearInterval(downInterval);
downInterval = setInterval(() => { downInterval = setInterval(() => {
clearBoard();
moveDown(); moveDown();
drawBoard();
drawCurrentPiece(); refresh();
}, 1000); }, 1000);
} else if (event.key == 'ArrowLeft') { } else if (event.key == 'ArrowLeft') {
clearInterval(leftInterval); clearInterval(leftInterval);
@ -370,4 +413,6 @@ img.onload = () => {
return key != event.key; return key != event.key;
}); });
}); });
} }