Using HTML + JS (one file) create a 2d top down view, add our character (blue square) and add environment (snowy forest created from shapes), provide full code
Assistant
Sure! Below is a complete HTML file that uses JavaScript to create a 2D top-down view. It includes a blue square representing the character and a snowy forest environment created using various shapes on the canvas. You can save this code as an .html file and open it in your web browser to see it in action.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2D Top-Down Snowy Forest</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #87CEEB; /* Light blue sky */
}
canvas {
display: block;
background-color: #FFFFFF; /* White snow ground */
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas to full window size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Character properties
const character = {
x: canvas.width / 2 - 25, // Starting X position
y: canvas.height / 2 - 25, // Starting Y position
size: 50, // Size of the square
color: 'blue',
speed: 5,
dx: 0,
dy: 0
};
// Handle keyboard input
const keys = {
ArrowUp: false,
ArrowDown: false,
ArrowLeft: false,
ArrowRight: false
};
window.addEventListener('keydown', function(e) {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = true;
}
});
window.addEventListener('keyup', function(e) {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = false;
}
});
// Update character position
function updateCharacter() {
character.dx = 0;
character.dy = 0;
if (keys.ArrowUp) character.dy = -character.speed;
if (keys.ArrowDown) character.dy = character.speed;
if (keys.ArrowLeft) character.dx = -character.speed;
if (keys.ArrowRight) character.dx = character.speed;
character.x += character.dx;
character.y += character.dy;
// Keep character within canvas boundaries
if (character.x < 0) character.x = 0;
if (character.y < 0) character.y = 0;
if (character.x + character.size > canvas.width) character.x = canvas.width - character.size;
if (character.y + character.size > canvas.height) character.y = canvas.height - character.size;
}
// Draw the character
function drawCharacter() {
ctx.fillStyle = character.color;
ctx.fillRect(character.x, character.y, character.size, character.size);
}
// Draw trees
function drawTrees() {
// Simple pine trees using triangles and rectangles
for (let i = 0; i < 50; i++) { // Adjust number for density
let treeX = Math.random() * canvas.width;
let treeY = Math.random() * canvas.height;
let treeHeight = 50 + Math.random() * 50;
let treeWidth = treeHeight / 2;
// Avoid drawing trees over the character
if (
treeX > character.x - 100 && treeX < character.x + character.size + 100 &&
treeY > character.y - 100 && treeY < character.y + character.size + 100
) {
continue;
}
// Draw the trunk
ctx.fillStyle = '#8B4513'; // Brown color
ctx.fillRect(treeX - treeWidth / 8, treeY, treeWidth / 4, treeHeight / 2);
// Draw the foliage
ctx.fillStyle = 'green';
ctx.beginPath();
ctx.moveTo(treeX, treeY - treeHeight / 2);
ctx.lineTo(treeX - treeWidth / 2, treeY);
ctx.lineTo(treeX + treeWidth / 2, treeY);
ctx.closePath();
ctx.fill();
}
}
// Draw snowflakes
function drawSnowflakes() {
const snowflakeCount = 100;
ctx.fillStyle = 'white';
for (let i = 0; i < snowflakeCount; i++) {
let x = Math.random() * canvas.width;
let y = Math.random() * canvas.height;
let radius = Math.random() * 2 + 1;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
}
}
// Draw the ground (optional if you want a color different from canvas background)
function drawGround() {
ctx.fillStyle = '#FFFFFF'; // White snow
ctx.fillRect(0, canvas.height / 2, canvas.width, canvas.height / 2);
}
// Main draw function
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSnowflakes();
drawTrees();
drawCharacter();
// drawGround(); // Uncomment if you want to draw a separate ground
}
// Update game objects
function update() {
updateCharacter();
draw();
requestAnimationFrame(update);
}
// Start the game loop
update();
// Handle window resize
window.addEventListener('resize', function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Optionally reset character position or other elements
});
</script>
</body>
</html>
Explanation of the Code:
Canvas Setup:
The canvas is set to cover the entire browser window.
The background color of the body is set to a light blue to simulate the sky, while the canvas background is white to represent snow.
Character:
Represented by a blue square positioned at the center of the canvas.
The character can be moved using the arrow keys (Up, Down, Left, Right).
Environment:
Trees: Randomly placed pine trees are drawn using brown rectangles for trunks and green triangles for foliage.
Snowflakes: Small white circles scattered across the canvas to represent falling snow.
Ground: An optional white rectangle can be drawn to represent the snow-covered ground area.
Controls:
The arrow keys allow you to move the blue square around the canvas.
The movement is smooth and constrained within the canvas boundaries.
Responsive Design:
The canvas resizes with the browser window to maintain full coverage.
Trees and snowflakes are regenerated upon resizing to fill the new space.
Feel free to customize the number of trees, snowflakes, character size, and colors to better suit your preferences!