Sure! Below is a complete, single-file HTML5 Snake game that features vibrant, dynamic colors with flashing effects. This game leverages HTML, CSS, and JavaScript to create an engaging and colorful experience.
How to Run the Game
- Copy the Code: Copy the entire code provided below.
- Create an HTML File: Open a text editor (like Notepad, VS Code, or Sublime Text) and paste the copied code into a new file.
- Save the File: Save the file with a
.html extension, for example, colorful_snake_game.html.
- Open in Browser: Double-click the saved file or right-click and choose "Open with" followed by your preferred web browser (e.g., Chrome, Firefox, Edge).
The game will start automatically. Use the arrow keys on your keyboard to control the snake.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Colorful Snake Game</title>
<style>
body {
margin: 0;
background: #111;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
font-family: Arial, sans-serif;
color: #fff;
}
#gameCanvas {
background: #222;
border: 2px solid #555;
box-shadow: 0 0 20px #00ffea, inset 0 0 10px #000;
}
#score {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
font-size: 24px;
text-shadow: 0 0 10px #fff;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 36px;
display: none;
text-align: center;
color: #ff4081;
text-shadow: 0 0 10px #fff;
}
#gameOver button {
margin-top: 20px;
padding: 10px 20px;
font-size: 20px;
background: #00e676;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
#gameOver button:hover {
background: #00c853;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<div id="gameOver">
<div>Game Over!</div>
<button onclick="initGame()">Restart</button>
</div>
<canvas id="gameCanvas" width="600" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('gameOver');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake;
let apple;
let dx = gridSize;
let dy = 0;
let direction = 'RIGHT';
let score = 0;
let gameInterval;
let hue = 0;
class SnakePart {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
function initGame() {
snake = [
new SnakePart(10 * gridSize, 10 * gridSize),
new SnakePart(9 * gridSize, 10 * gridSize),
new SnakePart(8 * gridSize, 10 * gridSize)
];
placeApple();
direction = 'RIGHT';
dx = gridSize;
dy = 0;
score = 0;
scoreElement.textContent = `Score: ${score}`;
gameOverElement.style.display = 'none';
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 100);
}
function placeApple() {
apple = {
x: Math.floor(Math.random() * tileCount) * gridSize,
y: Math.floor(Math.random() * tileCount) * gridSize
};
// Ensure apple doesn't spawn on the snake
snake.forEach(part => {
if (part.x === apple.x && part.y === apple.y) {
placeApple();
}
});
}
function gameLoop() {
updateSnake();
if (checkCollision()) {
clearInterval(gameInterval);
gameOverElement.style.display = 'block';
return;
}
if (snake[0].x === apple.x && snake[0].y === apple.y) {
eatApple();
}
draw();
}
function updateSnake() {
const head = snake[0];
const newHead = new SnakePart(head.x + dx, head.y + dy);
snake.unshift(newHead);
snake.pop();
}
function checkCollision() {
const head = snake[0];
// Wall collision
if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) {
return true;
}
// Self collision
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
return true;
}
}
return false;
}
function eatApple() {
snake.push(new SnakePart(snake[snake.length - 1].x, snake[snake.length - 1].y));
score++;
scoreElement.textContent = `Score: ${score}`;
placeApple();
// Increase speed
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, Math.max(50, 100 - score));
}
function draw() {
// Clear canvas with a semi-transparent background for trailing effect
ctx.fillStyle = 'rgba(34, 34, 34, 0.8)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw apple with flashing colors
ctx.fillStyle = `hsl(${ (hue + 120) % 360 }, 100%, 50%)`;
ctx.fillRect(apple.x, apple.y, gridSize, gridSize);
// Draw snake with moving colors
for (let i = 0; i < snake.length; i++) {
const part = snake[i];
ctx.fillStyle = `hsl(${ (hue + i * 20) % 360 }, 100%, 50%)`;
ctx.fillRect(part.x, part.y, gridSize, gridSize);
}
hue = (hue + 1) % 360;
}
// Handle keyboard input
document.addEventListener('keydown', function(e) {
switch(e.key) {
case 'ArrowUp':
if (direction !== 'DOWN') {
dx = 0;
dy = -gridSize;
direction = 'UP';
}
break;
case 'ArrowDown':
if (direction !== 'UP') {
dx = 0;
dy = gridSize;
direction = 'DOWN';
}
break;
case 'ArrowLeft':
if (direction !== 'RIGHT') {
dx = -gridSize;
dy = 0;
direction = 'LEFT';
}
break;
case 'ArrowRight':
if (direction !== 'LEFT') {
dx = gridSize;
dy = 0;
direction = 'RIGHT';
}
break;
}
});
// Start the game for the first time
initGame();
</script>
</body>
</html>
Features of This Game
- Dynamic Colors: The snake and the apple continuously cycle through hues, creating a vibrant rainbow effect.
- Flashing Effects: The apple changes color smoothly, making it visually appealing.
- Responsive Controls: Use the arrow keys to navigate the snake.
- Score Display: Your current score is displayed at the top center of the screen.
- Game Over Screen: When the snake collides with the walls or itself, a "Game Over" message appears with an option to restart.
- Speed Increase: The game gradually speeds up as you collect more apples, increasing the challenge.
Customization
Feel free to tweak the game parameters to suit your preferences:
- Canvas Size: Adjust the
width and height attributes of the <canvas> element to change the game's size.
- Grid Size: Modify the
gridSize variable to make the tiles larger or smaller.
- Speed: Change the initial interval in
setInterval(gameLoop, 100) to make the game start faster or slower.
- Colors: You can adjust the HSL values in the
draw function to experiment with different color schemes.
Enjoy your colorful Snake game!