<style>
#gameContainer {
margin: 0;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #000;
font-family: 'PixelFont', sans-serif;
position: relative;
border: 2px solid #fff;
}
#gameCanvas {
background-color: #000;
display: block;
min-width: 500px;
min-height: 500px;
}
#score {
color: #fff;
font-size: 24px;
position: absolute;
top: 20px;
font-family: 'PixelFont', sans-serif;
}
#startButton {
color: #fff;
font-size: 24px;
font-family: 'PixelFont', sans-serif;
animation: blink 1s infinite;
position: absolute;
}
@keyframes blink {
50% { opacity: 0; }
}
#colorForm {
position: relative;
background: black;
padding: 10px;
border-radius: 0px;
}
#mobileControls {
position: absolute;
bottom: 10px;
display: flex;
gap: 10px;
}
#mobileControls button {
padding: 10px;
font-size: 18px;
font-family: 'PixelFont', sans-serif;
}
</style>
<div id="gameContainer">
<div id="score">Score: 0</div>
<canvas id="gameCanvas"></canvas>
<button id="startButton">START</button>
</div>
<script>
let canvasColor = '#000';
let snakeColor = '#fff';
let foodColor = '#f00';
let borderColor = '#fff';
function updateColor(element, color) {
switch (element) {
case 'canvasColor':
canvasColor = color;
document.getElementById('gameContainer').style.backgroundColor = color;
break;
case 'snakeColor':
snakeColor = color;
break;
case 'foodColor':
foodColor = color;
break;
case 'borderColor':
borderColor = color;
document.getElementById('gameContainer').style.borderColor = color;
break;
}
draw();
saveColors(); // Call saveColors after updating colors
}
function saveColors() {
const canvasWidth = document.getElementById('canvasWidth').value || canvas.style.width;
const canvasHeight = document.getElementById('canvasHeight').value || canvas.style.height;
const css = `
<style>
#gameContainer {
background-color: ${canvasColor} !important;
border-color: ${borderColor} !important;
}
#score {
color: ${snakeColor} !important;
}
#startButton {
color: ${snakeColor} !important;
}
#gameCanvas {
background-color: ${canvasColor} !important;
width: ${canvasWidth} !important;
height: ${canvasHeight} !important;
min-width: 500px !important;
min-height: 500px !important;
}
</style>
`;
const trimmedCss = css.trim();
document.getElementById('add-css').value = trimmedCss;
document.getElementById('add-css-export').innerHTML = trimmedCss;
}
function updateCanvasSize() {
const canvasWidth = document.getElementById('canvasWidth').value;
const canvasHeight = document.getElementById('canvasHeight').value;
if (canvasWidth) {
canvas.style.width = canvasWidth;
}
if (canvasHeight) {
canvas.style.height = canvasHeight;
}
canvas.width = Math.max(canvas.offsetWidth, 500);
canvas.height = Math.max(canvas.offsetHeight, 500);
draw(); // Redraw the canvas with the new size
}
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const grid = 20;
let count = 0;
let snake = {
x: 160,
y: 160,
dx: grid,
dy: 0,
cells: [],
maxCells: 4
};
let food = {
x: 320,
y: 320
};
let score = 0;
let gameStarted = false;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function resetGame() {
snake.x = 160;
snake.y = 160;
snake.dx = grid;
snake.dy = 0;
snake.cells = [];
snake.maxCells = 4;
score = 0;
document.getElementById('score').innerText = `Score: ${score}`;
food.x = getRandomInt(0, 25) * grid;
food.y = getRandomInt(0, 25) * grid;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = canvasColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = snakeColor;
snake.cells.forEach(function(cell, index) {
ctx.fillRect(cell.x, cell.y, grid - 1, grid - 1);
if (cell.x === food.x && cell.y === food.y) {
snake.maxCells++;
score++;
document.getElementById('score').innerText = `Score: ${score}`;
food.x = getRandomInt(0, 25) * grid;
food.y = getRandomInt(0, 25) * grid;
}
for (let i = index + 1; i < snake.cells.length; i++) {
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
resetGame();
}
}
});
ctx.fillStyle = foodColor;
ctx.fillRect(food.x, food.y, grid - 1, grid - 1);
}
function update() {
if (!gameStarted) return;
if (count++ < 4) {
requestAnimationFrame(update);
return;
}
count = 0;
snake.x += snake.dx;
snake.y += snake.dy;
if (snake.x < 0) {
snake.x = canvas.width - grid;
} else if (snake.x >= canvas.width) {
snake.x = 0;
}
if (snake.y < 0) {
snake.y = canvas.height - grid;
} else if (snake.y >= canvas.height) {
snake.y = 0;
}
snake.cells.unshift({ x: snake.x, y: snake.y });
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
draw();
requestAnimationFrame(update);
}
document.addEventListener('keydown', function(e) {
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
} else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
} else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
} else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
const startButton = document.getElementById('startButton');
startButton.addEventListener('click', () => {
gameStarted = true;
startButton.style.display = 'none';
resetGame();
update();
});
canvas.width = Math.max(canvas.offsetWidth, 500);
canvas.height = Math.max(canvas.offsetHeight, 500);
draw(); // Initial draw to reflect default size
</script>
<style>
#gameContainer {
margin: 0;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #000;
font-family: 'PixelFont', sans-serif;
position: relative;
border: 2px solid #fff;
}
#gameCanvas {
background-color: #000;
display: block;
min-width: 500px;
min-height: 500px;
}
#score {
color: #fff;
font-size: 24px;
position: absolute;
top: 20px;
font-family: 'PixelFont', sans-serif;
}
#startButton {
color: #fff;
font-size: 24px;
font-family: 'PixelFont', sans-serif;
animation: blink 1s infinite;
position: absolute;
}
@keyframes blink {
50% { opacity: 0; }
}
#colorForm {
position: relative;
background: black;
padding: 10px;
border-radius: 0px;
}
#mobileControls {
position: absolute;
bottom: 10px;
display: flex;
gap: 10px;
}
#mobileControls button {
padding: 10px;
font-size: 18px;
font-family: 'PixelFont', sans-serif;
}
</style>
<div id="gameContainer">
<div id="score">Score: 0</div>
<canvas id="gameCanvas"></canvas>
<button id="startButton">START</button>
</div>
<script>
let canvasColor = '#000';
let snakeColor = '#fff';
let foodColor = '#f00';
let borderColor = '#fff';
function updateColor(element, color) {
switch (element) {
case 'canvasColor':
canvasColor = color;
document.getElementById('gameContainer').style.backgroundColor = color;
break;
case 'snakeColor':
snakeColor = color;
break;
case 'foodColor':
foodColor = color;
break;
case 'borderColor':
borderColor = color;
document.getElementById('gameContainer').style.borderColor = color;
break;
}
draw();
saveColors(); // Call saveColors after updating colors
}
function saveColors() {
const canvasWidth = document.getElementById('canvasWidth').value || canvas.style.width;
const canvasHeight = document.getElementById('canvasHeight').value || canvas.style.height;
const css = `
<style>
#gameContainer {
background-color: ${canvasColor} !important;
border-color: ${borderColor} !important;
}
#score {
color: ${snakeColor} !important;
}
#startButton {
color: ${snakeColor} !important;
}
#gameCanvas {
background-color: ${canvasColor} !important;
width: ${canvasWidth} !important;
height: ${canvasHeight} !important;
min-width: 500px !important;
min-height: 500px !important;
}
</style>
`;
const trimmedCss = css.trim();
document.getElementById('add-css').value = trimmedCss;
document.getElementById('add-css-export').innerHTML = trimmedCss;
}
function updateCanvasSize() {
const canvasWidth = document.getElementById('canvasWidth').value;
const canvasHeight = document.getElementById('canvasHeight').value;
if (canvasWidth) {
canvas.style.width = canvasWidth;
}
if (canvasHeight) {
canvas.style.height = canvasHeight;
}
canvas.width = Math.max(canvas.offsetWidth, 500);
canvas.height = Math.max(canvas.offsetHeight, 500);
draw(); // Redraw the canvas with the new size
}
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const grid = 20;
let count = 0;
let snake = {
x: 160,
y: 160,
dx: grid,
dy: 0,
cells: [],
maxCells: 4
};
let food = {
x: 320,
y: 320
};
let score = 0;
let gameStarted = false;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function resetGame() {
snake.x = 160;
snake.y = 160;
snake.dx = grid;
snake.dy = 0;
snake.cells = [];
snake.maxCells = 4;
score = 0;
document.getElementById('score').innerText = `Score: ${score}`;
food.x = getRandomInt(0, 25) * grid;
food.y = getRandomInt(0, 25) * grid;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = canvasColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = snakeColor;
snake.cells.forEach(function(cell, index) {
ctx.fillRect(cell.x, cell.y, grid - 1, grid - 1);
if (cell.x === food.x && cell.y === food.y) {
snake.maxCells++;
score++;
document.getElementById('score').innerText = `Score: ${score}`;
food.x = getRandomInt(0, 25) * grid;
food.y = getRandomInt(0, 25) * grid;
}
for (let i = index + 1; i < snake.cells.length; i++) {
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
resetGame();
}
}
});
ctx.fillStyle = foodColor;
ctx.fillRect(food.x, food.y, grid - 1, grid - 1);
}
function update() {
if (!gameStarted) return;
if (count++ < 4) {
requestAnimationFrame(update);
return;
}
count = 0;
snake.x += snake.dx;
snake.y += snake.dy;
if (snake.x < 0) {
snake.x = canvas.width - grid;
} else if (snake.x >= canvas.width) {
snake.x = 0;
}
if (snake.y < 0) {
snake.y = canvas.height - grid;
} else if (snake.y >= canvas.height) {
snake.y = 0;
}
snake.cells.unshift({ x: snake.x, y: snake.y });
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
draw();
requestAnimationFrame(update);
}
document.addEventListener('keydown', function(e) {
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
} else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
} else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
} else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
const startButton = document.getElementById('startButton');
startButton.addEventListener('click', () => {
gameStarted = true;
startButton.style.display = 'none';
resetGame();
update();
});
canvas.width = Math.max(canvas.offsetWidth, 500);
canvas.height = Math.max(canvas.offsetHeight, 500);
draw(); // Initial draw to reflect default size
</script>
<style>
#gameContainer {
margin: 0;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #000;
font-family: 'PixelFont', sans-serif;
position: relative;
border: 2px solid #fff;
}
#gameCanvas {
background-color: #000;
display: block;
min-width: 500px;
min-height: 500px;
}
#score {
color: #fff;
font-size: 24px;
position: absolute;
top: 20px;
font-family: 'PixelFont', sans-serif;
}
#startButton {
color: #fff;
font-size: 24px;
font-family: 'PixelFont', sans-serif;
animation: blink 1s infinite;
position: absolute;
}
@keyframes blink {
50% { opacity: 0; }
}
#colorForm {
position: relative;
background: black;
padding: 10px;
border-radius: 0px;
}
#mobileControls {
position: absolute;
bottom: 10px;
display: flex;
gap: 10px;
}
#mobileControls button {
padding: 10px;
font-size: 18px;
font-family: 'PixelFont', sans-serif;
}
</style>
<div id="gameContainer">
<div id="score">Score: 0</div>
<canvas id="gameCanvas"></canvas>
<button id="startButton">START</button>
</div>
<script>
let canvasColor = '#000';
let snakeColor = '#fff';
let foodColor = '#f00';
let borderColor = '#fff';
function updateColor(element, color) {
switch (element) {
case 'canvasColor':
canvasColor = color;
document.getElementById('gameContainer').style.backgroundColor = color;
break;
case 'snakeColor':
snakeColor = color;
break;
case 'foodColor':
foodColor = color;
break;
case 'borderColor':
borderColor = color;
document.getElementById('gameContainer').style.borderColor = color;
break;
}
draw();
saveColors(); // Call saveColors after updating colors
}
function saveColors() {
const canvasWidth = document.getElementById('canvasWidth').value || canvas.style.width;
const canvasHeight = document.getElementById('canvasHeight').value || canvas.style.height;
const css = `
<style>
#gameContainer {
background-color: ${canvasColor} !important;
border-color: ${borderColor} !important;
}
#score {
color: ${snakeColor} !important;
}
#startButton {
color: ${snakeColor} !important;
}
#gameCanvas {
background-color: ${canvasColor} !important;
width: ${canvasWidth} !important;
height: ${canvasHeight} !important;
min-width: 500px !important;
min-height: 500px !important;
}
</style>
`;
const trimmedCss = css.trim();
document.getElementById('add-css').value = trimmedCss;
document.getElementById('add-css-export').innerHTML = trimmedCss;
}
function updateCanvasSize() {
const canvasWidth = document.getElementById('canvasWidth').value;
const canvasHeight = document.getElementById('canvasHeight').value;
if (canvasWidth) {
canvas.style.width = canvasWidth;
}
if (canvasHeight) {
canvas.style.height = canvasHeight;
}
canvas.width = Math.max(canvas.offsetWidth, 500);
canvas.height = Math.max(canvas.offsetHeight, 500);
draw(); // Redraw the canvas with the new size
}
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const grid = 20;
let count = 0;
let snake = {
x: 160,
y: 160,
dx: grid,
dy: 0,
cells: [],
maxCells: 4
};
let food = {
x: 320,
y: 320
};
let score = 0;
let gameStarted = false;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function resetGame() {
snake.x = 160;
snake.y = 160;
snake.dx = grid;
snake.dy = 0;
snake.cells = [];
snake.maxCells = 4;
score = 0;
document.getElementById('score').innerText = `Score: ${score}`;
food.x = getRandomInt(0, 25) * grid;
food.y = getRandomInt(0, 25) * grid;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = canvasColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = snakeColor;
snake.cells.forEach(function(cell, index) {
ctx.fillRect(cell.x, cell.y, grid - 1, grid - 1);
if (cell.x === food.x && cell.y === food.y) {
snake.maxCells++;
score++;
document.getElementById('score').innerText = `Score: ${score}`;
food.x = getRandomInt(0, 25) * grid;
food.y = getRandomInt(0, 25) * grid;
}
for (let i = index + 1; i < snake.cells.length; i++) {
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
resetGame();
}
}
});
ctx.fillStyle = foodColor;
ctx.fillRect(food.x, food.y, grid - 1, grid - 1);
}
function update() {
if (!gameStarted) return;
if (count++ < 4) {
requestAnimationFrame(update);
return;
}
count = 0;
snake.x += snake.dx;
snake.y += snake.dy;
if (snake.x < 0) {
snake.x = canvas.width - grid;
} else if (snake.x >= canvas.width) {
snake.x = 0;
}
if (snake.y < 0) {
snake.y = canvas.height - grid;
} else if (snake.y >= canvas.height) {
snake.y = 0;
}
snake.cells.unshift({ x: snake.x, y: snake.y });
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
draw();
requestAnimationFrame(update);
}
document.addEventListener('keydown', function(e) {
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
} else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
} else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
} else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
const startButton = document.getElementById('startButton');
startButton.addEventListener('click', () => {
gameStarted = true;
startButton.style.display = 'none';
resetGame();
update();
});
canvas.width = Math.max(canvas.offsetWidth, 500);
canvas.height = Math.max(canvas.offsetHeight, 500);
draw(); // Initial draw to reflect default size
</script>
You can keep your changes by saving your game to the dashboard, and copy them again later.