Make a top down game using HTML + JS (one file) that have world similar to SuperWorldBox game, this game fully working
Assistant
Certainly! Below is the code for a simple top-down game similar to SuperWorldBox, written in HTML and JavaScript within a single file. You can copy the code into a file with an .html extension and open it in a web browser to play the game.
html
<!DOCTYPE html>
<html>
<head>
<title>SuperWorldBox-like Game</title>
<style>
canvas {
border: 1px solid black;
background-color: #87CEEB; /* Sky blue background */
display: block;
margin: 0 auto;
}
body {
text-align: center;
font-family: Arial, sans-serif;
}
#instructions {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>SuperWorldBox-like Game</h1>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="instructions">
<p>Left-click on the canvas to create terrain (grass).</p>
<p>Right-click to add entities on grass tiles.</p>
<p>Press 'P' to plant trees, 'A' to add animals, 'H' to add humans.</p>
</div>
<script>
// Canvas setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game world
const world = {
width: canvas.width,
height: canvas.height,
tiles: [],
tileSize: 20
};
// Initialize world tiles
for (let x = 0; x < world.width / world.tileSize; x++) {
world.tiles[x] = [];
for (let y = 0; y < world.height / world.tileSize; y++) {
world.tiles[x][y] = { type: 'water' }; // Default to water
}
}
// Entities
const entities = [];
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Update game state
function update() {
// Update entities
entities.forEach(entity => {
entity.update();
});
}
// Draw game
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw tiles
for (let x = 0; x < world.tiles.length; x++) {
for (let y = 0; y < world.tiles[x].length; y++) {
const tile = world.tiles[x][y];
let color;
switch (tile.type) {
case 'water':
color = '#1E90FF';
break;
case 'grass':
color = '#32CD32';
break;
}
ctx.fillStyle = color;
ctx.fillRect(x * world.tileSize, y * world.tileSize, world.tileSize, world.tileSize);
}
}
// Draw entities
entities.forEach(entity => {
entity.draw(ctx);
});
}
// Terrain manipulation
canvas.addEventListener('click', function(event) {
const rect = canvas.getBoundingClientRect();
const x = Math.floor((event.clientX - rect.left) / world.tileSize);
const y = Math.floor((event.clientY - rect.top) / world.tileSize);
if (world.tiles[x] && world.tiles[x][y]) {
world.tiles[x][y].type = 'grass';
}
});
// Entity classes
class Entity {
constructor(x, y) {
this.x = x;
this.y = y;
}
update() {}
draw(ctx) {}
}
class Tree extends Entity {
constructor(x, y) {
super(x, y);
}
draw(ctx) {
ctx.fillStyle = '#8B4513'; // Brown trunk
ctx.fillRect(this.x * world.tileSize + 7, this.y * world.tileSize + 10, 6, 10);
ctx.fillStyle = '#006400'; // Dark green leaves
ctx.beginPath();
ctx.moveTo(this.x * world.tileSize + 10, this.y * world.tileSize);
ctx.lineTo(this.x * world.tileSize + 5, this.y * world.tileSize + 10);
ctx.lineTo(this.x * world.tileSize + 15, this.y * world.tileSize + 10);
ctx.closePath();
ctx.fill();
}
}
class Animal extends Entity {
constructor(x, y) {
super(x, y);
}
update() {
// Simple movement
const dir = Math.floor(Math.random() * 4);
switch (dir) {
case 0:
this.x = Math.max(0, this.x - 1);
break;
case 1:
this.x = Math.min(world.tiles.length - 1, this.x + 1);
break;
case 2:
this.y = Math.max(0, this.y - 1);
break;
case 3:
this.y = Math.min(world.tiles[0].length - 1, this.y + 1);
break;
}
}
draw(ctx) {
ctx.fillStyle = '#FFA500'; // Orange
ctx.fillRect(this.x * world.tileSize + 5, this.y * world.tileSize + 5, 10, 10);
}
}
class Human extends Entity {
constructor(x, y) {
super(x, y);
}
update() {
// Simple movement
const dir = Math.floor(Math.random() * 4);
switch (dir) {
case 0:
this.x = Math.max(0, this.x - 1);
break;
case 1:
this.x = Math.min(world.tiles.length - 1, this.x + 1);
break;
case 2:
this.y = Math.max(0, this.y - 1);
break;
case 3:
this.y = Math.min(world.tiles[0].length - 1, this.y + 1);
break;
}
}
draw(ctx) {
ctx.fillStyle = '#FFD700'; // Gold
ctx.fillRect(this.x * world.tileSize + 5, this.y * world.tileSize + 5, 10, 10);
}
}
// Keyboard controls
let currentEntityType = 'tree';
document.addEventListener('keydown', function(event) {
if (event.key === 'p' || event.key === 'P') {
currentEntityType = 'tree';
} else if (event.key === 'a' || event.key === 'A') {
currentEntityType = 'animal';
} else if (event.key === 'h' || event.key === 'H') {
currentEntityType = 'human';
}
});
// Add entities on right-click
canvas.addEventListener('contextmenu', function(event) {
event.preventDefault();
const rect = canvas.getBoundingClientRect();
const x = Math.floor((event.clientX - rect.left) / world.tileSize);
const y = Math.floor((event.clientY - rect.top) / world.tileSize);
if (world.tiles[x] && world.tiles[x][y] && world.tiles[x][y].type === 'grass') {
let entity;
if (currentEntityType === 'tree') {
entity = new Tree(x, y);
} else if (currentEntityType === 'animal') {
entity = new Animal(x, y);
} else if (currentEntityType === 'human') {
entity = new Human(x, y);
}
if (entity) {
entities.push(entity);
}
}
return false;
});
// Start the game
gameLoop();
</script>
</body>
</html>
Instructions:
Save the code above into a file named superworldbox.html.
Open the file in a modern web browser (e.g., Chrome, Firefox).
Left-click on the canvas to transform water tiles into grass (create land).
Right-click on grass tiles to place entities:
Press 'P' to select planting trees.
Press 'A' to select adding animals.
Press 'H' to select adding humans.
Features:
Terrain Editing: Click to change water into grass, creating your own landmasses.
Entity Placement: Right-click to place trees, animals, or humans on grass tiles.
Simple Simulation: Animals and humans move randomly across the land.
Feel free to expand upon this code by adding more features, such as:
Additional entity types.
Interactions between entities (e.g., animals eating plants).
More complex AI behaviors.
Environmental effects (e.g., spreading of trees).
Note: This is a simplified version of a SuperWorldBox-like game designed for educational purposes. It demonstrates basic concepts of game development using HTML5 Canvas and JavaScript.