gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Peux tu coder un jeu comme flappy bird qui peut se jouer dans un navigateur firefox ?
- User
Peux tu coder un jeu comme flappy bird qui peut se jouer dans un navigateur firefox ?
- Assistant
Peux tu coder un jeu comme flappy bird qui peut se jouer dans un navigateur firefox ?
Créer un jeu similaire à Flappy Bird que l'on peut jouer dans un navigateur est tout à fait possible avec HTML, CSS et JavaScript. Voici un exemple simplifié pour vous aider à démarrer. Ce code peut être copié dans un fichier HTML et ouvert dans un navigateur comme Firefox.
Voici un exemple de code :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird Clone</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #70c5ce;
}
canvas {
border: 2px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
let bird = { x: 50, y: 150, width: 20, height: 20, gravity: 0.6, lift: -15, velocity: 0 };
let pipes = [];
let frameCount = 0;
let gameOver = false;
function resetGame() {
bird.y = 150;
bird.velocity = 0;
pipes = [];
frameCount = 0;
gameOver = false;
}
function drawBird() {
ctx.fillStyle = "yellow";
ctx.fillRect(bird.x, bird.y, bird.width, bird.height);
}
function drawPipes() {
ctx.fillStyle = "green";
pipes.forEach(pipe => {
ctx.fillRect(pipe.x, 0, pipe.width, pipe.top);
ctx.fillRect(pipe.x, canvas.height - pipe.bottom, pipe.width, pipe.bottom);
});
}
function update() {
if (gameOver) return;
bird.velocity += bird.gravity;
bird.y += bird.velocity;
if (bird.y + bird.height >= canvas.height) {
gameOver = true;
}
if (frameCount % 75 === 0) {
const gap = 100;
const top = Math.random() * (canvas.height - gap - 20) + 20;
const bottom = canvas.height - (top + gap);
pipes.push({ x: canvas.width, top: top, bottom: bottom, width: 30 });
}
pipes.forEach(pipe => {
pipe.x -= 3;
if (pipe.x < bird.x + bird.width && pipe.x + pipe.width > bird.x) {
if (bird.y < pipe.top || bird.y + bird.height > canvas.height - pipe.bottom) {
gameOver = true;
}
}
});
pipes = pipes.filter(pipe => pipe.x + pipe.width > 0);
frameCount++;
}
function drawGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBird();
drawPipes();
if (gameOver) {
ctx.fillStyle = "red";
ctx.font = "30px Arial";
ctx.fillText("Game Over", 100, canvas.height / 2);
ctx.fillText("Press Space to Restart", 50, canvas.height / 2 + 40);
}
}
function gameLoop() {
update();
drawGame();
requestAnimationFrame(gameLoop);
}
document.addEventListener("keydown", function(event) {
if (event.code === "Space") {
if (gameOver) {
resetGame();
} else {
bird.velocity += bird.lift;
}
}
});
resetGame();
gameLoop();
</script>
</body>
</html>
.html, par exemple flappy_bird_clone.html.N'hésitez pas à modifier et à améliorer ce code pour ajouter des fonctionnalités supplémentaires telles que des scores, des sons, etc. Bon développement !
Not yet included in a published release.