Spaces:
Running

games / index.html
Matheusxtx's picture
undefined - Initial Deployment
c4ad2fe verified
Raw History Blame Contribute Delete
17.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Flappy Bird Clone</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
touch-action: manipulation;
-webkit-tap-highlight-color: transparent;
font-family: 'Arial', sans-serif;
}
#gameCanvas {
display: block;
background-color: #70c5ce;
touch-action: none;
}
#gameContainer {
position: relative;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#startScreen, #gameOverScreen {
position: absolute;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
color: white;
z-index: 10;
}
.btn {
background-color: #f8d347;
color: #333;
border: none;
padding: 12px 24px;
font-size: 18px;
border-radius: 30px;
cursor: pointer;
margin-top: 20px;
font-weight: bold;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
transition: all 0.2s;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);
}
.btn:active {
transform: translateY(1px);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.title {
font-size: 3rem;
font-weight: bold;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
color: #f8d347;
}
.score-display {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
font-size: 2rem;
font-weight: bold;
color: white;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
z-index: 5;
}
.instructions {
text-align: center;
margin-bottom: 20px;
font-size: 1.2rem;
max-width: 80%;
}
@media (max-width: 600px) {
.title {
font-size: 2rem;
}
.btn {
padding: 10px 20px;
font-size: 16px;
}
.instructions {
font-size: 1rem;
}
}
</style>
</head>
<body class="bg-gray-900">
<div id="gameContainer" class="w-full h-screen">
<div id="score" class="score-display">0</div>
<canvas id="gameCanvas"></canvas>
<div id="startScreen">
<h1 class="title">FLAPPY BIRD</h1>
<p class="instructions text-white">Toque na tela para fazer o pássaro voar e passar pelos canos!</p>
<button id="startBtn" class="btn">COMEÇAR</button>
</div>
<div id="gameOverScreen" style="display: none;">
<h1 class="title">GAME OVER</h1>
<p id="finalScore" class="text-white text-2xl mb-4">Pontuação: 0</p>
<p id="highScore" class="text-white text-xl mb-6">Recorde: 0</p>
<button id="restartBtn" class="btn">JOGAR NOVAMENTE</button>
</div>
</div>
<script>
// Game variables
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const startBtn = document.getElementById('startBtn');
const restartBtn = document.getElementById('restartBtn');
const scoreDisplay = document.getElementById('score');
const finalScoreDisplay = document.getElementById('finalScore');
const highScoreDisplay = document.getElementById('highScore');
// Set canvas size
function resizeCanvas() {
const container = document.getElementById('gameContainer');
canvas.width = container.clientWidth;
canvas.height = container.clientHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Game state
let gameRunning = false;
let score = 0;
let highScore = localStorage.getItem('flappyHighScore') || 0;
let animationId;
// Bird properties
const bird = {
x: 100,
y: canvas.height / 2,
width: 40,
height: 30,
velocity: 0,
gravity: 0.5,
jumpForce: -10,
color: '#f8d347',
update: function() {
this.velocity += this.gravity;
this.y += this.velocity;
// Check if bird hits the ground or ceiling
if (this.y + this.height > canvas.height) {
this.y = canvas.height - this.height;
gameOver();
}
if (this.y < 0) {
this.y = 0;
gameOver();
}
},
jump: function() {
this.velocity = this.jumpForce;
},
draw: function() {
ctx.fillStyle = this.color;
// Draw bird body
ctx.beginPath();
ctx.ellipse(this.x + this.width/2, this.y + this.height/2,
this.width/2, this.height/2, 0, 0, Math.PI * 2);
ctx.fill();
// Draw beak
ctx.fillStyle = '#f39c12';
ctx.beginPath();
ctx.moveTo(this.x + this.width, this.y + this.height/2);
ctx.lineTo(this.x + this.width + 15, this.y + this.height/2);
ctx.lineTo(this.x + this.width, this.y + this.height/2 + 8);
ctx.fill();
// Draw eye
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(this.x + this.width/2 + 5, this.y + this.height/2 - 5, 3, 0, Math.PI * 2);
ctx.fill();
// Draw wings (animated based on velocity)
ctx.fillStyle = '#f1c40f';
const wingY = this.y + this.height/2;
const wingHeight = Math.abs(this.velocity) * 2 + 5;
ctx.beginPath();
ctx.ellipse(this.x + this.width/2, wingY,
this.width/2, wingHeight, Math.PI/4, 0, Math.PI * 2);
ctx.fill();
},
getBounds: function() {
return {
x: this.x,
y: this.y,
width: this.width,
height: this.height
};
}
};
// Pipes
const pipes = {
list: [],
width: 60,
gap: 150,
speed: 3,
frequency: 1500, // ms
lastPipeTime: 0,
update: function(timestamp) {
// Add new pipes
if (timestamp - this.lastPipeTime > this.frequency) {
this.addPipe();
this.lastPipeTime = timestamp;
}
// Move and remove off-screen pipes
for (let i = this.list.length - 1; i >= 0; i--) {
this.list[i].x -= this.speed;
// Check if pipe is passed by bird (for scoring)
if (!this.list[i].passed && this.list[i].x + this.width < bird.x) {
this.list[i].passed = true;
score++;
scoreDisplay.textContent = score;
}
// Remove pipes that are off screen
if (this.list[i].x + this.width < 0) {
this.list.splice(i, 1);
}
}
},
addPipe: function() {
const minHeight = 50;
const maxHeight = canvas.height - this.gap - minHeight;
const height = Math.floor(Math.random() * (maxHeight - minHeight + 1)) + minHeight;
this.list.push({
x: canvas.width,
height: height,
passed: false
});
},
draw: function() {
ctx.fillStyle = '#27ae60';
for (const pipe of this.list) {
// Top pipe
ctx.fillRect(pipe.x, 0, this.width, pipe.height);
// Bottom pipe
const bottomY = pipe.height + this.gap;
ctx.fillRect(pipe.x, bottomY, this.width, canvas.height - bottomY);
// Pipe edges
ctx.fillStyle = '#2ecc71';
ctx.fillRect(pipe.x - 5, pipe.height - 20, this.width + 10, 20);
ctx.fillRect(pipe.x - 5, bottomY, this.width + 10, 20);
ctx.fillStyle = '#27ae60';
}
},
checkCollision: function() {
const birdBounds = bird.getBounds();
for (const pipe of this.list) {
const bottomY = pipe.height + this.gap;
// Check collision with top pipe
if (birdBounds.x + birdBounds.width > pipe.x &&
birdBounds.x < pipe.x + this.width &&
birdBounds.y < pipe.height) {
return true;
}
// Check collision with bottom pipe
if (birdBounds.x + birdBounds.width > pipe.x &&
birdBounds.x < pipe.x + this.width &&
birdBounds.y + birdBounds.height > bottomY) {
return true;
}
}
return false;
}
};
// Background elements
const background = {
clouds: [],
lastCloudTime: 0,
cloudFrequency: 3000,
init: function() {
// Create initial clouds
for (let i = 0; i < 3; i++) {
this.addCloud(Math.random() * canvas.width);
}
},
addCloud: function(x) {
const size = Math.random() * 30 + 20;
const y = Math.random() * (canvas.height / 3);
const speed = Math.random() * 0.5 + 0.5;
this.clouds.push({
x: x,
y: y,
size: size,
speed: speed
});
},
update: function(timestamp) {
// Add new clouds
if (timestamp - this.lastCloudTime > this.cloudFrequency) {
this.addCloud(canvas.width);
this.lastCloudTime = timestamp;
}
// Move clouds
for (let i = this.clouds.length - 1; i >= 0; i--) {
this.clouds[i].x -= this.clouds[i].speed;
// Remove off-screen clouds
if (this.clouds[i].x + this.clouds[i].size * 2 < 0) {
this.clouds.splice(i, 1);
}
}
},
draw: function() {
// Draw sky gradient
const skyGradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
skyGradient.addColorStop(0, '#70c5ce');
skyGradient.addColorStop(1, '#2980b9');
ctx.fillStyle = skyGradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw clouds
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
for (const cloud of this.clouds) {
// Draw multiple circles to form a cloud
ctx.beginPath();
ctx.arc(cloud.x, cloud.y, cloud.size, 0, Math.PI * 2);
ctx.arc(cloud.x + cloud.size * 0.6, cloud.y - cloud.size * 0.2, cloud.size * 0.8, 0, Math.PI * 2);
ctx.arc(cloud.x + cloud.size * 1.3, cloud.y, cloud.size * 0.7, 0, Math.PI * 2);
ctx.arc(cloud.x + cloud.size * 0.8, cloud.y + cloud.size * 0.3, cloud.size * 0.6, 0, Math.PI * 2);
ctx.fill();
}
// Draw ground
ctx.fillStyle = '#27ae60';
ctx.fillRect(0, canvas.height - 20, canvas.width, 20);
}
};
// Game functions
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
gameRunning = true;
// Reset game state
score = 0;
scoreDisplay.textContent = '0';
bird.y = canvas.height / 2;
bird.velocity = 0;
pipes.list = [];
pipes.lastPipeTime = 0;
background.init();
// Start game loop
lastTime = 0;
animationId = requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
cancelAnimationFrame(animationId);
// Update high score
if (score > highScore) {
highScore = score;
localStorage.setItem('flappyHighScore', highScore);
}
// Show game over screen
finalScoreDisplay.textContent = `Pontuação: ${score}`;
highScoreDisplay.textContent = `Recorde: ${highScore}`;
gameOverScreen.style.display = 'flex';
}
function gameLoop(timestamp) {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw background
background.update(timestamp);
background.draw();
// Update and draw pipes
pipes.update(timestamp);
pipes.draw();
// Update and draw bird
bird.update();
bird.draw();
// Check collisions
if (pipes.checkCollision()) {
gameOver();
return;
}
// Continue game loop
if (gameRunning) {
animationId = requestAnimationFrame(gameLoop);
}
}
// Event listeners
startBtn.addEventListener('click', startGame);
restartBtn.addEventListener('click', startGame);
// Touch and click controls
canvas.addEventListener('click', function() {
if (gameRunning) {
bird.jump();
}
});
canvas.addEventListener('touchstart', function(e) {
e.preventDefault();
if (gameRunning) {
bird.jump();
}
}, { passive: false });
// Prevent scrolling on touch
document.addEventListener('touchmove', function(e) {
if (gameRunning) {
e.preventDefault();
}
}, { passive: false });
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Matheusxtx/games" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>