This commit is contained in:
sageTheDM 2025-01-05 03:20:42 +01:00
parent 8478bc66e7
commit 78795c5be3
3 changed files with 79 additions and 34 deletions

View file

@ -6,8 +6,7 @@ const downButton = document.getElementById("down");
const leftButton = document.getElementById("left");
const rightButton = document.getElementById("right");
const startButton = document.getElementById("start");
const aButton = document.getElementById("a");
const bButton = document.getElementById("b");
const grid = document.getElementById("grid");
// Game variables
let snake;
@ -17,10 +16,17 @@ let isGameRunning = false;
// Initialize the game
function initGame() {
document.getElementById('grid').style.display = 'grid';
document.getElementById('title').style.display = 'none';
document.getElementById('description').style.display = 'none';
snake = [{ x: 10, y: 10 }];
apple = spawnApple();
isGameRunning = true;
gameInterval = setInterval(gameLoop, 100);
// Render grid only during initialization
renderGrid();
render();
}
// Game loop
@ -33,15 +39,14 @@ function gameLoop() {
// Spawn an apple at a random position
function spawnApple() {
return {
x: Math.floor(Math.random() * 20),
y: Math.floor(Math.random() * 20),
x: Math.floor(Math.random() * 10),
y: Math.floor(Math.random() * 10),
};
}
// Update the snake's position
function updateSnake() {
// Logic to move the snake based on direction
// This is where you would handle the snake's movement
}
// Check for collisions with walls, itself, or apple
@ -54,6 +59,23 @@ function render() {
// Logic to render the snake and apple on the screen
}
// Render the grid
function renderGrid() {
const gridSize = 10;
grid.innerHTML = ''; // Clear the grid first
for (let y = 0; y < gridSize; y++) {
let row = document.createElement('div');
row.classList.add('row');
for (let x = 0; x < gridSize; x++) {
let cell = document.createElement('div');
cell.classList.add('cell');
row.appendChild(cell);
}
grid.appendChild(row);
}
}
// Button event listeners
upButton.addEventListener("click", () => {
// Logic to move the snake up
@ -76,12 +98,3 @@ startButton.addEventListener("click", () => {
initGame();
}
});
// Additional button functionalities for A and B
aButton.addEventListener("click", () => {
// Logic for A button (e.g., pause or special action)
});
bButton.addEventListener("click", () => {
// Logic for B button (e.g., reset or another action)
});