Using HTML + JS (one file) create a top-down view game where make endless desert, add our player and car, we can enter car by pressing "Enter" and ride it, the car have realistic physics
Assistant
Here is a simple top-down game created using HTML and JavaScript in a single file. The game features an endless desert, a player, and a car. The player can enter the car by pressing "Enter" when nearby and drive it with realistic physics.
html
<!DOCTYPE html>
<html>
<head>
<title>Desert Driving Game</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { background: #EDC9AF; display: block; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get the canvas and context
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Player variables
var player = {
x: 0,
y: 0,
width: 20,
height: 20,
color: "blue",
speed: 3,
inCar: false
};
// Car variables
var car = {
x: 100,
y: 0,
width: 50,
height: 30,
color: "red",
speed: 0,
angle: 0,
maxSpeed: 5,
acceleration: 0.2,
friction: 0.05,
inUse: false
};
// Keypresses
var keys = {};
window.addEventListener("keydown", function(e){
keys[e.key.toLowerCase()] = true;
// Entering/exiting car
if (e.key === "Enter") {
var dx = player.x - car.x;
var dy = player.y - car.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (!player.inCar && distance < 50) {
player.inCar = true;
car.inUse = true;
} else if (player.inCar) {
player.inCar = false;
car.inUse = false;
// Place player near car
player.x = car.x - Math.cos(car.angle) * 50;
player.y = car.y - Math.sin(car.angle) * 50;
}
}
});
window.addEventListener("keyup", function(e){
keys[e.key.toLowerCase()] = false;
});
// Game loop
function gameLoop(){
update();
render();
requestAnimationFrame(gameLoop);
}
function update(){
if (player.inCar) {
// Car controls
if (keys['w']) { // Accelerate forward
car.speed += car.acceleration;
if (car.speed > car.maxSpeed) car.speed = car.maxSpeed;
}
if (keys['s']) { // Brake/reverse
car.speed -= car.acceleration;
if (car.speed < -car.maxSpeed/2) car.speed = -car.maxSpeed/2;
}
if (keys['a']) { // Turn left
car.angle -= 0.05 * car.speed;
}
if (keys['d']) { // Turn right
car.angle += 0.05 * car.speed;
}
// Apply friction
if (car.speed > 0) {
car.speed -= car.friction;
if (car.speed < 0) car.speed = 0;
}
if (car.speed < 0) {
car.speed += car.friction;
if (car.speed > 0) car.speed = 0;
}
// Update car position
car.x += Math.cos(car.angle) * car.speed;
car.y += Math.sin(car.angle) * car.speed;
} else {
// Player controls
if (keys['w']) { // Up
player.y -= player.speed;
}
if (keys['s']) { // Down
player.y += player.speed;
}
if (keys['a']) { // Left
player.x -= player.speed;
}
if (keys['d']) { // Right
player.x += player.speed;
}
}
}
function render(){
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Since the world is endless, simulate camera following the player or car
var camX, camY;
if (player.inCar) {
camX = car.x - canvas.width / 2;
camY = car.y - canvas.height / 2;
} else {
camX = player.x - canvas.width / 2;
camY = player.y - canvas.height / 2;
}
ctx.save();
ctx.translate(-camX, -camY);
// Draw some desert elements (randomly placed cacti for visual interest)
for (var i = -5000; i < 5000; i += 200) {
for (var j = -5000; j < 5000; j += 200) {
if (Math.random() < 0.05) {
drawCactus(i + Math.random() * 200, j + Math.random() * 200);
}
}
}
// Draw car
ctx.save();
ctx.translate(car.x, car.y);
ctx.rotate(car.angle);
ctx.fillStyle = car.color;
ctx.fillRect(-car.width / 2, -car.height / 2, car.width, car.height);
ctx.restore();
// Draw player if not in car
if (!player.inCar) {
ctx.fillStyle = player.color;
ctx.fillRect(player.x - player.width/2, player.y - player.height/2, player.width, player.height);
}
ctx.restore();
}
function drawCactus(x, y){
ctx.fillStyle = "green";
ctx.fillRect(x, y, 10, 30);
ctx.fillRect(x - 5, y + 10, 5, 10);
ctx.fillRect(x + 10, y + 10, 5, 10);
}
// Start game loop
gameLoop();
// Adjust canvas size on window resize
window.addEventListener('resize', function(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html>
Instructions:
Movement Controls:
Player (on foot): Use W, A, S, D keys to move up, left, down, and right respectively.
Car (when inside): Use W to accelerate, S to brake/reverse, A to turn left, and D to turn right.
Entering/Exiting the Car:
Approach the car (get within 50 pixels).
Press Enter to enter the car.
While in the car, press Enter again to exit.
Features:
Endless Desert: The game simulates an endless desert by translating the canvas based on the player's or car's position.
Player: Represented by a blue rectangle that can move freely when not in the car.
Car: Represented by a red rectangle with realistic physics, including acceleration, deceleration, friction, and steering.
Realistic Car Physics: The car accelerates when you press W, decelerates when you press S, and slows down over time due to friction. Steering is more effective at higher speeds.
Desert Elements: Randomly placed cacti are drawn to provide visual interest in the desert environment.
Notes:
The game uses HTML5 Canvas for rendering.
All code is contained within a single HTML file for simplicity.
The physics and graphics are kept simple for demonstration purposes.
No external libraries are required; everything is implemented using vanilla JavaScript.
To Run the Game:
Copy the entire code above into a new text file.
Save the file with an .html extension (e.g., desert_game.html).
Open the file with a modern web browser (e.g., Chrome, Firefox, Edge).
The game should start automatically, and you can begin playing.