// Stop the background music
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
// Clear the game board and reset the score
document.getElementById("menu").style.display = "none";
const gameBoard = document.getElementById('game-board');
gameBoard.innerHTML = '';
const scoreElement = document.getElementById('score');
scoreElement.innerHTML = 'Score: 0';
// Reset the game variables
click = 0;
draggedRectangles = [];
numberOfRectangles = 0;
rectangleColor = null;
i = 0;
pickedUpRectangles = 0;
isGameEnded = false;
// Start a new game
startGame();
}
code
function startGame() {
document.getElementById("startBtn").style.display = "none";
const gameBoard = document.getElementById('game-board');
gameBoard.innerHTML = '';
for (let row = 0; row < boardHeight; row++) {
const divRow = document.createElement('div');
divRow.classList.add('row');
for (let col = 0; col < boardWidth; col++) {
const divRectangle = document.createElement('div');
divRectangle.classList.add('rectangle');
divRectangle.addEventListener("click", pickRectangle);
if (row < numFilledRows) {
divRectangle.style.backgroundColor = getRandomColor();
divRectangle.classList.add('visible');
}
divRow.appendChild(divRectangle);
}
gameBoard.appendChild(divRow);
}
// Create a new HTML element to display the score
const scoreElement = document.getElementById("score");
scoreElement.innerHTML = 'Score: 0'; // Initialize score value in the element
backgroundMusic.play();
backgroundMusic.loop = true;
setTimerAndAddRow();
}
function getRandomColor() {
const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange'];
const randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex];
}
Assistant
To make the rectangles move in columns when you pick them up, you need to modify the dragRectangle() function. Here's an updated version of the function:
javascript
function dragRectangle(event) {
if (draggedRectangles.length > 0) {
const gameBoard = document.getElementById("game-board");
const boardRect = gameBoard.getBoundingClientRect();
const columnWidth = gameBoard.offsetWidth / boardWidth;
const columnIndex = Math.floor((event.clientX - boardRect.left) / columnWidth);
// Calculate the left position based on the column index
const leftPosition = boardRect.left + columnIndex * columnWidth;
draggedRectangles.forEach((draggedRectangle) => {
draggedRectangle.style.left = leftPosition + 'px';
draggedRectangle.style.top = event.clientY - draggedRectangle.offsetHeight / 2 + 'px';
});
}
}
let click = 0;
let draggedRectangles = [];
let numberOfRectangles = 0;
let rectangleColor = null;
let i = 0;
let pickedUpRectangles = 0;
// Get the colors of the rectangles in the bottom row
const bottomRowRectangles = rowDivs[rowDivs.length - 1].querySelectorAll('.rectangle');
const bottomRowColors = Array.from(bottomRowRectangles).map(rectangle => rectangle.style.backgroundColor);
// Shift all rows down by 1
for (let i = rowDivs.length - 5; i > 0; i--) {
const currentRowRectangles = rowDivs[i].querySelectorAll('.rectangle');
const prevRowRectangles = rowDivs[i - 1].querySelectorAll('.rectangle');
// Fill the first row with new colors
const firstRowRectangles = rowDivs[0].querySelectorAll('.rectangle');
Array.from(firstRowRectangles).forEach((rectangle, index) => {
rectangle.style.backgroundColor = bottomRowColors[index] || getRandomColor();
rectangle.classList.add("visible");
});
function checkColumns() {
const rectangles = document.getElementsByClassName("rectangle");
for (let col = 0; col < boardWidth; col++) {
let count = 0;
for (let row = boardHeight - 4; row >= 0; row--) {
const index = row * boardWidth + col;
const rectangle = rectangles[index];
if (rectangle.classList.contains("visible")) {
count++;
}
}
if (count >= 10) {
endGame();
return;
}
}
}
function endGame() {
isGameEnded = true;
document.getElementById("menu").style.display = "flex";
document.removeEventListener("mousemove", dragRectangle);
backgroundMusic.pause();
const playerName = prompt("Game over! Enter your name:");
if (playerName) {
updateLeaderboard(playerName, score);
displayLeaderboard();
}
}
code
</script>
This function calculates the column index based on the current mouse position and adjusts the left position of the dragged rectangles to be aligned with that column.