formatted like my Boss wants it (help!)

This commit is contained in:
sageTheDM 2025-02-25 10:04:34 +01:00
parent 5d11f87dd3
commit b507a6b0ea
19 changed files with 1425 additions and 1136 deletions

View file

@ -1,13 +1,13 @@
'use strict';
"use strict";
const upButton = document.getElementById('up');
const downButton = document.getElementById('down');
const leftButton = document.getElementById('left');
const rightButton = document.getElementById('right');
const startButton = document.getElementById('start');
const grid = document.createElement('div');
grid.id = 'grid';
document.querySelector('.game').appendChild(grid);
const upButton = document.getElementById("up");
const downButton = document.getElementById("down");
const leftButton = document.getElementById("left");
const rightButton = document.getElementById("right");
const startButton = document.getElementById("start");
const grid = document.createElement("div");
grid.id = "grid";
document.querySelector(".game").appendChild(grid);
let snake = [{ x: 5, y: 5 }];
let apple = { x: 8, y: 5 };
@ -17,128 +17,134 @@ let isGameRunning = false;
const gridSize = 10;
function initGame() {
grid.style.display = 'grid';
grid.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
grid.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
grid.style.width = '350px';
grid.style.height = '350px';
document.getElementById('title').style.display = 'none';
document.getElementById('description').style.display = 'none';
grid.style.display = "grid";
grid.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
grid.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
grid.style.width = "350px";
grid.style.height = "350px";
document.getElementById("title").style.display = "none";
document.getElementById("description").style.display = "none";
isGameRunning = true;
snake = [{ x: 5, y: 5 }];
apple = spawnApple();
direction = { x: 0, y: 0 };
isGameRunning = true;
snake = [{ x: 5, y: 5 }];
apple = spawnApple();
direction = { x: 0, y: 0 };
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 200);
render();
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 200);
render();
}
function spawnApple() {
let newApple;
do {
newApple = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
} while (snake.some(segment => segment.x === newApple.x && segment.y === newApple.y));
return newApple;
let newApple;
do {
newApple = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize),
};
} while (
snake.some(
(segment) => segment.x === newApple.x && segment.y === newApple.y
)
);
return newApple;
}
function updateSnake() {
const newHead = {
x: snake[0].x + direction.x,
y: snake[0].y + direction.y
};
const newHead = {
x: snake[0].x + direction.x,
y: snake[0].y + direction.y,
};
if (newHead.x < 0) newHead.x = gridSize - 1;
if (newHead.y < 0) newHead.y = gridSize - 1;
if (newHead.x >= gridSize) newHead.x = 0;
if (newHead.y >= gridSize) newHead.y = 0;
if (newHead.x < 0) newHead.x = gridSize - 1;
if (newHead.y < 0) newHead.y = gridSize - 1;
if (newHead.x >= gridSize) newHead.x = 0;
if (newHead.y >= gridSize) newHead.y = 0;
if (snake.some(segment => segment.x === newHead.x && segment.y === newHead.y)) {
gameOver();
return;
}
if (
snake.some((segment) => segment.x === newHead.x && segment.y === newHead.y)
) {
gameOver();
return;
}
snake.unshift(newHead);
snake.unshift(newHead);
if (newHead.x === apple.x && newHead.y === apple.y) {
apple = spawnApple();
} else {
snake.pop();
}
if (newHead.x === apple.x && newHead.y === apple.y) {
apple = spawnApple();
} else {
snake.pop();
}
}
function render() {
grid.innerHTML = "";
grid.innerHTML = "";
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
const cell = document.createElement("div");
cell.classList.add("cell");
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
const cell = document.createElement("div");
cell.classList.add("cell");
if ((x + y) % 2 === 0) {
cell.classList.add("light-green");
} else {
cell.classList.add("dark-green");
}
if ((x + y) % 2 === 0) {
cell.classList.add("light-green");
} else {
cell.classList.add("dark-green");
}
if (snake.some(segment => segment.x === x && segment.y === y)) {
cell.classList.add("snake");
}
if (snake.some((segment) => segment.x === x && segment.y === y)) {
cell.classList.add("snake");
}
if (apple.x === x && apple.y === y) {
cell.classList.add("apple");
}
if (apple.x === x && apple.y === y) {
cell.classList.add("apple");
}
grid.appendChild(cell);
}
grid.appendChild(cell);
}
}
}
function gameLoop() {
if (!isGameRunning) return;
updateSnake();
render();
if (!isGameRunning) return;
updateSnake();
render();
}
function gameOver() {
alert('Game Over!');
clearInterval(gameInterval);
isGameRunning = false;
alert("Game Over!");
clearInterval(gameInterval);
isGameRunning = false;
}
function handleDirectionInput(key) {
switch (key) {
case 'ArrowUp':
case 'w':
if (direction.y === 0) direction = { x: 0, y: -1 };
break;
case 'ArrowDown':
case 's':
if (direction.y === 0) direction = { x: 0, y: 1 };
break;
case 'ArrowLeft':
case 'a':
if (direction.x === 0) direction = { x: -1, y: 0 };
break;
case 'ArrowRight':
case 'd':
if (direction.x === 0) direction = { x: 1, y: 0 };
break;
}
switch (key) {
case "ArrowUp":
case "w":
if (direction.y === 0) direction = { x: 0, y: -1 };
break;
case "ArrowDown":
case "s":
if (direction.y === 0) direction = { x: 0, y: 1 };
break;
case "ArrowLeft":
case "a":
if (direction.x === 0) direction = { x: -1, y: 0 };
break;
case "ArrowRight":
case "d":
if (direction.x === 0) direction = { x: 1, y: 0 };
break;
}
}
upButton.addEventListener('click', () => handleDirectionInput('ArrowUp'));
downButton.addEventListener('click', () => handleDirectionInput('ArrowDown'));
leftButton.addEventListener('click', () => handleDirectionInput('ArrowLeft'));
rightButton.addEventListener('click', () => handleDirectionInput('ArrowRight'));
startButton.addEventListener('click', () => {
if (!isGameRunning) initGame();
upButton.addEventListener("click", () => handleDirectionInput("ArrowUp"));
downButton.addEventListener("click", () => handleDirectionInput("ArrowDown"));
leftButton.addEventListener("click", () => handleDirectionInput("ArrowLeft"));
rightButton.addEventListener("click", () => handleDirectionInput("ArrowRight"));
startButton.addEventListener("click", () => {
if (!isGameRunning) initGame();
});
document.addEventListener('keydown', (event) => {
handleDirectionInput(event.key);
document.addEventListener("keydown", (event) => {
handleDirectionInput(event.key);
});

View file

@ -1,238 +1,238 @@
/* Base Reset */
body,
html {
margin: 0;
padding: 0;
font-family: monospace;
background-color: #3a2d56;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
padding: 0;
font-family: monospace;
background-color: #3a2d56;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* GameBoy Layout */
.gameboy {
background-color: #5f4c82; /* Game Boy Color purple shell */
width: 441px;
height: 735px;
border-radius: 20px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.6);
display: flex;
flex-direction: column;
align-items: center;
padding: 10px;
position: relative;
background-color: #5f4c82;
width: 441px;
height: 735px;
border-radius: 20px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.6);
display: flex;
flex-direction: column;
align-items: center;
padding: 10px;
position: relative;
}
@media(max-width: 768px) {
.gameboy {
width: 100vw;
height: 100vh;
border-radius: 0;
}
@media (max-width: 768px) {
.gameboy {
width: 100vw;
height: 100vh;
border-radius: 0;
}
}
/* Screen */
.screen {
background-color: black;
border: 4px solid #0f380f;
width: 90%;
height: 55%;
margin-top: 20px;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.5);
overflow: hidden;
background-color: black;
border: 4px solid #0f380f;
width: 90%;
height: 55%;
margin-top: 20px;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.5);
overflow: hidden;
}
.game {
text-align: center;
width: 90%;
text-align: center;
width: 90%;
}
/* Titles */
h1 {
font-size: 2rem;
margin-bottom: 10px;
text-transform: uppercase;
color: #9bbc0f;
font-size: 2rem;
margin-bottom: 10px;
text-transform: uppercase;
color: #9bbc0f;
}
.description,
.description p {
font-size: 1.2rem;
margin: 0 auto;
padding: 0 auto;
color: white;
font-size: 1.2rem;
margin: 0 auto;
padding: 0 auto;
color: white;
}
/* Grid container */
#grid {
display: grid;
grid-template-columns: repeat(10, 1fr); /* Adjust to match gridSize */
grid-template-rows: repeat(10, 1fr); /* Adjust to match gridSize */
width: 400px; /* Adjust as needed */
height: 400px; /* Adjust as needed */
border: 2px solid #0f380f;
margin: 20px auto;
/* initially hide */
display: none;
display: grid;
grid-template-columns: repeat(10, 1fr); /* Adjust to match gridSize */
grid-template-rows: repeat(10, 1fr); /* Adjust to match gridSize */
width: 400px; /* Adjust as needed */
height: 400px; /* Adjust as needed */
border: 2px solid #0f380f;
margin: 20px auto;
/* initially hide */
display: none;
}
/* Individual cells */
.cell {
width: 100%;
height: 100%;
width: 100%;
height: 100%;
}
.cell.light-green {
background-color: #9bbc0f;
background-color: #9bbc0f;
}
.cell.dark-green {
background-color: #0f380f;
background-color: #0f380f;
}
/* Snake styling */
.snake {
background-color: #e600ff; /* Snake color */
z-index: 1000;
background-color: #e600ff; /* Snake color */
z-index: 1000;
}
/* Apple styling */
.apple {
background-color: red; /* Apple color */
z-index: 999;
background-color: red; /* Apple color */
z-index: 999;
}
/* Controls Section */
.controls {
margin-top: 20px;
display: flex;
justify-content: space-between;
width: 80%;
align-items: center;
margin-top: 20px;
display: flex;
justify-content: space-between;
width: 80%;
align-items: center;
}
/* D-Pad */
.dpad {
position: relative;
width: 120px;
height: 120px;
position: relative;
width: 120px;
height: 120px;
}
/* Base Styling for D-Pad Buttons */
.dpad-btn {
background-color: #0f380f;
color: #9bbc0f;
border: none;
border-radius: 5px;
position: absolute;
width: 42px;
height: 42px;
font-size: 1.5rem;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
z-index: 1;
background-color: #0f380f;
color: #9bbc0f;
border: none;
border-radius: 5px;
position: absolute;
width: 42px;
height: 42px;
font-size: 1.5rem;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
z-index: 1;
}
.dpad-btn.up {
top: 0;
left: 50%;
transform: translateX(-50%);
top: 0;
left: 50%;
transform: translateX(-50%);
}
.dpad-btn.down {
bottom: 0;
left: 50%;
transform: translateX(-50%);
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
.dpad-btn.left {
top: 50%;
left: 0;
transform: translateY(-50%);
top: 50%;
left: 0;
transform: translateY(-50%);
}
.dpad-btn.right {
top: 50%;
right: 0;
transform: translateY(-50%);
top: 50%;
right: 0;
transform: translateY(-50%);
}
/* D-Pad Center to Connect Buttons */
.dpad-center {
background-color: #0f380f;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 40px;
height: 40px;
border: 2px solid transparent;
z-index: 0;
border-radius: 5px;
background-color: #0f380f;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 40px;
height: 40px;
border: 2px solid transparent;
z-index: 0;
border-radius: 5px;
}
/* A and B Buttons */
.action-buttons {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 200px;
display: flex;
flex-direction: column;
justify-content: space-between;
height: 200px;
}
.btn {
background-color: #0f380f;
color: #9bbc0f;
border: 2px solid #9bbc0f;
border-radius: 50%;
width: 60px;
height: 60px;
font-size: 1.8rem;
cursor: pointer;
transition: transform 0.1s, background-color 0.2s;
background-color: #0f380f;
color: #9bbc0f;
border: 2px solid #9bbc0f;
border-radius: 50%;
width: 60px;
height: 60px;
font-size: 1.8rem;
cursor: pointer;
transition: transform 0.1s, background-color 0.2s;
}
.btn:hover {
background-color: #9bbc0f;
color: #0f380f;
background-color: #9bbc0f;
color: #0f380f;
}
.btn:active {
transform: scale(0.9);
transform: scale(0.9);
}
/* Start Button */
.start-btn {
background-color: #0f380f;
color: #9bbc0f;
border: 2px solid #9bbc0f;
border-radius: 5px;
width: 100px;
height: 40px;
font-size: 1.2rem;
cursor: pointer;
transition: transform 0.1s, background-color 0.2s;
margin-bottom: 20px;
background-color: #0f380f;
color: #9bbc0f;
border: 2px solid #9bbc0f;
border-radius: 5px;
width: 100px;
height: 40px;
font-size: 1.2rem;
cursor: pointer;
transition: transform 0.1s, background-color 0.2s;
margin-bottom: 20px;
}
.start-btn:hover {
background-color: #9bbc0f;
color: #0f380f;
background-color: #9bbc0f;
color: #0f380f;
}
.start-btn:active {
transform: scale(0.9);
transform: scale(0.9);
}
/* Hidden Canvas for Debugging or Fallback */
canvas {
display: none;
z-index: 1000;
display: none;
z-index: 1000;
}

View file

@ -1,60 +1,134 @@
'use strict';
const aBtn = document.querySelector('#a');
const bBtn = document.querySelector('#b');
const gameboy = document.querySelector('.gameboy');
"use strict";
const aBtn = document.querySelector("#a");
const bBtn = document.querySelector("#b");
const gameboy = document.querySelector(".gameboy");
const html = document.documentElement;
const body = document.body;
const dpadButtons = document.querySelectorAll('.dpad-btn');
const dpadCenter = document.querySelector('.dpad-center'); // Darker variant
const actionButtons = document.querySelectorAll('.btn');
const dpadButtons = document.querySelectorAll(".dpad-btn");
const dpadCenter = document.querySelector(".dpad-center"); // Darker variant
const actionButtons = document.querySelectorAll(".btn");
const colors = [
{ gameboyColor: '#B39DDB', htmlColor: '#D1C4E9', buttonColor: '#673AB7', buttonTextColor: '#FFFFFF', dpadCenterColor: '#5E35B1' },
{ gameboyColor: '#FFC107', htmlColor: '#FFF9C4', buttonColor: '#FF9800', buttonTextColor: '#000000', dpadCenterColor: '#EF6C00' },
{ gameboyColor: '#8BC34A', htmlColor: '#C5E1A5', buttonColor: '#FF5722', buttonTextColor: '#FFFFFF', dpadCenterColor: '#E64A19' },
{ gameboyColor: '#F44336', htmlColor: '#FFCDD2', buttonColor: '#E91E63', buttonTextColor: '#FFFFFF', dpadCenterColor: '#C2185B' },
{ gameboyColor: '#03A9F4', htmlColor: '#BBDEFB', buttonColor: '#FFEB3B', buttonTextColor: '#000000', dpadCenterColor: '#0277BD' },
{ gameboyColor: '#FF7043', htmlColor: '#FFCCBC', buttonColor: '#FF5722', buttonTextColor: '#FFFFFF', dpadCenterColor: '#D84315' },
{ gameboyColor: '#9C27B0', htmlColor: '#E1BEE7', buttonColor: '#7B1FA2', buttonTextColor: '#FFFFFF', dpadCenterColor: '#6A1B9A' },
{ gameboyColor: '#FFD700', htmlColor: '#FFF9C4', buttonColor: '#FF9800', buttonTextColor: '#FFFFFF', dpadCenterColor: '#F57F17' },
{ gameboyColor: '#009688', htmlColor: '#B2DFDB', buttonColor: '#4CAF50', buttonTextColor: '#FFFFFF', dpadCenterColor: '#00796B' },
{ gameboyColor: '#795548', htmlColor: '#D7CCC8', buttonColor: '#9E9E9E', buttonTextColor: '#000000', dpadCenterColor: '#5D4037' },
{ gameboyColor: '#FF5733', htmlColor: '#FFCCCB', buttonColor: '#C70039', buttonTextColor: '#FFFFFF', dpadCenterColor: '#B71C1C' },
{ gameboyColor: '#00BCD4', htmlColor: '#B2EBF2', buttonColor: '#00ACC1', buttonTextColor: '#FFFFFF', dpadCenterColor: '#00838F' }
{
gameboyColor: "#B39DDB",
htmlColor: "#D1C4E9",
buttonColor: "#673AB7",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#5E35B1",
},
{
gameboyColor: "#FFC107",
htmlColor: "#FFF9C4",
buttonColor: "#FF9800",
buttonTextColor: "#000000",
dpadCenterColor: "#EF6C00",
},
{
gameboyColor: "#8BC34A",
htmlColor: "#C5E1A5",
buttonColor: "#FF5722",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#E64A19",
},
{
gameboyColor: "#F44336",
htmlColor: "#FFCDD2",
buttonColor: "#E91E63",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#C2185B",
},
{
gameboyColor: "#03A9F4",
htmlColor: "#BBDEFB",
buttonColor: "#FFEB3B",
buttonTextColor: "#000000",
dpadCenterColor: "#0277BD",
},
{
gameboyColor: "#FF7043",
htmlColor: "#FFCCBC",
buttonColor: "#FF5722",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#D84315",
},
{
gameboyColor: "#9C27B0",
htmlColor: "#E1BEE7",
buttonColor: "#7B1FA2",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#6A1B9A",
},
{
gameboyColor: "#FFD700",
htmlColor: "#FFF9C4",
buttonColor: "#FF9800",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#F57F17",
},
{
gameboyColor: "#009688",
htmlColor: "#B2DFDB",
buttonColor: "#4CAF50",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#00796B",
},
{
gameboyColor: "#795548",
htmlColor: "#D7CCC8",
buttonColor: "#9E9E9E",
buttonTextColor: "#000000",
dpadCenterColor: "#5D4037",
},
{
gameboyColor: "#FF5733",
htmlColor: "#FFCCCB",
buttonColor: "#C70039",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#B71C1C",
},
{
gameboyColor: "#00BCD4",
htmlColor: "#B2EBF2",
buttonColor: "#00ACC1",
buttonTextColor: "#FFFFFF",
dpadCenterColor: "#00838F",
},
];
let currentColorIndex = localStorage.getItem('gameboyColorIndex') ? parseInt(localStorage.getItem('gameboyColorIndex')) : 0;
let currentColorIndex = localStorage.getItem("gameboyColorIndex")
? parseInt(localStorage.getItem("gameboyColorIndex"))
: 0;
function updateGameBoyColor() {
gameboy.style.backgroundColor = colors[currentColorIndex].gameboyColor;
html.style.backgroundColor = colors[currentColorIndex].htmlColor;
body.style.backgroundColor = colors[currentColorIndex].htmlColor;
gameboy.style.backgroundColor = colors[currentColorIndex].gameboyColor;
html.style.backgroundColor = colors[currentColorIndex].htmlColor;
body.style.backgroundColor = colors[currentColorIndex].htmlColor;
dpadButtons.forEach(button => {
button.style.backgroundColor = colors[currentColorIndex].buttonColor;
button.style.color = colors[currentColorIndex].buttonTextColor;
});
dpadButtons.forEach((button) => {
button.style.backgroundColor = colors[currentColorIndex].buttonColor;
button.style.color = colors[currentColorIndex].buttonTextColor;
});
// Using darker dpad center color
dpadCenter.style.backgroundColor = colors[currentColorIndex].dpadCenterColor;
dpadCenter.style.color = colors[currentColorIndex].buttonTextColor;
// Using darker dpad center color
dpadCenter.style.backgroundColor = colors[currentColorIndex].dpadCenterColor;
dpadCenter.style.color = colors[currentColorIndex].buttonTextColor;
actionButtons.forEach(button => {
button.style.backgroundColor = colors[currentColorIndex].buttonColor;
button.style.color = colors[currentColorIndex].buttonTextColor;
});
actionButtons.forEach((button) => {
button.style.backgroundColor = colors[currentColorIndex].buttonColor;
button.style.color = colors[currentColorIndex].buttonTextColor;
});
}
aBtn.addEventListener('click', () => {
currentColorIndex = (currentColorIndex - 1 + colors.length) % colors.length;
localStorage.setItem('gameboyColorIndex', currentColorIndex);
updateGameBoyColor();
aBtn.addEventListener("click", () => {
currentColorIndex = (currentColorIndex - 1 + colors.length) % colors.length;
localStorage.setItem("gameboyColorIndex", currentColorIndex);
updateGameBoyColor();
});
bBtn.addEventListener('click', () => {
currentColorIndex = (currentColorIndex + 1) % colors.length;
localStorage.setItem('gameboyColorIndex', currentColorIndex);
updateGameBoyColor();
bBtn.addEventListener("click", () => {
currentColorIndex = (currentColorIndex + 1) % colors.length;
localStorage.setItem("gameboyColorIndex", currentColorIndex);
updateGameBoyColor();
});
updateGameBoyColor();