<!-- Canvas pour le jeu -->
<canvas id="gameCanvas" width="800" height="600"></canvas>
<!-- Script JavaScript pour le jeu -->
<script>
// Configuration du Canvas
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Variables du jeu
let isDragging = false;
const slingX = 150;
const slingY = canvas.height - 150;
let birdX = slingX;
let birdY = slingY;
const birdRadius = 15;
let velocity = { x: 0, y: 0 };
let isFlying = false;
const gravity = 0.5;
let maxAttempts = 5;
let attemptsLeft = maxAttempts;
let score = 0;
let level = 1;
const scoreBoard = document.getElementById('scoreBoard');
const attemptsBoard = document.getElementById('attemptsBoard');
const levelBoard = document.getElementById('levelBoard');
// Liste des niveaux
const levels = [
{
obstacles: [
{ x: 550, y: canvas.height - 100, width: 50, height: 100 },
{ x: 650, y: canvas.height - 150, width: 50, height: 150 },
{ x: 600, y: canvas.height - 200, width: 50, height: 200 }
],
monsters: [
{ x: 625, y: canvas.height - 210, radius: 20, isHit: false, speedX: 1, speedY: 0, directionChangeTimer: 0 }
],
maxAttempts: 5
},
{
obstacles: [
{ x: 550, y: canvas.height - 50, width: 50, height: 50 },
{ x: 600, y: canvas.height - 100, width: 50, height: 100 },
{ x: 650, y: canvas.height - 150, width: 50, height: 150 },
{ x: 700, y: canvas.height - 200, width: 50, height: 200 }
],
monsters: [
{ x: 575, y: canvas.height - 60, radius: 20, isHit: false, speedX: 1.5, speedY: 0.5, directionChangeTimer: 0 },
{ x: 625, y: canvas.height - 110, radius: 20, isHit: false, speedX: -1.5, speedY: 0.5, directionChangeTimer: 0 }
],
maxAttempts: 5
},
{
obstacles: [
{ x: 550, y: canvas.height - 50, width: 50, height: 50 },
{ x: 600, y: canvas.height - 50, width: 50, height: 50 },
{ x: 650, y: canvas.height - 50, width: 50, height: 50 },
{ x: 700, y: canvas.height - 50, width: 50, height: 50 }
],
monsters: [
{ x: 575, y: canvas.height - 60, radius: 20, isHit: false, speedX: 2, speedY: 1, directionChangeTimer: 0 },
{ x: 625, y: canvas.height - 60, radius: 20, isHit: false, speedX: -2, speedY: 1, directionChangeTimer: 0 }
],
maxAttempts: 6
}
// Vous pouvez ajouter plus de niveaux ici
];
// Variables du niveau courant
let currentLevelIndex = 0;
let obstacles = [];
let monsters = [];
// Nuages en mouvement
let clouds = [
{ x: 100, y: 100, speedX: 0.5 },
{ x: 400, y: 80, speedX: 0.3 },
{ x: 600, y: 120, speedX: 0.4 }
];
// Volcans
let volcanoes = [
{ x: 200, y: canvas.height - 300, particleTimer: 0 },
{ x: 500, y: canvas.height - 250, particleTimer: 0 }
];
// Particules de lave
let lavaParticles = [];
// Charger le niveau
function loadLevel(levelIndex) {
level = levelIndex + 1;
levelBoard.textContent = 'Niveau : ' + level;
const levelData = levels[levelIndex];
obstacles = JSON.parse(JSON.stringify(levelData.obstacles));
monsters = JSON.parse(JSON.stringify(levelData.monsters));
attemptsLeft = levelData.maxAttempts;
maxAttempts = levelData.maxAttempts;
birdX = slingX;
birdY = slingY;
velocity = { x: 0, y: 0 };
isFlying = false;
isDragging = false;
lavaParticles = []; // Réinitialiser les particules de lave
updateScore();
updateAttempts();
}
// Gestion des événements de la souris
canvas.addEventListener('mousedown', function(e) {
if (attemptsLeft > 0 && !isFlying) {
let rect = canvas.getBoundingClientRect();
let mouseX = e.clientX - rect.left;
let mouseY = e.clientY - rect.top;
// Vérifier si la souris est sur l'oiseau
if (Math.hypot(mouseX - birdX, mouseY - birdY) < birdRadius) {
isDragging = true;
}
}
});
canvas.addEventListener('mousemove', function(e) {
if (isDragging && !isFlying) {
let rect = canvas.getBoundingClientRect();
birdX = e.clientX - rect.left;
birdY = e.clientY - rect.top;
// Limiter le déplacement de l'oiseau
let dx = birdX - slingX;
let dy = birdY - slingY;
let distance = Math.hypot(dx, dy);
let maxDistance = 100; // Distance maximale pour tirer
if (distance > maxDistance) {
let angle = Math.atan2(dy, dx);
birdX = slingX + maxDistance * Math.cos(angle);
birdY = slingY + maxDistance * Math.sin(angle);
}
}
});
canvas.addEventListener('mouseup', function(e) {
if (isDragging) {
isDragging = false;
isFlying = true;
velocity.x = (slingX - birdX) / 5;
velocity.y = (slingY - birdY) / 5;
attemptsLeft--;
updateAttempts();
}
});
// Fonction de mise à jour du jeu
function update() {
// Mettre à jour les nuages
clouds.forEach(cloud => {
cloud.x += cloud.speedX;
if (cloud.x > canvas.width + 100) {
cloud.x = -150; // Revenir à gauche
}
});
// Mettre à jour les volcans et générer des particules de lave
volcanoes.forEach(volcano => {
volcano.particleTimer--;
if (volcano.particleTimer <= 0) {
volcano.particleTimer = Math.floor(Math.random() * 30 + 20); // Entre 20 et 50 frames
// Générer une particule de lave
let particle = {
x: volcano.x,
y: volcano.y - 10,
vx: (Math.random() * 2 - 1) * 2, // Vitesse horizontale aléatoire
vy: -Math.random() * 3 - 2, // Vitesse verticale vers le haut
size: Math.random() * 5 + 2, // Taille entre 2 et 7
life: 100 // Durée de vie de la particule
};
lavaParticles.push(particle);
}
});
// Mettre à jour les particules de lave
for (let i = 0; i < lavaParticles.length; i++) {
let p = lavaParticles[i];
p.x += p.vx;
p.y += p.vy;
p.vy += 0.1; // Gravité
p.life--;
if (p.life <= 0 || p.y > canvas.height) {
lavaParticles.splice(i, 1);
i--;
}
}
// Mettre à jour les monstres (ennemis) avec des mouvements imprévisibles
monsters.forEach(monster => {
monster.directionChangeTimer--;
if (monster.directionChangeTimer <= 0) {
monster.directionChangeTimer = Math.floor(Math.random() * 60 + 30); // Change direction fréquemment
// Vitesse aléatoire entre -2 et 2 pour X et Y
monster.speedX = (Math.random() * 4 - 2);
monster.speedY = (Math.random() * 4 - 2);
}
// Mettre à jour la position
monster.x += monster.speedX;
monster.y += monster.speedY;
// Limiter les mouvements dans la zone de jeu
// Limites horizontales
if (monster.x - monster.radius < 500) {
monster.x = 500 + monster.radius;
monster.speedX *= -1;
}
if (monster.x + monster.radius > canvas.width) {
monster.x = canvas.width - monster.radius;
monster.speedX *= -1;
}
// Limites verticales
if (monster.y - monster.radius < canvas.height - 300) {
monster.y = canvas.height - 300 + monster.radius;
monster.speedY *= -1;
}
if (monster.y + monster.radius > canvas.height - 50) {
monster.y = canvas.height - 50 - monster.radius;
monster.speedY *= -1;
}
});
if (isFlying) {
velocity.y += gravity;
birdX += velocity.x;
birdY += velocity.y;
// Vérifier les collisions avec les obstacles
for (let i = 0; i < obstacles.length; i++) {
let obstacle = obstacles[i];
if (rectCollision(
birdX - birdRadius,
birdY - birdRadius,
birdRadius * 2,
birdRadius * 2,
obstacle.x,
obstacle.y,
obstacle.width,
obstacle.height
)) {
// Collision détectée
// Réduire la vitesse de l'oiseau
velocity.x *= -0.5; // Rebondissement simplifié
velocity.y *= -0.5;
// Détruire l'obstacle si la vitesse est suffisante
if (Math.abs(velocity.x) + Math.abs(velocity.y) > 5) {
obstacles.splice(i, 1);
i--;
}
}
}
// Vérifier les collisions avec les monstres
for (let i = 0; i < monsters.length; i++) {
let monster = monsters[i];
if (!monster.isHit && circleCollision(birdX, birdY, birdRadius, monster.x, monster.y, monster.radius)) {
// Collision détectée
monster.isHit = true;
score += 100;
updateScore();
}
}
// Vérifier si l'oiseau sort de l'écran ou touche le sol
if (
birdY - birdRadius > canvas.height ||
birdX + birdRadius < 0 ||
birdX - birdRadius > canvas.width
) {
isFlying = false;
birdX = slingX;
birdY = slingY;
velocity = { x: 0, y: 0 };
// Vérifier si le jeu est terminé
if (monsters.every(monster => monster.isHit)) {
// Passer au niveau suivant
if (currentLevelIndex < levels.length - 1) {
alert('Niveau terminé ! Vous passez au niveau suivant.');
currentLevelIndex++;
loadLevel(currentLevelIndex);
} else {
// Afficher un message de victoire finale
alert('Félicitations ! Vous avez terminé tous les niveaux !\nScore total : ' + score);
resetGame();
}
} else if (attemptsLeft === 0) {
// Afficher un message de défaite
alert('Vous avez utilisé toutes vos tentatives.\nScore total : ' + score);
resetGame();
}
}
}
}
// Fonction de collision rectangle
function rectCollision(x1, y1, w1, h1, x2, y2, w2, h2) {
return (
x1 < x2 + w2 &&
x1 + w1 > x2 &&
y1 < y2 + h2 &&
y1 + h1 > y2
);
}
// Fonction de collision cercle
function circleCollision(x1, y1, r1, x2, y2, r2) {
let dx = x1 - x2;
let dy = y1 - y2;
let distance = Math.hypot(dx, dy);
return distance < r1 + r2;
}
// Fonction pour réinitialiser le niveau
function resetLevel() {
loadLevel(currentLevelIndex);
}
// Fonction pour réinitialiser le jeu
function resetGame() {
currentLevelIndex = 0;
score = 0;
loadLevel(currentLevelIndex);
}
// Fonction pour mettre à jour le score affiché
function updateScore() {
scoreBoard.textContent = 'Score : ' + score;
}
// Fonction pour mettre à jour les tentatives restantes affichées
function updateAttempts() {
attemptsBoard.textContent = 'Tentatives restantes : ' + attemptsLeft;
}
// Fonction pour dessiner les nuages
function drawCloud(x, y) {
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(x, y, 20, Math.PI * 0.5, Math.PI * 1.5);
ctx.arc(x + 25, y - 20, 35, Math.PI * 1, Math.PI * 1.85);
ctx.arc(x + 70, y - 15, 25, Math.PI * 1.37, Math.PI * 0.37, true);
ctx.arc(x + 50, y + 10, 35, Math.PI * 1.5, Math.PI * 0.5);
ctx.closePath();
ctx.fill();
}
// Fonction pour dessiner les volcans
function drawVolcanoes() {
volcanoes.forEach(volcano => {
// Dessiner le volcan
ctx.fillStyle = '#A9A9A9';
ctx.beginPath();
ctx.moveTo(volcano.x - 100, canvas.height - 50);
ctx.lineTo(volcano.x, volcano.y);
ctx.lineTo(volcano.x + 100, canvas.height - 50);
ctx.closePath();
ctx.fill();
// Dessiner la lave au sommet
ctx.fillStyle = 'orange';
ctx.beginPath();
ctx.moveTo(volcano.x - 15, volcano.y + 15);
ctx.lineTo(volcano.x, volcano.y);
ctx.lineTo(volcano.x + 15, volcano.y + 15);
ctx.closePath();
ctx.fill();
});
}
// Fonction pour dessiner les particules de lave
function drawLavaParticles() {
lavaParticles.forEach(particle => {
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fill();
});
}
// Fonction pour dessiner les arbres
function drawTree(x, y) {
// Tronc
ctx.fillStyle = '#8B4513';
ctx.fillRect(x + 15, y, 20, 50);
// Feuillage
ctx.fillStyle = '#228B22';
ctx.beginPath();
ctx.moveTo(x + 25, y - 75);
ctx.lineTo(x, y);
ctx.lineTo(x + 50, y);
ctx.closePath();
ctx.fill();
}
// Fonction pour dessiner le sol
function drawGround() {
// Sol avec texture d'herbe
let grassHeight = 50;
ctx.fillStyle = '#7CFC00';
ctx.fillRect(0, canvas.height - grassHeight, canvas.width, grassHeight);
// Bandes d'herbe pour la texture
ctx.fillStyle = '#66CDAA';
for (let i = 0; i < canvas.width; i += 15) {
ctx.beginPath();
ctx.moveTo(i, canvas.height - grassHeight);
ctx.lineTo(i + 10, canvas.height - grassHeight - 10);
ctx.lineTo(i + 20, canvas.height - grassHeight);
ctx.closePath();
ctx.fill();
}
}
// Fonction pour dessiner le décor
function drawBackground() {
// Dessiner le ciel (déjà bleu clair grâce au CSS)
// Dessiner les volcans
drawVolcanoes();
// Dessiner les particules de lave
drawLavaParticles();
// Dessiner les nuages en mouvement
clouds.forEach(cloud => {
drawCloud(cloud.x, cloud.y);
});
// Dessiner les arbres au sol
drawTree(50, canvas.height - 100);
drawTree(700, canvas.height - 100);
drawTree(150, canvas.height - 120);
drawTree(650, canvas.height - 120);
drawTree(250, canvas.height - 110);
drawTree(550, canvas.height - 110);
// Dessiner le sol
drawGround();
}
// Fonction pour dessiner les obstacles
function drawObstacle(obstacle) {
ctx.fillStyle = '#8B4513'; // Couleur des obstacles en bois
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
ctx.strokeStyle = '#000';
ctx.strokeRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
// Texture du bois
ctx.fillStyle = '#A0522D';
for (let y = obstacle.y; y < obstacle.y + obstacle.height; y += 10) {
ctx.fillRect(obstacle.x, y, obstacle.width, 5);
}
}
// Fonction pour dessiner les monstres
function drawMonster(monster) {
if (!monster.isHit) {
ctx.beginPath();
ctx.arc(monster.x, monster.y, monster.radius, 0, Math.PI * 2);
ctx.fillStyle = 'green';
ctx.fill();
ctx.strokeStyle = '#000';
ctx.stroke();
// Dessiner les yeux
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(monster.x - 5, monster.y - 5, 5, 0, Math.PI * 2);
ctx.arc(monster.x + 5, monster.y - 5, 5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(monster.x - 5, monster.y - 5, 2, 0, Math.PI * 2);
ctx.arc(monster.x + 5, monster.y - 5, 2, 0, Math.PI * 2);
ctx.fill();
// Dessiner la bouche
ctx.beginPath();
ctx.arc(monster.x, monster.y + 5, 7, Math.PI * 0.2, Math.PI * 0.8);
ctx.stroke();
}
}
// Fonction pour dessiner l'oiseau
function drawBird() {
// Corps de l'oiseau
ctx.beginPath();
ctx.arc(birdX, birdY, birdRadius, 0, Math.PI * 2);
ctx.fillStyle = 'red';
ctx.fill();
ctx.strokeStyle = '#000';
ctx.stroke();
// Dessiner les yeux
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(birdX - 5, birdY - 5, 5, 0, Math.PI * 2);
ctx.arc(birdX + 5, birdY - 5, 5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(birdX - 5, birdY - 5, 2, 0, Math.PI * 2);
ctx.arc(birdX + 5, birdY - 5, 2, 0, Math.PI * 2);
ctx.fill();
// Bec
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.moveTo(birdX, birdY);
ctx.lineTo(birdX + 10, birdY + 5);
ctx.lineTo(birdX, birdY + 10);
ctx.closePath();
ctx.fill();
}
// Fonction de dessin du jeu
function draw() {
// Effacer le canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Dessiner le décor
drawBackground();
// Dessiner les obstacles
obstacles.forEach(obstacle => {
drawObstacle(obstacle);
});
// Dessiner les monstres
monsters.forEach(monster => {
drawMonster(monster);
});
// Dessiner le lance-pierre
ctx.beginPath();
ctx.moveTo(slingX - 10, slingY);
ctx.lineTo(slingX + 10, slingY);
ctx.strokeStyle = '#8B4513';
ctx.lineWidth = 5;
ctx.stroke();
// Dessiner l'élastique
if (isDragging) {
ctx.beginPath();
ctx.moveTo(slingX, slingY);
ctx.lineTo(birdX, birdY);
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
ctx.stroke();
}
// Dessiner l'oiseau
drawBird();
}
// Boucle principale du jeu
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Démarrer le jeu
resetGame();
gameLoop();
</script>
Améliore le jeu en rajoutant des "explosions" graphiques quand l'oiseau touche un monstre pour rajouter plus d'animations dans le jeu
Bien sûr ! J'ai amélioré le jeu en ajoutant des animations d'explosions lorsque l'oiseau touche un monstre. Voici le code mis à jour avec les modifications nécessaires.
html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Jeu Style Angry Birds - Version Améliorée avec Volcans et Monstres Imprévisibles</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #87CEEB; /* Ciel bleu */
}
#gameCanvas {
display: block;
margin: 0 auto;
background-color: #87CEEB;
}
#scoreBoard, #attemptsBoard, #levelBoard {
position: absolute;
top: 10px;
font-size: 24px;
color: #000;
font-family: Arial, sans-serif;
}
#scoreBoard {
left: 10px;
}
#attemptsBoard {
right: 10px;
}
#levelBoard {
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<!-- Zone d'affichage du score -->
<div id="scoreBoard">Score : 0</div>
<!-- Zone d'affichage des tentatives restantes -->
<div id="attemptsBoard">Tentatives restantes : 5</div>
<!-- Zone d'affichage du niveau -->
<div id="levelBoard">Niveau : 1</div>
<!-- Canvas pour le jeu -->
<canvas id="gameCanvas" width="800" height="600"></canvas>
<!-- Script JavaScript pour le jeu -->
<script>
// Configuration du Canvas
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Variables du jeu
let isDragging = false;
const slingX = 150;
const slingY = canvas.height - 150;
let birdX = slingX;
let birdY = slingY;
const birdRadius = 15;
let velocity = { x: 0, y: 0 };
let isFlying = false;
const gravity = 0.5;
let maxAttempts = 5;
let attemptsLeft = maxAttempts;
let score = 0;
let level = 1;
const scoreBoard = document.getElementById('scoreBoard');
const attemptsBoard = document.getElementById('attemptsBoard');
const levelBoard = document.getElementById('levelBoard');
// Liste des niveaux
const levels = [
{
obstacles: [
{ x: 550, y: canvas.height - 100, width: 50, height: 100 },
{ x: 650, y: canvas.height - 150, width: 50, height: 150 },
{ x: 600, y: canvas.height - 200, width: 50, height: 200 }
],
monsters: [
{ x: 625, y: canvas.height - 210, radius: 20, isHit: false, speedX: 1, speedY: 0, directionChangeTimer: 0 }
],
maxAttempts: 5
},
{
obstacles: [
{ x: 550, y: canvas.height - 50, width: 50, height: 50 },
{ x: 600, y: canvas.height - 100, width: 50, height: 100 },
{ x: 650, y: canvas.height - 150, width: 50, height: 150 },
{ x: 700, y: canvas.height - 200, width: 50, height: 200 }
],
monsters: [
{ x: 575, y: canvas.height - 60, radius: 20, isHit: false, speedX: 1.5, speedY: 0.5, directionChangeTimer: 0 },
{ x: 625, y: canvas.height - 110, radius: 20, isHit: false, speedX: -1.5, speedY: 0.5, directionChangeTimer: 0 }
],
maxAttempts: 5
},
{
obstacles: [
{ x: 550, y: canvas.height - 50, width: 50, height: 50 },
{ x: 600, y: canvas.height - 50, width: 50, height: 50 },
{ x: 650, y: canvas.height - 50, width: 50, height: 50 },
{ x: 700, y: canvas.height - 50, width: 50, height: 50 }
],
monsters: [
{ x: 575, y: canvas.height - 60, radius: 20, isHit: false, speedX: 2, speedY: 1, directionChangeTimer: 0 },
{ x: 625, y: canvas.height - 60, radius: 20, isHit: false, speedX: -2, speedY: 1, directionChangeTimer: 0 }
],
maxAttempts: 6
}
// Vous pouvez ajouter plus de niveaux ici
];
// Variables du niveau courant
let currentLevelIndex = 0;
let obstacles = [];
let monsters = [];
// Nuages en mouvement
let clouds = [
{ x: 100, y: 100, speedX: 0.5 },
{ x: 400, y: 80, speedX: 0.3 },
{ x: 600, y: 120, speedX: 0.4 }
];
// Volcans
let volcanoes = [
{ x: 200, y: canvas.height - 300, particleTimer: 0 },
{ x: 500, y: canvas.height - 250, particleTimer: 0 }
];
// Particules de lave
let lavaParticles = [];
// Particules d'explosion
let explosions = [];
// Charger le niveau
function loadLevel(levelIndex) {
level = levelIndex + 1;
levelBoard.textContent = 'Niveau : ' + level;
const levelData = levels[levelIndex];
obstacles = JSON.parse(JSON.stringify(levelData.obstacles));
monsters = JSON.parse(JSON.stringify(levelData.monsters));
attemptsLeft = levelData.maxAttempts;
maxAttempts = levelData.maxAttempts;
birdX = slingX;
birdY = slingY;
velocity = { x: 0, y: 0 };
isFlying = false;
isDragging = false;
lavaParticles = []; // Réinitialiser les particules de lave
explosions = []; // Réinitialiser les explosions
updateScore();
updateAttempts();
}
// Gestion des événements de la souris
canvas.addEventListener('mousedown', function(e) {
if (attemptsLeft > 0 && !isFlying) {
let rect = canvas.getBoundingClientRect();
let mouseX = e.clientX - rect.left;
let mouseY = e.clientY - rect.top;
// Vérifier si la souris est sur l'oiseau
if (Math.hypot(mouseX - birdX, mouseY - birdY) < birdRadius) {
isDragging = true;
}
}
});
canvas.addEventListener('mousemove', function(e) {
if (isDragging && !isFlying) {
let rect = canvas.getBoundingClientRect();
birdX = e.clientX - rect.left;
birdY = e.clientY - rect.top;
// Limiter le déplacement de l'oiseau
let dx = birdX - slingX;
let dy = birdY - slingY;
let distance = Math.hypot(dx, dy);
let maxDistance = 100; // Distance maximale pour tirer
if (distance > maxDistance) {
let angle = Math.atan2(dy, dx);
birdX = slingX + maxDistance * Math.cos(angle);
birdY = slingY + maxDistance * Math.sin(angle);
}
}
});
canvas.addEventListener('mouseup', function(e) {
if (isDragging) {
isDragging = false;
isFlying = true;
velocity.x = (slingX - birdX) / 5;
velocity.y = (slingY - birdY) / 5;
attemptsLeft--;
updateAttempts();
}
});
// Fonction de mise à jour du jeu
function update() {
// Mettre à jour les nuages
clouds.forEach(cloud => {
cloud.x += cloud.speedX;
if (cloud.x > canvas.width + 100) {
cloud.x = -150; // Revenir à gauche
}
});
// Mettre à jour les volcans et générer des particules de lave
volcanoes.forEach(volcano => {
volcano.particleTimer--;
if (volcano.particleTimer <= 0) {
volcano.particleTimer = Math.floor(Math.random() * 30 + 20); // Entre 20 et 50 frames
// Générer une particule de lave
let particle = {
x: volcano.x,
y: volcano.y - 10,
vx: (Math.random() * 2 - 1) * 2, // Vitesse horizontale aléatoire
vy: -Math.random() * 3 - 2, // Vitesse verticale vers le haut
size: Math.random() * 5 + 2, // Taille entre 2 et 7
life: 100 // Durée de vie de la particule
};
lavaParticles.push(particle);
}
});
// Mettre à jour les particules de lave
for (let i = 0; i < lavaParticles.length; i++) {
let p = lavaParticles[i];
p.x += p.vx;
p.y += p.vy;
p.vy += 0.1; // Gravité
p.life--;
if (p.life <= 0 || p.y > canvas.height) {
lavaParticles.splice(i, 1);
i--;
}
}
// Mettre à jour les explosions
for (let i = 0; i < explosions.length; i++) {
let explosion = explosions[i];
explosion.life--;
// Mettre à jour les particules de l'explosion
for (let j = 0; j < explosion.particles.length; j++) {
let p = explosion.particles[j];
p.x += p.vx;
p.y += p.vy;
p.vy += 0.05; // Gravité légère
p.life--;
if (p.life <= 0) {
explosion.particles.splice(j, 1);
j--;
}
}
// Si l'explosion n'a plus de particules ou est terminée, la retirer
if (explosion.life <= 0 || explosion.particles.length === 0) {
explosions.splice(i, 1);
i--;
}
}
// Mettre à jour les monstres (ennemis) avec des mouvements imprévisibles
monsters.forEach(monster => {
monster.directionChangeTimer--;
if (monster.directionChangeTimer <= 0) {
monster.directionChangeTimer = Math.floor(Math.random() * 60 + 30); // Change direction fréquemment
// Vitesse aléatoire entre -2 et 2 pour X et Y
monster.speedX = (Math.random() * 4 - 2);
monster.speedY = (Math.random() * 4 - 2);
}
// Mettre à jour la position
monster.x += monster.speedX;
monster.y += monster.speedY;
// Limiter les mouvements dans la zone de jeu
// Limites horizontales
if (monster.x - monster.radius < 500) {
monster.x = 500 + monster.radius;
monster.speedX *= -1;
}
if (monster.x + monster.radius > canvas.width) {
monster.x = canvas.width - monster.radius;
monster.speedX *= -1;
}
// Limites verticales
if (monster.y - monster.radius < canvas.height - 300) {
monster.y = canvas.height - 300 + monster.radius;
monster.speedY *= -1;
}
if (monster.y + monster.radius > canvas.height - 50) {
monster.y = canvas.height - 50 - monster.radius;
monster.speedY *= -1;
}
});
if (isFlying) {
velocity.y += gravity;
birdX += velocity.x;
birdY += velocity.y;
// Vérifier les collisions avec les obstacles
for (let i = 0; i < obstacles.length; i++) {
let obstacle = obstacles[i];
if (rectCollision(
birdX - birdRadius,
birdY - birdRadius,
birdRadius * 2,
birdRadius * 2,
obstacle.x,
obstacle.y,
obstacle.width,
obstacle.height
)) {
// Collision détectée
// Réduire la vitesse de l'oiseau
velocity.x *= -0.5; // Rebondissement simplifié
velocity.y *= -0.5;
// Détruire l'obstacle si la vitesse est suffisante
if (Math.abs(velocity.x) + Math.abs(velocity.y) > 5) {
obstacles.splice(i, 1);
i--;
}
}
}
// Vérifier les collisions avec les monstres
for (let i = 0; i < monsters.length; i++) {
let monster = monsters[i];
if (!monster.isHit && circleCollision(birdX, birdY, birdRadius, monster.x, monster.y, monster.radius)) {
// Collision détectée
monster.isHit = true;
score += 100;
updateScore();
// Créer une explosion à la position du monstre
createExplosion(monster.x, monster.y);
}
}
// Vérifier si l'oiseau sort de l'écran ou touche le sol
if (
birdY - birdRadius > canvas.height ||
birdX + birdRadius < 0 ||
birdX - birdRadius > canvas.width
) {
isFlying = false;
birdX = slingX;
birdY = slingY;
velocity = { x: 0, y: 0 };
// Vérifier si le jeu est terminé
if (monsters.every(monster => monster.isHit)) {
// Passer au niveau suivant
if (currentLevelIndex < levels.length - 1) {
alert('Niveau terminé ! Vous passez au niveau suivant.');
currentLevelIndex++;
loadLevel(currentLevelIndex);
} else {
// Afficher un message de victoire finale
alert('Félicitations ! Vous avez terminé tous les niveaux !\nScore total : ' + score);
resetGame();
}
} else if (attemptsLeft === 0) {
// Afficher un message de défaite
alert('Vous avez utilisé toutes vos tentatives.\nScore total : ' + score);
resetGame();
}
}
}
}
// Fonction de collision rectangle
function rectCollision(x1, y1, w1, h1, x2, y2, w2, h2) {
return (
x1 < x2 + w2 &&
x1 + w1 > x2 &&
y1 < y2 + h2 &&
y1 + h1 > y2
);
}
// Fonction de collision cercle
function circleCollision(x1, y1, r1, x2, y2, r2) {
let dx = x1 - x2;
let dy = y1 - y2;
let distance = Math.hypot(dx, dy);
return distance < r1 + r2;
}
// Fonction pour créer une explosion
function createExplosion(x, y) {
let explosion = {
particles: [],
life: 60 // Durée de l'explosion en frames
};
for (let i = 0; i < 30; i++) {
let particle = {
x: x,
y: y,
vx: (Math.random() * 2 - 1) * 5,
vy: (Math.random() * 2 - 1) * 5,
radius: Math.random() * 5 + 2,
life: Math.random() * 30 + 30
};
explosion.particles.push(particle);
}
explosions.push(explosion);
}
// Fonction pour réinitialiser le niveau
function resetLevel() {
loadLevel(currentLevelIndex);
}
// Fonction pour réinitialiser le jeu
function resetGame() {
currentLevelIndex = 0;
score = 0;
loadLevel(currentLevelIndex);
}
// Fonction pour mettre à jour le score affiché
function updateScore() {
scoreBoard.textContent = 'Score : ' + score;
}
// Fonction pour mettre à jour les tentatives restantes affichées
function updateAttempts() {
attemptsBoard.textContent = 'Tentatives restantes : ' + attemptsLeft;
}
// Fonction pour dessiner les nuages
function drawCloud(x, y) {
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(x, y, 20, Math.PI * 0.5, Math.PI * 1.5);
ctx.arc(x + 25, y - 20, 35, Math.PI * 1, Math.PI * 1.85);
ctx.arc(x + 70, y - 15, 25, Math.PI * 1.37, Math.PI * 0.37, true);
ctx.arc(x + 50, y + 10, 35, Math.PI * 1.5, Math.PI * 0.5);
ctx.closePath();
ctx.fill();
}
// Fonction pour dessiner les volcans
function drawVolcanoes() {
volcanoes.forEach(volcano => {
// Dessiner le volcan
ctx.fillStyle = '#A9A9A9';
ctx.beginPath();
ctx.moveTo(volcano.x - 100, canvas.height - 50);
ctx.lineTo(volcano.x, volcano.y);
ctx.lineTo(volcano.x + 100, canvas.height - 50);
ctx.closePath();
ctx.fill();
// Dessiner la lave au sommet
ctx.fillStyle = 'orange';
ctx.beginPath();
ctx.moveTo(volcano.x - 15, volcano.y + 15);
ctx.lineTo(volcano.x, volcano.y);
ctx.lineTo(volcano.x + 15, volcano.y + 15);
ctx.closePath();
ctx.fill();
});
}
// Fonction pour dessiner les particules de lave
function drawLavaParticles() {
lavaParticles.forEach(particle => {
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fill();
});
}
// Fonction pour dessiner les explosions
function drawExplosions() {
explosions.forEach(explosion => {
explosion.particles.forEach(particle => {
ctx.fillStyle = 'rgba(255, 165, 0,' + (particle.life / 60) + ')'; // Couleur orange avec transparence
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
ctx.fill();
});
});
}
// Fonction pour dessiner les arbres
function drawTree(x, y) {
// Tronc
ctx.fillStyle = '#8B4513';
ctx.fillRect(x + 15, y, 20, 50);
// Feuillage
ctx.fillStyle = '#228B22';
ctx.beginPath();
ctx.moveTo(x + 25, y - 75);
ctx.lineTo(x, y);
ctx.lineTo(x + 50, y);
ctx.closePath();
ctx.fill();
}
// Fonction pour dessiner le sol
function drawGround() {
// Sol avec texture d'herbe
let grassHeight = 50;
ctx.fillStyle = '#7CFC00';
ctx.fillRect(0, canvas.height - grassHeight, canvas.width, grassHeight);
// Bandes d'herbe pour la texture
ctx.fillStyle = '#66CDAA';
for (let i = 0; i < canvas.width; i += 15) {
ctx.beginPath();
ctx.moveTo(i, canvas.height - grassHeight);
ctx.lineTo(i + 10, canvas.height - grassHeight - 10);
ctx.lineTo(i + 20, canvas.height - grassHeight);
ctx.closePath();
ctx.fill();
}
}
// Fonction pour dessiner le décor
function drawBackground() {
// Dessiner le ciel (déjà bleu clair grâce au CSS)
// Dessiner les volcans
drawVolcanoes();
// Dessiner les particules de lave
drawLavaParticles();
// Dessiner les explosions
drawExplosions();
// Dessiner les nuages en mouvement
clouds.forEach(cloud => {
drawCloud(cloud.x, cloud.y);
});
// Dessiner les arbres au sol
drawTree(50, canvas.height - 100);
drawTree(700, canvas.height - 100);
drawTree(150, canvas.height - 120);
drawTree(650, canvas.height - 120);
drawTree(250, canvas.height - 110);
drawTree(550, canvas.height - 110);
// Dessiner le sol
drawGround();
}
// Fonction pour dessiner les obstacles
function drawObstacle(obstacle) {
ctx.fillStyle = '#8B4513'; // Couleur des obstacles en bois
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
ctx.strokeStyle = '#000';
ctx.strokeRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
// Texture du bois
ctx.fillStyle = '#A0522D';
for (let y = obstacle.y; y < obstacle.y + obstacle.height; y += 10) {
ctx.fillRect(obstacle.x, y, obstacle.width, 5);
}
}
// Fonction pour dessiner les monstres
function drawMonster(monster) {
if (!monster.isHit) {
ctx.beginPath();
ctx.arc(monster.x, monster.y, monster.radius, 0, Math.PI * 2);
ctx.fillStyle = 'green';
ctx.fill();
ctx.strokeStyle = '#000';
ctx.stroke();
// Dessiner les yeux
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(monster.x - 5, monster.y - 5, 5, 0, Math.PI * 2);
ctx.arc(monster.x + 5, monster.y - 5, 5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(monster.x - 5, monster.y - 5, 2, 0, Math.PI * 2);
ctx.arc(monster.x + 5, monster.y - 5, 2, 0, Math.PI * 2);
ctx.fill();
// Dessiner la bouche
ctx.beginPath();
ctx.arc(monster.x, monster.y + 5, 7, Math.PI * 0.2, Math.PI * 0.8);
ctx.stroke();
}
}
// Fonction pour dessiner l'oiseau
function drawBird() {
// Corps de l'oiseau
ctx.beginPath();
ctx.arc(birdX, birdY, birdRadius, 0, Math.PI * 2);
ctx.fillStyle = 'red';
ctx.fill();
ctx.strokeStyle = '#000';
ctx.stroke();
// Dessiner les yeux
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(birdX - 5, birdY - 5, 5, 0, Math.PI * 2);
ctx.arc(birdX + 5, birdY - 5, 5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(birdX - 5, birdY - 5, 2, 0, Math.PI * 2);
ctx.arc(birdX + 5, birdY - 5, 2, 0, Math.PI * 2);
ctx.fill();
// Bec
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.moveTo(birdX, birdY);
ctx.lineTo(birdX + 10, birdY + 5);
ctx.lineTo(birdX, birdY + 10);
ctx.closePath();
ctx.fill();
}
// Fonction de dessin du jeu
function draw() {
// Effacer le canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Dessiner le décor
drawBackground();
// Dessiner les obstacles
obstacles.forEach(obstacle => {
drawObstacle(obstacle);
});
// Dessiner les monstres
monsters.forEach(monster => {
drawMonster(monster);
});
// Dessiner le lance-pierre
ctx.beginPath();
ctx.moveTo(slingX - 10, slingY);
ctx.lineTo(slingX + 10, slingY);
ctx.strokeStyle = '#8B4513';
ctx.lineWidth = 5;
ctx.stroke();
// Dessiner l'élastique
if (isDragging) {
ctx.beginPath();
ctx.moveTo(slingX, slingY);
ctx.lineTo(birdX, birdY);
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
ctx.stroke();
}
// Dessiner l'oiseau
drawBird();
}
// Boucle principale du jeu
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Démarrer le jeu
resetGame();
gameLoop();
</script>
</body>
</html>
Explications des modifications apportées :
Ajout d'un tableau explosions : Ce tableau stocke les explosions en cours d'animation.
javascript
let explosions = [];
Création de la fonction createExplosion(x, y) : Cette fonction est appelée lorsque l'oiseau touche un monstre. Elle génère une explosion composée de plusieurs particules à la position spécifiée.
javascript
function createExplosion(x, y) {
let explosion = {
particles: [],
life: 60 // Durée de l'explosion en frames
};
for (let i = 0; i < 30; i++) {
let particle = {
x: x,
y: y,
vx: (Math.random() * 2 - 1) * 5,
vy: (Math.random() * 2 - 1) * 5,
radius: Math.random() * 5 + 2,
life: Math.random() * 30 + 30
};
explosion.particles.push(particle);
}
explosions.push(explosion);
}
Mise à jour des explosions dans la fonction update() : Les particules de chaque explosion sont mises à jour à chaque frame pour simuler le mouvement et la disparition progressive de l'explosion.
javascript
// Mettre à jour les explosions
for (let i = 0; i < explosions.length; i++) {
let explosion = explosions[i];
explosion.life--;
// Mettre à jour les particules de l'explosion
for (let j = 0; j < explosion.particles.length; j++) {
let p = explosion.particles[j];
p.x += p.vx;
p.y += p.vy;
p.vy += 0.05; // Gravité légère
p.life--;
if (p.life <= 0) {
explosion.particles.splice(j, 1);
j--;
}
}
// Si l'explosion n'a plus de particules ou est terminée, la retirer
if (explosion.life <= 0 || explosion.particles.length === 0) {
explosions.splice(i, 1);
i--;
}
}
Résultat attendu :
Lorsqu'un oiseau touche un monstre, une animation d'explosion se déclenche à la position du monstre, ajoutant ainsi une touche visuelle enrichissante au jeu. Les particules de l'explosion s'éparpillent et disparaissent progressivement, créant un effet dynamique et visuel agréable.
Note :
Vous pouvez ajuster les paramètres tels que le nombre de particules, leur vitesse, leur taille, et la durée de l'explosion pour obtenir l'effet visuel souhaité.
N'hésitez pas à intégrer ce code dans votre jeu et à le personnaliser davantage pour améliorer l'expérience de jeu.
Dessin des explosions dans la fonction drawExplosions() : Chaque particule de l'explosion est dessinée avec une transparence qui diminue progressivement, ce qui donne un effet de disparition.
Appel à createExplosion(monster.x, monster.y) lors de la collision avec un monstre : Lorsque l'oiseau touche un monstre, une explosion est créée à la position du monstre.
javascript
if (!monster.isHit && circleCollision(birdX, birdY, birdRadius, monster.x, monster.y, monster.radius)) {
// Collision détectée
monster.isHit = true;
score += 100;
updateScore();
// Créer une explosion à la position du monstre
createExplosion(monster.x, monster.y);
}
Intégration du dessin des explosions dans le décor : L'appel à drawExplosions() est placé dans la fonction drawBackground() pour que les explosions soient dessinées avec le décor.