Sure! Below is a complete HTML file with embedded JavaScript that creates a 2D top-down game. The game generates a pixelated world that resembles Earth's landscape, and allows the player to spawn 8-bit characters by clicking on the landscape with the left mouse button.
Copy the entire code below into a single file with a .html extension and open it in a web browser to play the game.
<!DOCTYPE html>
<html>
<head>
<title>2D Top-Down Game</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; background: #000; }
#instructions {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-family: Arial, sans-serif;
background-color: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="instructions">
<h2>2D Top-Down Game</h2>
<p>Click on the landscape to spawn 8-bit characters.</p>
</div>
<script>
// Setup canvas
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Tile and world dimensions
const tileSize = 16;
const worldWidth = Math.floor(canvas.width / tileSize);
const worldHeight = Math.floor(canvas.height / tileSize);
// World array
const world = [];
for(let y = 0; y < worldHeight; y++) {
world[y] = [];
for(let x = 0; x < worldWidth; x++) {
world[y][x] = 0; // Initialize with zeroes
}
}
// Generate terrain resembling Earth's landscape
const centerX = worldWidth / 2;
const centerY = worldHeight / 2;
const maxDistance = Math.sqrt(centerX*centerX + centerY*centerY);
for(let y = 0; y < worldHeight; y++) {
for(let x = 0; x < worldWidth; x++) {
const dx = x - centerX;
const dy = y - centerY;
const distance = Math.sqrt(dx*dx + dy*dy);
let height = 1 - (distance / maxDistance); // Height decreases from center
height += (Math.random() - 0.5) * 0.5; // Add noise
world[y][x] = height;
}
}
// Function to draw the world
function drawWorld() {
for(let y = 0; y < worldHeight; y++) {
for(let x = 0; x < worldWidth; x++) {
const height = world[y][x];
if(height > 0.3) {
// Land
ctx.fillStyle = '#228B22'; // Forest green
} else {
// Water
ctx.fillStyle = '#1E90FF'; // Blue
}
ctx.fillRect(x * tileSize, y * tileSize, tileSize, tileSize);
}
}
}
// Character array
const characters = [];
// Character class
class Character {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
// Draw 8-bit character sprite
ctx.fillStyle = '#FFD700'; // Gold color
ctx.fillRect(this.x * tileSize + 4, this.y * tileSize + 2, 8, 12); // Body
ctx.fillStyle = '#FFFFFF'; // White for face
ctx.fillRect(this.x * tileSize + 5, this.y * tileSize + 4, 6, 6); // Face
ctx.fillStyle = '#000000'; // Black for eyes
ctx.fillRect(this.x * tileSize + 6, this.y * tileSize + 5, 1, 1); // Left eye
ctx.fillRect(this.x * tileSize + 9, this.y * tileSize + 5, 1, 1); // Right eye
ctx.fillRect(this.x * tileSize + 7, this.y * tileSize + 7, 2, 1); // Mouth
}
}
// Handle mouse clicks to spawn characters
canvas.addEventListener('click', function(e) {
const rect = canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
const x = Math.floor(mouseX / tileSize);
const y = Math.floor(mouseY / tileSize);
// Check bounds
if(x >= 0 && x < worldWidth && y >= 0 && y < worldHeight) {
// Only spawn on land
if(world[y][x] > 0.3) {
const character = new Character(x, y);
characters.push(character);
draw();
}
}
});
// Function to draw everything
function draw() {
drawWorld();
characters.forEach(character => character.draw());
}
// Initial draw
draw();
</script>
</body>
</html>
Instructions:
- Spawn Characters: Click on the land areas (green tiles) to spawn 8-bit characters.
- Explore the World: The world is randomly generated each time you reload the page, resembling Earth's landscape with land and water.
- Enjoy the Retro Style: The graphics are intentionally pixelated to give an 8-bit retro gaming feel.
Features:
- World Generation: The game generates a pixelated terrain with land and water tiles. The land is concentrated towards the center to resemble an island, creating an Earth-like landscape.
- 8-bit Characters: Characters are simple 8-bit sprites that appear where you click, as long as the tile is land.
- Single File: The entire game is contained within a single HTML file, making it easy to run and share.
Technical Overview:
- Canvas API: The game uses the HTML5 Canvas API to draw graphics on the screen.
- Terrain Generation: A simple algorithm calculates the distance of each tile from the center to create a height map. Random noise is added to make the terrain more natural.
- Event Handling: Mouse click events are captured to determine where to spawn characters.
- Responsive Design: The canvas is set to the full width and height of the browser window, making the game responsive.
Note: Since the game uses random terrain generation, each playthrough will offer a unique landscape to explore and populate with characters.
Enjoy creating your own pixelated world and populating it with 8-bit characters!