o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Make tower building simulator with physics using HTML + JS (one file)
- User
Make tower building simulator with physics using HTML + JS (one file)
- Assistant
Make tower building simulator with physics using HTML + JS (one file)
Here's a simple tower building simulator with physics using HTML and JavaScript, all contained in one file. This game allows you to drop blocks to build a tower as high as possible without it collapsing.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tower Building Simulator</title>
<style>
canvas {
background: #87CEEB; /* Sky blue background */
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<script>
// Get canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game variables
const gravity = 0.5;
const friction = 0.8;
let blocks = [];
let currentBlock = null;
let gameOver = false;
// Block class
class Block {
constructor(x, y, width, height, color) {
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.width = width;
this.height = height;
this.color = color;
this.isFalling = true;
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
update() {
if (this.isFalling) {
this.vy += gravity;
this.y += this.vy;
this.x += this.vx;
// Check for collisions with other blocks
for (let block of blocks) {
if (block !== this && this.collidesWith(block)) {
this.vy = 0;
this.vx = 0;
this.isFalling = false;
this.y = block.y - this.height;
break;
}
}
// Check if block hits the ground
if (this.y + this.height >= canvas.height) {
this.vy = 0;
this.vx = 0;
this.isFalling = false;
this.y = canvas.height - this.height;
}
// Check for out of bounds (game over condition)
if (this.x + this.width < 0 || this.x > canvas.width || this.y > canvas.height) {
gameOver = true;
}
}
}
collidesWith(other) {
return !(this.x + this.width < other.x ||
this.x > other.x + other.width ||
this.y + this.height < other.y ||
this.y > other.y + other.height);
}
}
// Initialize the first block
function spawnBlock() {
const width = 80;
const height = 20;
const x = (canvas.width - width) / 2;
const y = 0;
const color = '#' + Math.floor(Math.random()*16777215).toString(16);
currentBlock = new Block(x, y, width, height, color);
}
// Handle user input
document.addEventListener('keydown', function(e) {
if (currentBlock && currentBlock.isFalling) {
if (e.key === 'ArrowLeft') {
currentBlock.vx = -5;
} else if (e.key === 'ArrowRight') {
currentBlock.vx = 5;
}
}
});
document.addEventListener('keyup', function(e) {
if (currentBlock) {
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
currentBlock.vx = 0;
} else if (e.key === ' ' || e.key === 'ArrowDown') {
currentBlock.isFalling = true;
}
}
});
// Game loop
function gameLoop() {
if (gameOver) {
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#FFF';
ctx.font = '48px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2);
return;
}
requestAnimationFrame(gameLoop);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw blocks
if (currentBlock) {
currentBlock.update();
currentBlock.draw();
if (!currentBlock.isFalling) {
blocks.push(currentBlock);
spawnBlock();
}
} else {
spawnBlock();
}
for (let block of blocks) {
block.draw();
}
}
// Start the game
gameLoop();
</script>
</body>
</html>
vy) of the blocks when they are falling.You can modify the game by changing parameters like block size, gravity, or adding more features like scoring or different block shapes.
Note: This is a simplified physics simulation intended for educational purposes and may not reflect real-world physics accurately.
Not yet included in a published release.