Snake Game with a Twist: Obstacles and Power-Ups
In this version of the classic Snake game, we'll add obstacles and power-ups to make the game more challenging and exciting.
Gameplay
- The snake can move up, down, left, or right using the arrow keys.
- The snake must eat the food pellets to grow longer.
- The game features obstacles that the snake must avoid.
- Power-ups will appear randomly, giving the snake a temporary boost or penalty.
Code
We'll use HTML, CSS, and JavaScript to create the game.
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
background-color: #f0f0f0;
text-align: center;
}
#gameCanvas {
border: 1px solid black;
}
JavaScript (script.js)
// Get the canvas element
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set the canvas dimensions
canvas.width = 400;
canvas.height = 400;
// Define game variables
let snake = [
{ x: 200, y: 200 },
{ x: 190, y: 200 },
{ x: 180, y: 200 }
];
let direction = 'right';
let food = { x: Math.floor(Math.random() * 40) * 10, y: Math.floor(Math.random() * 40) * 10 };
let obstacles = [];
let powerUps = [];
let score = 0;
let gameOver = false;
// Draw the game board
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw the snake
ctx.fillStyle = 'green';
for (let i = 0; i < snake.length; i++) {
ctx.fillRect(snake[i].x, snake[i].y, 10, 10);
}
// Draw the food
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, 10, 10);
// Draw obstacles
ctx.fillStyle = 'gray';
for (let i = 0; i < obstacles.length; i++) {
ctx.fillRect(obstacles[i].x, obstacles[i].y, 10, 10);
}
// Draw power-ups
ctx.fillStyle = 'blue';
for (let i = 0; i < powerUps.length; i++) {
ctx.fillRect(powerUps[i].x, powerUps[i].y, 10, 10);
}
// Draw the score
ctx.fillStyle = 'white';
ctx.font = '24px Arial';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillText(`Score: ${score}`, 10, 10);
}
// Update the game state
function update() {
if (gameOver) return;
// Move the snake
let newHead = { x: snake[0].x, y: snake[0].y };
switch (direction) {
case 'up':
newHead.y -= 10;
break;
case 'down':
newHead.y += 10;
break;
case 'left':
newHead.x -= 10;
break;
case 'right':
newHead.x += 10;
break;
}
snake.unshift(newHead);
// Check for collision with food
if (snake[0].x === food.x && snake[0].y === food.y) {
score++;
food = { x: Math.floor(Math.random() * 40) * 10, y: Math.floor(Math.random() * 40) * 10 };
} else {
snake.pop();
}
// Check for collision with obstacles
for (let i = 0; i < obstacles.length; i++) {
if (snake[0].x === obstacles[i].x && snake[0].y === obstacles[i].y) {
gameOver = true;
}
}
// Check for collision with power-ups
for (let i = 0; i < powerUps.length; i++) {
if (snake[0].x === powerUps[i].x && snake[0].y === powerUps[i].y) {
// Apply power-up effect
if (powerUps[i].type === 'boost') {
score += 5;
} else if (powerUps[i].type === 'penalty') {
score -= 2;
}
powerUps.splice(i, 1);
}
}
// Check for collision with self or boundary
if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height || checkCollision(snake[0], snake.slice(1))) {
gameOver = true;
}
// Randomly generate obstacles and power-ups
if (Math.random() < 0.1) {
obstacles.push({ x: Math.floor(Math.random() * 40) * 10, y: Math.floor(Math.random() * 40) * 10 });
}
if (Math.random() < 0.05) {
powerUps.push({ x: Math.floor(Math.random() * 40) * 10, y: Math.floor(Math.random() * 40) * 10, type: Math.random() < 0.5 ? 'boost' : 'penalty' });
}
}
// Check for collision between two points
function checkCollision(point1, points) {
for (let i = 0; i < points.length; i++) {
if (point1.x === points[i].x && point1.y === points[i].y) {
return true;
}
}
return false;
}
// Handle keyboard input
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'ArrowUp':
if (direction !== 'down') direction = 'up';
break;
case 'ArrowDown':
if (direction !== 'up') direction = 'down';
break;
case 'ArrowLeft':
if (direction !== 'right') direction = 'left';
break;
case 'ArrowRight':
if (direction !== 'left') direction = 'right';
break;
}
});
// Main game loop
setInterval(() => {
update();
draw();
if (gameOver) {
ctx.fillStyle = 'white';
ctx.font = '48px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('Game Over!', canvas.width / 2, canvas.height / 2);
}
}, 100);
How to Run the Game
- Create a new HTML file called
index.html and copy the HTML code into it.
- Create a new CSS file called
styles.css and copy the CSS code into it.
- Create a new JavaScript file called
script.js and copy the JavaScript code into it.
- Open
index.html in a web browser to play the game.
Gameplay Tips
- Use the arrow keys to control the snake.
- Eat food pellets to grow longer and earn points.
- Avoid obstacles and power-ups to survive.
- Power-ups can give you a boost or penalty, so be careful!
Have fun playing the game!