Code snake in html make it cool, touchscreen, and the snake neon green add color visual effects too all in ond html file
Assistant
Certainly! Below is a complete single HTML file implementing a cool, touchscreen-compatible Snake game with a neon green snake and colorful visual effects.
Features:
Neon green snake with glow effect.
Responsive canvas adjusting to the window size.
Touch controls with swipe gestures.
Keyboard controls (arrow keys).
Colorful glowing apple.
Smooth animations and stylish background.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Neon Green Snake Game - Touch & Keyboard</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Orbitron&display=swap');
html, body {
margin: 0;
padding: 0;
background: radial-gradient(circle at center, #000000 0%, #080a0b 100%);
overflow: hidden;
height: 100%;
font-family: 'Orbitron', monospace;
user-select: none;
}
canvas {
display: block;
margin: 0 auto;
background:
radial-gradient(circle at center, #0f0 1px, transparent 2px),
radial-gradient(circle at center, #0f0 1px, transparent 2px);
background-position: 0 0, 25px 25px;
background-size: 50px 50px;
/* give illusion of grid */
filter: drop-shadow(0 0 8px #00ff00aa);
}
#scoreboard {
position: fixed;
top: 10px;
left: 50%;
transform: translateX(-50%);
color: #0f0;
font-size: 24px;
text-shadow:
0 0 5px #0f0,
0 0 10px #0f0,
0 0 20px #00ff00,
0 0 30px #00ff00,
0 0 40px #00ff00;
z-index: 10;
letter-spacing: 2px;
}
#message {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #0f0;
font-size: 32px;
text-shadow:
0 0 10px #00ff00,
0 0 20px #00ff00,
0 0 30px #00ff00;
display: none;
z-index: 20;
text-align: center;
}
button {
background: none;
border: 2px solid #0f0;
padding: 10px 20px;
color: #0f0;
font-family: 'Orbitron', monospace;
font-size: 18px;
cursor: pointer;
text-shadow: 0 0 5px #0f0;
margin-top: 20px;
border-radius: 6px;
transition: background 0.3s, color 0.3s;
}
button:hover {
background: #0f0;
color: #000;
}
</style>
</head>
<body>
<div id="scoreboard">Score: 0</div>
<div id="message">
Game Over<br/>
<button id="restartBtn">Restart</button>
</div>
<canvas id="game"></canvas>
<script>
(() => {
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const scoreboard = document.getElementById('scoreboard');
const message = document.getElementById('message');
const restartBtn = document.getElementById('restartBtn');
// Grid and scale
let scale;
let columns;
let rows;
// Direction vectors
const directions = {
ArrowUp: { x: 0, y: -1 },
ArrowDown: { x: 0, y: 1 },
ArrowLeft: { x: -1, y: 0 },
ArrowRight: { x: 1, y: 0 },
};
// Game variables
let snake;
let apple;
let direction;
let nextDirection;
let score;
let speed; // speed in ms per movement
let lastMoveTime;
let running;
// Touch control variables
let touchStartX = 0;
let touchStartY = 0;
const minSwipeDist = 30;
// Neon green glow colors
const snakeColor = '#00FF00';
const snakeGlow = '#0f0';
// Apple neon glow colors
const appleColor = '#FF0055';
const appleGlow = '#ff3399';
function resizeCanvas() {
// Take 90% of smaller dimension
const size = Math.min(window.innerWidth, window.innerHeight) * 0.9;
canvas.width = size - (size % 25);
canvas.height = size - (size % 25);
scale = 25;
columns = canvas.width / scale;
rows = canvas.height / scale;
}
function init() {
resizeCanvas();
// Initialize snake in center
snake = [
{ x: Math.floor(columns / 2), y: Math.floor(rows / 2) },
{ x: Math.floor(columns / 2) - 1, y: Math.floor(rows / 2) },
{ x: Math.floor(columns / 2) - 2, y: Math.floor(rows / 2) },
];
direction = { x: 1, y: 0 };
nextDirection = direction;
score = 0;
speed = 120; // ms per frame move
lastMoveTime = 0;
running = true;
placeApple();
scoreboard.textContent = `Score: ${score}`;
message.style.display = 'none';
}
function placeApple() {
while (true) {
apple = {
x: Math.floor(Math.random() * columns),
y: Math.floor(Math.random() * rows),
};
if (!snake.some(s => s.x === apple.x && s.y === apple.y)) break;
}
}
function drawGrid() {
// Already created background grid with CSS, no need to draw grid lines here
}
// Function to draw the snake with neon glow
function drawSnake() {
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
// Draw shadow glow
ctx.shadowColor = snakeGlow;
ctx.shadowBlur = 15;
ctx.fillStyle = snakeColor;
snake.forEach(segment => {
let px = segment.x * scale;
let py = segment.y * scale;
// Draw a neon glowing rounded rectangle
drawRoundedRect(px, py, scale, scale, scale / 3);
});
ctx.shadowBlur = 0;
}
// Function to draw apple with neon glow
function drawApple() {
const px = apple.x * scale + scale / 2;
const py = apple.y * scale + scale / 2;
ctx.shadowColor = appleGlow;
ctx.shadowBlur = 20;
// Neon glowing circle
const gradient = ctx.createRadialGradient(px, py, scale * 0.1, px, py, scale * 0.6);
gradient.addColorStop(0, appleColor);
gradient.addColorStop(1, 'rgba(255,0,85,0)');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(px, py, scale * 0.6, 0, Math.PI * 2);
ctx.fill();
// Core brighter circle
ctx.shadowBlur = 0;
ctx.fillStyle = appleColor;
ctx.beginPath();
ctx.arc(px, py, scale * 0.3, 0, Math.PI * 2);
ctx.fill();
}
// Rounded rectangle helper
function drawRoundedRect(x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
ctx.fill();
}
// Game update logic
function update(deltaTime) {
if (!running) return;
lastMoveTime += deltaTime;
if (lastMoveTime < speed) return;
lastMoveTime = 0;
// Update direction
if (
nextDirection.x !== -direction.x ||
nextDirection.y !== -direction.y
) {
direction = nextDirection;
}
// New head position
const head = {
x: (snake[0].x + direction.x + columns) % columns,
y: (snake[0].y + direction.y + rows) % rows,
};
// Check collision with self
if (snake.some(seg => seg.x === head.x && seg.y === head.y)) {
gameOver();
return;
}
snake.unshift(head);
// Check if apple eaten
if (head.x === apple.x && head.y === apple.y) {
score++;
scoreboard.textContent = `Score: ${score}`;
placeApple();
// Increase speed every 5 points to 50ms min
if (score % 5 === 0 && speed > 50) {
speed -= 5;
}
} else {
snake.pop();
}
}
// Clear canvas
function clear() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Draw all game elements
function draw() {
clear();
drawGrid(); // grid drawn with CSS background already
drawApple();
drawSnake();
}
// Game Loop
let lastTimestamp = 0;
function gameLoop(timestamp = 0) {
let deltaTime = timestamp - lastTimestamp;
lastTimestamp = timestamp;
update(deltaTime);
draw();
if (running) requestAnimationFrame(gameLoop);
}
// Game over function
function gameOver() {
running = false;
message.style.display = 'block';
}
// Input handlers
function keyDownHandler(e) {
if (!directions[e.key]) return;
const newDir = directions[e.key];
if (newDir.x === -direction.x && newDir.y === -direction.y) return; // No reverse
nextDirection = newDir;
e.preventDefault();
}
// Touch handlers for swipe detection
function touchStartHandler(e) {
if (e.touches.length !== 1) return;
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
}
function touchMoveHandler(e) {
if (e.touches.length !== 1) return;
const deltaX = e.touches[0].clientX - touchStartX;
const deltaY = e.touches[0].clientY - touchStartY;
if (Math.abs(deltaX) < minSwipeDist && Math.abs(deltaY) < minSwipeDist) return;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
// Horizontal swipe
if (deltaX > 0 && direction.x !== -1) { // swipe right
nextDirection = { x: 1, y: 0 };
} else if (deltaX < 0 && direction.x !== 1) { // swipe left
nextDirection = { x: -1, y: 0 };
}
} else {
// Vertical swipe
if (deltaY > 0 && direction.y !== -1) { // swipe down
nextDirection = { x: 0, y: 1 };
} else if (deltaY < 0 && direction.y !== 1) { // swipe up
nextDirection = { x: 0, y: -1 };
}
}
touchStartX = e.touches[0].clientX; // reset start to allow continuous swipes
touchStartY = e.touches[0].clientY;
}
// Restart game
restartBtn.addEventListener('click', () => {
init();
requestAnimationFrame(gameLoop);
});
// Resize handler
window.addEventListener('resize', () => {
// Save relative snake positions and apple positions as percentages
// then recalibrate according to new columns and rows
const snakePercent = snake.map(s => ({x: s.x / columns, y: s.y / rows}));
const applePercent = { x: apple.x / columns, y: apple.y / rows };
resizeCanvas();
snake = snakePercent.map(p => ({
x: Math.floor(p.x * columns),
y: Math.floor(p.y * rows),
}));
apple = {
x: Math.floor(applePercent.x * columns),
y: Math.floor(applePercent.y * rows),
};
});
// Event listeners
window.addEventListener('keydown', keyDownHandler);
canvas.addEventListener('touchstart', touchStartHandler);
canvas.addEventListener('touchmove', touchMoveHandler);
// Start
init();
requestAnimationFrame(gameLoop);
})();
</script>
</body>
</html>
How to use
Save the above code into an .html file.
Open it in any modern browser (desktop or mobile).
Use arrow keys or swipe on touchscreen to control the snake.
Eat the neon glowing apple to grow and score points.
The snake wraps around the edges.
Game speeds up every 5 points.
When you collide with yourself, the game over message appears with a restart button.