o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write a mini golf game in HTML canvas with randomly generated map
- User
Write a mini golf game in HTML canvas with randomly generated map
- Assistant
Write a mini golf game in HTML canvas with randomly generated map
Sure! Below is a simple implementation of a mini golf game using HTML Canvas with a randomly generated map. The game includes a ball, a hole, and randomly placed rectangular obstacles. The player can click and drag to set the direction and power of the shot.
mini-golf.html.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mini Golf Game</title>
<style>
body {
text-align: center;
background-color: #2E8B57;
margin: 0;
padding: 0;
}
canvas {
background-color: #228B22;
display: block;
margin: 20px auto;
border: 2px solid #006400;
}
#info {
color: white;
font-family: Arial, sans-serif;
margin-top: -10px;
}
</style>
</head>
<body>
<h1 style="color:white;">Mini Golf Game</h1>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="info">Click and drag to aim and set power. Get the ball in the hole!</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
// Ball and Hole properties
const ballRadius = 10;
const holeRadius = 15;
let ball = {
x: 50,
y: HEIGHT - 50,
vx: 0,
vy: 0,
color: 'white'
};
let hole = {
x: WIDTH - 50,
y: 50,
radius: holeRadius,
color: 'black'
};
// Obstacles
const obstacles = [];
const numObstacles = 5;
// Generate random obstacles
function generateObstacles() {
obstacles.length = 0;
for (let i = 0; i < numObstacles; i++) {
let width = 100;
let height = 20;
let x = Math.random() * (WIDTH - width - 100) + 50;
let y = Math.random() * (HEIGHT - height - 100) + 50;
// Ensure obstacles do not overlap with ball or hole
if (distance(x + width / 2, y + height / 2, ball.x, ball.y) < 100 ||
distance(x + width / 2, y + height / 2, hole.x, hole.y) < 100) {
i--;
continue;
}
obstacles.push({x, y, width, height, color: 'grey'});
}
}
// Utility function to calculate distance between two points
function distance(x1, y1, x2, y2) {
return Math.hypot(x2 - x1, y2 - y1);
}
// Draw ball
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.fill();
ctx.closePath();
}
// Draw hole
function drawHole() {
ctx.beginPath();
ctx.arc(hole.x, hole.y, hole.radius, 0, Math.PI * 2);
ctx.fillStyle = hole.color;
ctx.fill();
ctx.closePath();
}
// Draw obstacles
function drawObstacles() {
obstacles.forEach(obs => {
ctx.fillStyle = obs.color;
ctx.fillRect(obs.x, obs.y, obs.width, obs.height);
});
}
// Reset game
function resetGame() {
ball.x = 50;
ball.y = HEIGHT - 50;
ball.vx = 0;
ball.vy = 0;
generateObstacles();
}
// Collision detection between circle and rectangle
function circleRectCollision(circle, rect) {
let distX = Math.abs(circle.x - rect.x - rect.width / 2);
let distY = Math.abs(circle.y - rect.y - rect.height / 2);
if (distX > (rect.width / 2 + circle.radius)) { return false; }
if (distY > (rect.height / 2 + circle.radius)) { return false; }
if (distX <= (rect.width / 2)) { return true; }
if (distY <= (rect.height / 2)) { return true; }
let dx = distX - rect.width / 2;
let dy = distY - rect.height / 2;
return (dx * dx + dy * dy <= (circle.radius * circle.radius));
}
// Update game state
function update() {
// Update ball position
ball.x += ball.vx;
ball.y += ball.vy;
// Apply friction
ball.vx *= 0.98;
ball.vy *= 0.98;
// Stop the ball if velocity is very low
if (Math.abs(ball.vx) < 0.1) ball.vx = 0;
if (Math.abs(ball.vy) < 0.1) ball.vy = 0;
// Collision with walls
if (ball.x - ballRadius < 0 || ball.x + ballRadius > WIDTH) {
ball.vx = -ball.vx;
ball.x = ball.x - ballRadius < 0 ? ballRadius : WIDTH - ballRadius;
}
if (ball.y - ballRadius < 0 || ball.y + ballRadius > HEIGHT) {
ball.vy = -ball.vy;
ball.y = ball.y - ballRadius < 0 ? ballRadius : HEIGHT - ballRadius;
}
// Collision with obstacles
obstacles.forEach(obs => {
if (circleRectCollision(ball, obs)) {
// Simple collision response: reverse velocity
// More sophisticated response can be implemented
if (ball.vx > 0) {
ball.vx = -ball.vx;
ball.x = obs.x - ballRadius;
} else {
ball.vx = -ball.vx;
ball.x = obs.x + obs.width + ballRadius;
}
if (ball.vy > 0) {
ball.vy = -ball.vy;
ball.y = obs.y - ballRadius;
} else {
ball.vy = -ball.vy;
ball.y = obs.y + obs.height + ballRadius;
}
}
});
// Check if ball is in the hole
if (distance(ball.x, ball.y, hole.x, hole.y) < hole.radius) {
alert("Congratulations! You've sunk the ball!");
resetGame();
}
}
// Render the game
function render() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
drawHole();
drawObstacles();
drawBall();
}
// Game loop
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
// Handle mouse input for aiming and shooting
let isDragging = false;
let dragStart = {x: 0, y: 0};
canvas.addEventListener('mousedown', (e) => {
// Only allow aiming if the ball is stationary
if (ball.vx === 0 && ball.vy === 0) {
isDragging = true;
const rect = canvas.getBoundingClientRect();
dragStart.x = e.clientX - rect.left;
dragStart.y = e.clientY - rect.top;
}
});
canvas.addEventListener('mousemove', (e) => {
if (isDragging) {
const rect = canvas.getBoundingClientRect();
const currentX = e.clientX - rect.left;
const currentY = e.clientY - rect.top;
render();
// Draw the aiming line
ctx.beginPath();
ctx.moveTo(ball.x, ball.y);
ctx.lineTo(currentX, currentY);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
}
});
canvas.addEventListener('mouseup', (e) => {
if (isDragging) {
const rect = canvas.getBoundingClientRect();
const dragEndX = e.clientX - rect.left;
const dragEndY = e.clientY - rect.top;
// Calculate the force based on drag distance
const forceX = ball.x - dragEndX;
const forceY = ball.y - dragEndY;
// Scale the force
ball.vx = forceX * 0.1;
ball.vy = forceY * 0.1;
isDragging = false;
}
});
// Initialize the game
resetGame();
gameLoop();
</script>
</body>
</html>
HTML Structure:
<canvas> element where the game is rendered.<div> for game instructions.CSS Styling:
JavaScript Logic:
This is a basic implementation. Here are some ideas to enhance the game:
Feel free to modify and expand upon this code to create a more sophisticated mini golf game!