- Joined
- Jun 30, 2023
- Messages
- 1
- Reaction score
- 0
I have a basic grid stacking game. It's not completed yet, but here's what I have so far - Red blocks fall from the top of a 7x7 grid. When the blocks land, they should stay there, and another block spawns. However, this is not the case. When a block lands, another spawns on top of the first. Any ideas?
// JavaScript code
const gridSize = 7;
const cellSize = 30;
const dropInterval = 1000; // Time interval for the square to drop (in milliseconds)
let currentSquare = null;
let intervalId = null;
let blockPositions = [];
// Create a 2D array for the grid
const grid = new Array(gridSize);
for (let i = 0; i < gridSize; i++) {
grid = new Array(gridSize);
}
// Fill the grid with numbers and grid pieces
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
if (i === 0 && j === 0) {
// Top-left corner
grid[j] = '';
} else if (i === 0) {
// First row (numbers)
grid[j] = j.toString();
} else if (j === 0) {
// First column (numbers)
grid[j] = i.toString();
} else {
// Grid pieces
grid[j] = '';
}
}
}
// Create the HTML grid
const table = document.getElementById('grid');
for (let i = 0; i < gridSize; i++) {
const row = document.createElement('tr');
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement('td');
cell.textContent = grid[j];
row.appendChild(cell);
}
table.appendChild(row);
}
// Spawn a new square
function spawnSquare() {
const spawnX = Math.floor(Math.random() * (gridSize - 1)) + 1; // Randomize the spawn position within the grid
currentSquare = { x: spawnX, y: 1 }; // Initial position of the square
// Check if the position above the current square is available
if (collidesWithBlock()) {
stopFalling();
resetGame();
return; // Stop the game if the position above is occupied
}
drawSquare();
startFalling();
}
// Draw the current square on the grid
function drawSquare() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
cell.classList.add('square');
}
// Remove the current square from the grid
function clearSquare() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
cell.classList.remove('square');
}
// Move the current square down
function moveDown() {
clearSquare();
currentSquare.y++;
drawSquare();
if (collidesWithGrid() || collidesWithBottom() || collidesWithBlock()) {
currentSquare.y--;
stackSquare();
removeCompletedRows();
stopFalling();
spawnSquare();
}
}
// Check if the current square collides with the grid or bottom
function collidesWithGrid() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
return cell.textContent !== '';
}
function collidesWithBottom() {
return currentSquare.y === gridSize - 1;
}
// Check if the current square collides with a block in place
function collidesWithBlock() {
const row = table.children[currentSquare.y + 1];
const cell = row.children[currentSquare.x];
return cell.classList.contains('square');
}
// Stack the current square on top of the grid
function stackSquare() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
cell.classList.add('square');
cell.textContent = 'X'; // Update the cell's text content to 'X'
blockPositions.push({ x: currentSquare.x, y: currentSquare.y });
}
// Remove completed rows from the grid
function removeCompletedRows() {
const completedRows = [];
for (let i = 1; i < gridSize; i++) {
let isRowComplete = true;
for (let j = 1; j < gridSize; j++) {
const row = table.children;
const cell = row.children[j];
if (!cell.classList.contains('square')) {
isRowComplete = false;
break;
}
}
if (isRowComplete) {
completedRows.push(i);
}
}
// Remove completed rows and shift the rows above
for (let i = 0; i < completedRows.length; i++) {
const rowIndex = completedRows;
const row = table.children[rowIndex];
row.remove();
}
// Add new rows at the top
for (let i = 0; i < completedRows.length; i++) {
const newRow = document.createElement('tr');
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement('td');
cell.textContent = grid[0][j];
newRow.appendChild(cell);
}
table.prepend(newRow);
}
}
// Reset the game
function resetGame() {
clearGrid();
currentSquare = null;
blockPositions = [];
}
// Clear the grid
function clearGrid() {
for (let i = 1; i < gridSize; i++) {
const row = table.children;
for (let j = 1; j < gridSize; j++) {
const cell = row.children[j];
cell.classList.remove('square');
cell.textContent = '';
}
}
}
// Start the falling interval
function startFalling() {
intervalId = setInterval(moveDown, dropInterval);
}
// Stop the falling interval
function stopFalling() {
clearInterval(intervalId);
}
// Event listener for keyboard inputs
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
// Move the square to the left (if there's an empty space)
if (currentSquare.x > 1 && !collidesWithLeftBlock()) {
clearSquare();
currentSquare.x--;
drawSquare();
}
} else if (event.key === 'ArrowRight') {
// Move the square to the right (if there's an empty space)
if (currentSquare.x < gridSize - 1 && !collidesWithRightBlock()) {
clearSquare();
currentSquare.x++;
drawSquare();
}
} else if (event.key === 'ArrowDown') {
// Move the square down instantly
moveDown();
}
});
// Check if the current square collides with a block on the left
function collidesWithLeftBlock() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x - 1];
return cell.classList.contains('square');
}
// Check if the current square collides with a block on the right
function collidesWithRightBlock() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x + 1];
return cell.classList.contains('square');
}
// Start the game by spawning the first square
spawnSquare();
Thanks so much!
// JavaScript code
const gridSize = 7;
const cellSize = 30;
const dropInterval = 1000; // Time interval for the square to drop (in milliseconds)
let currentSquare = null;
let intervalId = null;
let blockPositions = [];
// Create a 2D array for the grid
const grid = new Array(gridSize);
for (let i = 0; i < gridSize; i++) {
grid = new Array(gridSize);
}
// Fill the grid with numbers and grid pieces
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
if (i === 0 && j === 0) {
// Top-left corner
grid[j] = '';
} else if (i === 0) {
// First row (numbers)
grid[j] = j.toString();
} else if (j === 0) {
// First column (numbers)
grid[j] = i.toString();
} else {
// Grid pieces
grid[j] = '';
}
}
}
// Create the HTML grid
const table = document.getElementById('grid');
for (let i = 0; i < gridSize; i++) {
const row = document.createElement('tr');
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement('td');
cell.textContent = grid[j];
row.appendChild(cell);
}
table.appendChild(row);
}
// Spawn a new square
function spawnSquare() {
const spawnX = Math.floor(Math.random() * (gridSize - 1)) + 1; // Randomize the spawn position within the grid
currentSquare = { x: spawnX, y: 1 }; // Initial position of the square
// Check if the position above the current square is available
if (collidesWithBlock()) {
stopFalling();
resetGame();
return; // Stop the game if the position above is occupied
}
drawSquare();
startFalling();
}
// Draw the current square on the grid
function drawSquare() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
cell.classList.add('square');
}
// Remove the current square from the grid
function clearSquare() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
cell.classList.remove('square');
}
// Move the current square down
function moveDown() {
clearSquare();
currentSquare.y++;
drawSquare();
if (collidesWithGrid() || collidesWithBottom() || collidesWithBlock()) {
currentSquare.y--;
stackSquare();
removeCompletedRows();
stopFalling();
spawnSquare();
}
}
// Check if the current square collides with the grid or bottom
function collidesWithGrid() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
return cell.textContent !== '';
}
function collidesWithBottom() {
return currentSquare.y === gridSize - 1;
}
// Check if the current square collides with a block in place
function collidesWithBlock() {
const row = table.children[currentSquare.y + 1];
const cell = row.children[currentSquare.x];
return cell.classList.contains('square');
}
// Stack the current square on top of the grid
function stackSquare() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x];
cell.classList.add('square');
cell.textContent = 'X'; // Update the cell's text content to 'X'
blockPositions.push({ x: currentSquare.x, y: currentSquare.y });
}
// Remove completed rows from the grid
function removeCompletedRows() {
const completedRows = [];
for (let i = 1; i < gridSize; i++) {
let isRowComplete = true;
for (let j = 1; j < gridSize; j++) {
const row = table.children;
const cell = row.children[j];
if (!cell.classList.contains('square')) {
isRowComplete = false;
break;
}
}
if (isRowComplete) {
completedRows.push(i);
}
}
// Remove completed rows and shift the rows above
for (let i = 0; i < completedRows.length; i++) {
const rowIndex = completedRows;
const row = table.children[rowIndex];
row.remove();
}
// Add new rows at the top
for (let i = 0; i < completedRows.length; i++) {
const newRow = document.createElement('tr');
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement('td');
cell.textContent = grid[0][j];
newRow.appendChild(cell);
}
table.prepend(newRow);
}
}
// Reset the game
function resetGame() {
clearGrid();
currentSquare = null;
blockPositions = [];
}
// Clear the grid
function clearGrid() {
for (let i = 1; i < gridSize; i++) {
const row = table.children;
for (let j = 1; j < gridSize; j++) {
const cell = row.children[j];
cell.classList.remove('square');
cell.textContent = '';
}
}
}
// Start the falling interval
function startFalling() {
intervalId = setInterval(moveDown, dropInterval);
}
// Stop the falling interval
function stopFalling() {
clearInterval(intervalId);
}
// Event listener for keyboard inputs
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
// Move the square to the left (if there's an empty space)
if (currentSquare.x > 1 && !collidesWithLeftBlock()) {
clearSquare();
currentSquare.x--;
drawSquare();
}
} else if (event.key === 'ArrowRight') {
// Move the square to the right (if there's an empty space)
if (currentSquare.x < gridSize - 1 && !collidesWithRightBlock()) {
clearSquare();
currentSquare.x++;
drawSquare();
}
} else if (event.key === 'ArrowDown') {
// Move the square down instantly
moveDown();
}
});
// Check if the current square collides with a block on the left
function collidesWithLeftBlock() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x - 1];
return cell.classList.contains('square');
}
// Check if the current square collides with a block on the right
function collidesWithRightBlock() {
const row = table.children[currentSquare.y];
const cell = row.children[currentSquare.x + 1];
return cell.classList.contains('square');
}
// Start the game by spawning the first square
spawnSquare();
Thanks so much!