<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mission Étoiles</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
background: #080b1c;
color: white;
font-family: system-ui, sans-serif;
}
main { width: min(94vw, 440px); text-align: center; }
h1 { margin: 12px 0; }
p { color: #bfc8ed; font-size: 14px; }
canvas {
display: block;
width: 100%;
background: #101631;
border: 2px solid #465399;
border-radius: 16px;
touch-action: none;
}
button {
margin: 14px;
padding: 12px 24px;
border: 0;
border-radius: 12px;
background: #80ffdb;
color: #101631;
font-weight: bold;
cursor: pointer;
}
</style>
</head>
<body>
<main>
<h1>🚀 Mission Étoiles</h1>
<p>Attrape les étoiles et évite les météorites !</p>
<canvas id="jeu" width="420" height="540"
aria-label="Jeu : attraper des étoiles et éviter des météorites">
</canvas>
<button id="rejouer">Recommencer</button>
<p>← → · Souris · Écran tactile</p>
</main>
<script>
const canvas = document.getElementById("jeu");
const ctx = canvas.getContext("2d");
const W = canvas.width, H = canvas.height;
const touches = new Set();
let joueur, objets, score, vies, termine, compteur;
let precedent = 0;
const fond = Array.from({ length: 65 }, () => ({
x: Math.random() * W,
y: Math.random() * H,
taille: Math.random() * 2 + 0.5
}));
function recommencer() {
joueur = { x: W / 2, y: H - 48 };
objets = [];
score = 0;
vies = 3;
termine = false;
compteur = 0;
touches.clear();
}
function limiter(x) {
return Math.max(23, Math.min(W - 23, x));
}
document.addEventListener("keydown", e => {
if (["ArrowLeft", "ArrowRight"].includes(e.key)) {
e.preventDefault();
touches.add(e.key);
}
});
document.addEventListener("keyup", e => touches.delete(e.key));
window.addEventListener("blur", () => touches.clear());
function deplacer(e) {
const rect = canvas.getBoundingClientRect();
joueur.x = limiter((e.clientX - rect.left) * W / rect.width);
}
canvas.addEventListener("pointerdown", e => {
canvas.setPointerCapture(e.pointerId);
deplacer(e);
});
canvas.addEventListener("pointermove", deplacer);
document.getElementById("rejouer").onclick = recommencer;
function actualiser(dt) {
if (termine) return;
if (touches.has("ArrowLeft")) joueur.x -= 330 * dt;
if (touches.has("ArrowRight")) joueur.x += 330 * dt;
joueur.x = limiter(joueur.x);
compteur += dt;
const intervalle = Math.max(0.25, 0.7 - score * 0.004);
if (compteur >= intervalle) {
compteur = 0;
objets.push({
x: 24 + Math.random() * (W - 48),
y: -25,
etoile: Math.random() < 0.6,
vitesse: 140 + Math.random() * 90 + score * 2
});
}
for (let i = objets.length - 1; i >= 0; i--) {
const o = objets[i];
o.y += o.vitesse * dt;
if (Math.hypot(o.x - joueur.x, o.y - joueur.y) < 29) {
if (o.etoile) score += 10;
else vies--;
objets.splice(i, 1);
if (vies <= 0) {
termine = true;
break;
}
} else if (o.y > H + 30) {
objets.splice(i, 1);
}
}
}
function dessiner(temps) {
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = "#c8d5ff";
for (const s of fond) {
ctx.globalAlpha = 0.4 + Math.sin(temps + s.x) * 0.3;
ctx.fillRect(s.x, s.y, s.taille, s.taille);
}
ctx.globalAlpha = 1;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "30px system-ui";
for (const o of objets) {
ctx.fillText(o.etoile ? "⭐" : "☄️", o.x, o.y);
}
// Vaisseau dessiné pour rester orienté vers le haut.
ctx.save();
ctx.translate(joueur.x, joueur.y);
ctx.fillStyle = "#ffb454";
ctx.beginPath();
ctx.moveTo(-7, 13);
ctx.lineTo(0, 24 + Math.sin(temps * 25) * 5);
ctx.lineTo(7, 13);
ctx.fill();
ctx.fillStyle = "#80ffdb";
ctx.beginPath();
ctx.moveTo(0, -23);
ctx.lineTo(19, 17);
ctx.lineTo(0, 10);
ctx.lineTo(-19, 17);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#334c91";
ctx.beginPath();
ctx.arc(0, -2, 5, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
ctx.fillStyle = "white";
ctx.font = "bold 19px system-ui";
ctx.textAlign = "left";
ctx.fillText("Score : " + score, 16, 27);
ctx.textAlign = "right";
ctx.fillText("❤️".repeat(vies), W - 16, 27);
if (termine) {
ctx.fillStyle = "rgba(5, 8, 22, 0.85)";
ctx.fillRect(0, 0, W, H);
ctx.textAlign = "center";
ctx.fillStyle = "white";
ctx.font = "bold 34px system-ui";
ctx.fillText("Partie terminée", W / 2, H / 2 - 35);
ctx.font = "23px system-ui";
ctx.fillText("Score : " + score, W / 2, H / 2 + 10);
ctx.font = "17px system-ui";
ctx.fillText("Clique sur Recommencer !", W / 2, H / 2 + 55);
}
}
function boucle(maintenant) {
const dt = Math.min((maintenant - precedent) / 1000, 0.04);
precedent = maintenant;
actualiser(dt);
dessiner(maintenant / 1000);
requestAnimationFrame(boucle);
}
recommencer();
requestAnimationFrame(boucle);
</script>
</body>
</html>