Voici un jeu Snake simple en JavaScript :
const canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 400;
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
const gridSize = 20;
let snake = [{x: 10, y: 10}];
let direction = {x: 1, y: 0};
let food = {x: 15, y: 15};
let score = 0;
let gameOver = false;
function drawSnake() {
ctx.fillStyle = 'green';
snake.forEach(segment => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
});
}
function drawFood() {
ctx.fillStyle = 'red';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}
function moveSnake() {
if (gameOver) return;
const head = {
x: snake[0].x + direction.x,
y: snake[0].y + direction.y
};
// Check collision with walls
if (head.x < 0 || head.x >= canvas.width / gridSize ||
head.y < 0 || head.y >= canvas.height / gridSize) {
gameOver = true;
return;
}
// Check collision with self
for (let i = 0; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver = true;
return;
}
}
snake.unshift(head);
// Check if snake ate food
if (head.x === food.x && head.y === food.y) {
score++;
generateFood();
} else {
snake.pop();
}
}
function generateFood() {
food = {
x: Math.floor(Math.random() * (canvas.width / gridSize)),
y: Math.floor(Math.random() * (canvas.height / gridSize))
};
// Make sure food doesn't spawn on snake
for (let i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
generateFood();
break;
}
}
}
function drawScore() {
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
}
function drawGameOver() {
ctx.fillStyle = 'black';
ctx.font = '40px Arial';
ctx.fillText('Game Over!', canvas.width / 2 - 100, canvas.height / 2);
ctx.font = '20px Arial';
ctx.fillText(`Score final: ${score}`, canvas.width / 2 - 60, canvas.height / 2 + 40);
ctx.fillText('Appuyez sur Espace pour rejouer', canvas.width / 2 - 140, canvas.height / 2 + 80);
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawFood();
moveSnake();
drawSnake();
drawScore();
if (gameOver) {
drawGameOver();
} else {
setTimeout(gameLoop, 100);
}
}
document.addEventListener('keydown', (e) => {
if (gameOver && e.code === 'Space') {
snake = [{x: 10, y: 10}];
direction = {x: 1, y: 0};
score = 0;
gameOver = false;
generateFood();
gameLoop();
return;
}
switch (e.code) {
case 'ArrowUp':
if (direction.y !== 1) direction = {x: 0, y: -1};
break;
case 'ArrowDown':
if (direction.y !== -1) direction = {x: 0, y: 1};
break;
case 'ArrowLeft':
if (direction.x !== 1) direction = {x: -1, y: 0};
break;
case 'ArrowRight':
if (direction.x !== -1) direction = {x: 1, y: 0};
break;
}
});
generateFood();
gameLoop();
Voici un aperçu visuel du jeu Snake :
+----------------------------------------+
| |
| Score: 3 |
| |
| |
| |
| 🍎 |
| |
| |
| |
| |
| |
| |
| |
| 🟩🟩🟩🟩 |
| |
| |
| |
| |
| |
+----------------------------------------+
Dans ce jeu:
- Le serpent est représenté en vert (🟩)
- La nourriture est représentée en rouge (🍎)
- Utilisez les flèches du clavier pour diriger le serpent
- Mangez la nourriture pour grandir et augmenter votre score
- Évitez de toucher les murs ou votre propre corps
- Appuyez sur Espace pour recommencer après un Game Over
Souhaitez-vous que j'explique ou détaille certaines parties du code?