Spaces:
Running

dubaiflight / index.html
onlyoneritz's picture
Update index.html
48ea734 verified
Raw History Blame Contribute Delete
17.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dubai Flight Simulator</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
#gameCanvas {
background: linear-gradient(to bottom, #87CEEB, #1E90FF);
display: block;
}
#startScreen, #gameOverScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.7);
color: white;
}
#gameOverScreen {
display: none;
}
.building {
position: absolute;
background: linear-gradient(to bottom, #FFD700, #FFA500);
border: 2px solid #333;
}
.plane {
position: absolute;
width: 50px;
height: 50px;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="%23ffffff" d="M22,16.21V14l-8-5V3.5A1.5,1.5,0,0,0,12.5,2h0A1.5,1.5,0,0,0,11,3.5V9l-8,5v2.21L11,14.6V19l-2,1.5V22l3.5-1,3.5,1V20.5L14,19v-4.4Z"/></svg>');
background-size: contain;
transform: rotate(90deg);
}
.star {
position: absolute;
width: 20px;
height: 20px;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="%23FFD700" d="M12,17.27L18.18,21l-1.64-7.03L22,9.24l-7.19-.61L12,2,9.19,8.63,2,9.24l5.46,4.73L5.82,21Z"/></svg>');
background-size: contain;
}
.cloud {
position: absolute;
background-color: white;
border-radius: 50%;
}
.burj-khalifa {
background: linear-gradient(to bottom, silver, #555);
border-left: 2px solid #333;
border-right: 2px solid #333;
}
</style>
</head>
<body class="bg-gray-900">
<canvas id="gameCanvas"></canvas>
<div id="startScreen" class="text-center p-4">
<h1 class="text-4xl font-bold mb-6 text-yellow-400">Dubai Flight Simulator</h1>
<p class="text-xl mb-8">Fly your plane between Dubai's skyscrapers and collect stars!</p>
<button id="startButton" class="bg-yellow-500 hover:bg-yellow-600 text-white font-bold py-3 px-6 rounded-full text-xl transition duration-300 transform hover:scale-105">
Start Flight
</button>
<div class="mt-8 text-sm">
<p>Controls: Use Arrow Keys or WASD to move</p>
<p>Avoid buildings and collect stars!</p>
</div>
</div>
<div id="gameOverScreen" class="text-center p-4">
<h1 class="text-4xl font-bold mb-4 text-red-500">Game Over!</h1>
<p class="text-2xl mb-2">Your Score: <span id="finalScore" class="text-yellow-400">0</span></p>
<p class="text-xl mb-8">Thanks for flying over Dubai!</p>
<button id="restartButton" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-3 px-6 rounded-full text-xl transition duration-300 transform hover:scale-105">
Fly Again
</button>
</div>
<div id="scoreDisplay" class="absolute top-4 left-4 bg-black bg-opacity-50 text-white p-3 rounded-lg hidden">
<div class="flex items-center">
<span class="text-yellow-400 mr-2">★</span>
<span id="score">0</span>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
const scoreDisplay = document.getElementById('scoreDisplay');
const scoreElement = document.getElementById('score');
const finalScoreElement = document.getElementById('finalScore');
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Game variables
let plane = {
x: 100,
y: canvas.height / 2,
width: 50,
height: 50,
speed: 5
};
let buildings = [];
let stars = [];
let clouds = [];
let score = 0;
let gameSpeed = 2;
let gameRunning = false;
let animationId;
let keys = {};
// Event listeners
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
window.addEventListener('keydown', (e) => {
keys[e.key] = true;
// Space to start game from start screen
if (e.key === ' ' && !gameRunning && startScreen.style.display !== 'none') {
startGame();
}
});
window.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if (gameRunning) {
plane.y = Math.min(plane.y, canvas.height - plane.height);
}
});
// Game functions
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
scoreDisplay.classList.remove('hidden');
// Reset game state
plane = {
x: 100,
y: canvas.height / 2,
width: 50,
height: 50,
speed: 5
};
buildings = [];
stars = [];
clouds = [];
score = 0;
scoreElement.textContent = score;
gameSpeed = 2;
gameRunning = true;
// Start game loop
gameLoop();
}
function gameLoop() {
if (!gameRunning) return;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw sky gradient
const skyGradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
skyGradient.addColorStop(0, '#87CEEB');
skyGradient.addColorStop(1, '#1E90FF');
ctx.fillStyle = skyGradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw sun
ctx.beginPath();
ctx.arc(canvas.width - 100, 100, 60, 0, Math.PI * 2);
ctx.fillStyle = '#FFD700';
ctx.fill();
// Update and draw clouds
updateClouds();
drawClouds();
// Update and draw buildings
updateBuildings();
drawBuildings();
// Update and draw stars
updateStars();
drawStars();
// Update and draw plane
updatePlane();
drawPlane();
// Check for collisions
checkCollisions();
// Increase difficulty over time
gameSpeed += 0.001;
// Continue game loop
animationId = requestAnimationFrame(gameLoop);
}
function updatePlane() {
// Move plane based on key presses
if (keys['ArrowUp'] || keys['w']) {
plane.y -= plane.speed;
}
if (keys['ArrowDown'] || keys['s']) {
plane.y += plane.speed;
}
if (keys['ArrowLeft'] || keys['a']) {
plane.x -= plane.speed;
}
if (keys['ArrowRight'] || keys['d']) {
plane.x += plane.speed;
}
// Keep plane within canvas bounds
plane.x = Math.max(0, Math.min(plane.x, canvas.width - plane.width));
plane.y = Math.max(0, Math.min(plane.y, canvas.height - plane.height));
}
function drawPlane() {
ctx.save();
ctx.translate(plane.x + plane.width / 2, plane.y + plane.height / 2);
ctx.rotate(Math.PI / 2);
ctx.drawImage(
createPlaneImage(),
-plane.width / 2,
-plane.height / 2,
plane.width,
plane.height
);
ctx.restore();
}
function createPlaneImage() {
const img = new Image();
img.src = 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="%23ffffff" d="M22,16.21V14l-8-5V3.5A1.5,1.5,0,0,0,12.5,2h0A1.5,1.5,0,0,0,11,3.5V9l-8,5v2.21L11,14.6V19l-2,1.5V22l3.5-1,3.5,1V20.5L14,19v-4.4Z"/></svg>';
return img;
}
function updateBuildings() {
// Add new buildings
if (Math.random() < 0.02) {
const isBurjKhalifa = Math.random() < 0.1; // 10% chance for Burj Khalifa
const width = isBurjKhalifa ? 30 : 50 + Math.random() * 70;
const height = isBurjKhalifa ? 300 + Math.random() * 100 : 100 + Math.random() * 200;
buildings.push({
x: canvas.width,
y: canvas.height - height,
width: width,
height: height,
speed: gameSpeed,
isBurjKhalifa: isBurjKhalifa
});
}
// Move buildings
for (let i = buildings.length - 1; i >= 0; i--) {
buildings[i].x -= buildings[i].speed;
// Remove buildings that are off screen
if (buildings[i].x + buildings[i].width < 0) {
buildings.splice(i, 1);
}
}
}
function drawBuildings() {
buildings.forEach(building => {
if (building.isBurjKhalifa) {
// Draw Burj Khalifa
ctx.fillStyle = '#555';
ctx.fillRect(building.x, building.y, building.width, building.height);
// Add windows
ctx.fillStyle = '#87CEEB';
const windowWidth = 4;
const windowHeight = 8;
const windowSpacing = 10;
for (let y = building.y + 10; y < building.y + building.height - 10; y += windowSpacing) {
for (let x = building.x + 5; x < building.x + building.width - 5; x += windowSpacing) {
if (Math.random() > 0.3) { // 70% chance to draw a window
ctx.fillRect(x, y, windowWidth, windowHeight);
}
}
}
// Add spire
ctx.beginPath();
ctx.moveTo(building.x + building.width / 2, building.y);
ctx.lineTo(building.x + building.width / 2 - 10, building.y - 50);
ctx.lineTo(building.x + building.width / 2 + 10, building.y - 50);
ctx.closePath();
ctx.fillStyle = '#333';
ctx.fill();
} else {
// Draw regular building
const gradient = ctx.createLinearGradient(0, building.y, 0, building.y + building.height);
gradient.addColorStop(0, '#FFD700');
gradient.addColorStop(1, '#FFA500');
ctx.fillStyle = gradient;
ctx.fillRect(building.x, building.y, building.width, building.height);
// Add windows
ctx.fillStyle = '#333';
const windowWidth = 8;
const windowHeight = 12;
const windowSpacing = 15;
for (let y = building.y + 10; y < building.y + building.height - 10; y += windowSpacing) {
for (let x = building.x + 10; x < building.x + building.width - 10; x += windowSpacing) {
ctx.fillRect(x, y, windowWidth, windowHeight);
}
}
}
});
}
function updateStars() {
// Add new stars
if (Math.random() < 0.03) {
stars.push({
x: canvas.width,
y: 50 + Math.random() * (canvas.height - 150),
width: 20,
height: 20,
speed: gameSpeed
});
}
// Move stars
for (let i = stars.length - 1; i >= 0; i--) {
stars[i].x -= stars[i].speed;
// Remove stars that are off screen
if (stars[i].x + stars[i].width < 0) {
stars.splice(i, 1);
}
}
}
function drawStars() {
stars.forEach(star => {
ctx.save();
ctx.translate(star.x + star.width / 2, star.y + star.height / 2);
ctx.rotate(Date.now() / 200); // Make star rotate
ctx.drawImage(
createStarImage(),
-star.width / 2,
-star.height / 2,
star.width,
star.height
);
ctx.restore();
});
}
function createStarImage() {
const img = new Image();
img.src = 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="%23FFD700" d="M12,17.27L18.18,21l-1.64-7.03L22,9.24l-7.19-.61L12,2,9.19,8.63,2,9.24l5.46,4.73L5.82,21Z"/></svg>';
return img;
}
function updateClouds() {
// Add new clouds
if (Math.random() < 0.01) {
const size = 30 + Math.random() * 50;
clouds.push({
x: canvas.width,
y: 50 + Math.random() * (canvas.height / 2),
width: size,
height: size / 2,
speed: gameSpeed / 2
});
}
// Move clouds
for (let i = clouds.length - 1; i >= 0; i--) {
clouds[i].x -= clouds[i].speed;
// Remove clouds that are off screen
if (clouds[i].x + clouds[i].width < 0) {
clouds.splice(i, 1);
}
}
}
function drawClouds() {
clouds.forEach(cloud => {
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
// Draw cloud as overlapping circles
ctx.beginPath();
ctx.arc(cloud.x, cloud.y, cloud.width / 2, 0, Math.PI * 2);
ctx.arc(cloud.x + cloud.width / 3, cloud.y - cloud.height / 3, cloud.width / 3, 0, Math.PI * 2);
ctx.arc(cloud.x + cloud.width / 2, cloud.y, cloud.width / 3, 0, Math.PI * 2);
ctx.arc(cloud.x + cloud.width / 3, cloud.y + cloud.height / 3, cloud.width / 4, 0, Math.PI * 2);
ctx.fill();
});
}
function checkCollisions() {
// Check building collisions
for (let i = 0; i < buildings.length; i++) {
const building = buildings[i];
if (
plane.x < building.x + building.width &&
plane.x + plane.width > building.x &&
plane.y < building.y + building.height &&
plane.y + plane.height > building.y
) {
gameOver();
return;
}
}
// Check star collisions
for (let i = stars.length - 1; i >= 0; i--) {
const star = stars[i];
if (
plane.x < star.x + star.width &&
plane.x + plane.width > star.x &&
plane.y < star.y + star.height &&
plane.y + plane.height > star.y
) {
// Collect star
stars.splice(i, 1);
score += 10;
scoreElement.textContent = score;
}
}
}
function gameOver() {
gameRunning = false;
cancelAnimationFrame(animationId);
finalScoreElement.textContent = score;
gameOverScreen.style.display = 'flex';
}
// Preload images
createPlaneImage();
createStarImage();
</script>
</html>