Here's a basic HTML5 flight simulator with simple graphics and keyboard controls:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Flight Simulator</title>
<style>
canvas {
border: 2px solid black;
background-color: skyblue;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Plane properties
let plane = {
x: 400,
y: 300,
width: 30,
height: 20,
speed: 0,
angle: 0
};
// Buildings
const buildings = [];
for (let i = 0; i < 20; i++) {
buildings.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
width: Math.random() * 50 + 20,
height: Math.random() * 100 + 50,
color: `rgb(${Math.random()*100}, ${Math.random()*100}, ${Math.random()*100})`
});
}
// Draw functions
function drawPlane() {
ctx.save();
ctx.translate(plane.x, plane.y);
ctx.rotate(plane.angle);
ctx.fillStyle = 'gray';
ctx.fillRect(-plane.width/2, -plane.height/2, plane.width, plane.height);
ctx.restore();
}
function drawBuildings() {
buildings.forEach(building => {
ctx.fillStyle = building.color;
ctx.fillRect(building.x, building.y, building.width, building.height);
});
}
function clear() {
ctx.fillStyle = 'skyblue';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
// Game loop
function gameLoop() {
clear();
drawBuildings();
drawPlane();
// Keyboard controls
if (keys.ArrowUp) {
plane.speed += 0.1;
}
if (keys.ArrowDown) {
plane.speed -= 0.1;
}
if (keys.ArrowLeft) {
plane.angle -= 0.1;
}
if (keys.ArrowRight) {
plane.angle += 0.1;
}
// Movement
plane.x += Math.cos(plane.angle) * plane.speed;
plane.y += Math.sin(plane.angle) * plane.speed;
// Wrap around screen
plane.x = (plane.x + canvas.width) % canvas.width;
plane.y = (plane.y + canvas.height) % canvas.height;
// Friction
plane.speed *= 0.99;
requestAnimationFrame(gameLoop);
}
// Keyboard handling
const keys = {};
window.addEventListener('keydown', (e) => {
keys[e.key] = true;
});
window.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
// Start game
gameLoop();
</script>
</body>
</html>
Key features:
- Simple 2D flight simulator
- Keyboard controls (arrow keys)
- Random building generation
- Plane movement with rotation
- Wrapping screen boundaries
- Basic physics (speed, friction)
Controls:
- Up Arrow: Accelerate
- Down Arrow: Decelerate
- Left Arrow: Rotate left
- Right Arrow: Rotate right
The simulator provides a basic flying experience with simple graphics and intuitive controls. The plane can fly around a sky with randomly generated buildings, with smooth movement and rotation.