o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
<!DOCTYPE html> <html> <head> <title>Top-Down Boat Game</title> <style> body { margin:…
- Usercode
- Assistant
// Adjust canvas size when the window is resized
window.addEventListener('resize', resizeCanvas);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
// Timing variables for game loop
let lastTime = 0;
function gameLoop(timestamp) {
const deltaTime = (timestamp - lastTime) / 1000; // Convert to seconds
lastTime = timestamp;
update(deltaTime);
draw();
requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
// Boat object representing the player's boat
const boat = {
x: canvas.width / 2,
y: canvas.height / 2,
angle: 0,
speed: 0,
maxSpeed: 200, // pixels per second
acceleration: 200, // pixels per second squared
rotationSpeed: Math.PI, // radians per second (180 degrees per second)
width: 40,
height: 20
};
// Key press tracking
const keys = {};
window.addEventListener('keydown', function (e) {
keys[e.code] = true;
});
window.addEventListener('keyup', function (e) {
keys[e.code] = false;
});
// Water object for simulation parameters
const water = {
waveAmplitude: 20,
waveLength: 100,
waveSpeed: 0.2, // waves per second
time: 0,
};
// Update function to handle game logic
function update(deltaTime) {
// Boat rotation
if (keys['ArrowLeft']) {
boat.angle -= boat.rotationSpeed * deltaTime;
}
if (keys['ArrowRight']) {
boat.angle += boat.rotationSpeed * deltaTime;
}
// Boat acceleration
if (keys['ArrowUp']) {
boat.speed += boat.acceleration * deltaTime;
} else if (keys['ArrowDown']) {
boat.speed -= boat.acceleration * deltaTime;
} else {
// Apply friction
boat.speed *= 0.98;
}
// Limit speed
if (boat.speed > boat.maxSpeed) boat.speed = boat.maxSpeed;
if (boat.speed < -boat.maxSpeed / 2) boat.speed = -boat.maxSpeed / 2;
// Update boat position
boat.x += Math.cos(boat.angle) * boat.speed * deltaTime;
boat.y += Math.sin(boat.angle) * boat.speed * deltaTime;
// Keep boat within canvas bounds
if (boat.x < 0) boat.x = canvas.width;
if (boat.x > canvas.width) boat.x = 0;
if (boat.y < 0) boat.y = canvas.height;
if (boat.y > canvas.height) boat.y = 0;
// Update water simulation
updateWater(deltaTime);
// Get water current at boat's position
const sinFactor = Math.sin((boat.y / water.waveLength + water.time) * Math.PI * 2);
const waterCurrent = sinFactor * 50; // pixels per second
// Apply water current to boat's x position
boat.x += waterCurrent * deltaTime;
}
// Draw function to render the game
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw water
drawWater();
// Boat bobbing effect
const bobbing = Math.sin((boat.x / water.waveLength + water.time) * Math.PI * 2) * 5;
// Draw boat
ctx.save();
ctx.translate(boat.x, boat.y + bobbing);
ctx.rotate(boat.angle);
ctx.fillStyle = 'brown';
ctx.fillRect(-boat.width / 2, -boat.height / 2, boat.width, boat.height);
ctx.restore();
}
// Update the water simulation time
function updateWater(deltaTime) {
water.time += deltaTime * water.waveSpeed;
}
// Draw the water with a wave effect
function drawWater() {
// Create a gradient for the water
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, '#1E90FF');
gradient.addColorStop(1, '#000080');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Overlay wave effect
ctx.fillStyle = 'rgba(255, 255, 255, 0.1)';
for (let i = 0; i < 10; i++) {
ctx.beginPath();
const waveOffset = (water.time + i * 0.5) * 50;
ctx.moveTo(0, Math.sin(water.time + i) * 10 + waveOffset);
ctx.bezierCurveTo(
canvas.width / 4, Math.sin(water.time + i + 1) * 10 + waveOffset,
canvas.width / 2, Math.sin(water.time + i + 2) * 10 + waveOffset,
canvas.width, Math.sin(water.time + i + 3) * 10 + waveOffset
);
ctx.lineTo(canvas.width, canvas.height);
ctx.lineTo(0, canvas.height);
ctx.closePath();
ctx.fill();
}
}
</script>
Certainly! To adapt the game for mobile devices and enhance the UI, we'll implement touch controls and improve the visual aesthetics of the game. Below is the updated code with these enhancements:
<!DOCTYPE html>
<html>
<head>
<title>Top-Down Boat Game</title>
<style>
body { margin: 0; overflow: hidden; font-family: Arial, sans-serif; }
canvas { display: block; background: #87CEEB; }
#controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 20px;
user-select: none;
}
.button {
width: 60px;
height: 60px;
background: rgba(255, 255, 255, 0.7);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
}
.button:active {
background: rgba(255, 255, 255, 0.9);
}
#score {
position: absolute;
top: 20px;
left: 20px;
color: white;
font-size: 24px;
text-shadow: 1px 1px 2px black;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="controls">
<div id="leftButton" class="button">◀</div>
<div id="upButton" class="button">▲</div>
<div id="rightButton" class="button">▶</div>
</div>
<div id="score">Score: 0</div>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
resizeCanvas();
// Adjust canvas size when the window is resized
window.addEventListener('resize', resizeCanvas);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Update boat position to stay in view
boat.x = canvas.width / 2;
boat.y = canvas.height / 2;
}
// Timing variables for game loop
let lastTime = 0;
function gameLoop(timestamp) {
const deltaTime = (timestamp - lastTime) / 1000; // Convert to seconds
lastTime = timestamp;
update(deltaTime);
draw();
requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
// Boat image
const boatImage = new Image();
boatImage.src = 'boat.png'; // Ensure you have a 'boat.png' image in the same directory
// Boat object representing the player's boat
const boat = {
x: canvas.width / 2,
y: canvas.height / 2,
angle: 0,
speed: 0,
maxSpeed: 200, // pixels per second
acceleration: 200, // pixels per second squared
rotationSpeed: Math.PI, // radians per second (180 degrees per second)
width: 60,
height: 30
};
// Key and touch press tracking
const keys = {};
window.addEventListener('keydown', function (e) {
keys[e.code] = true;
});
window.addEventListener('keyup', function (e) {
keys[e.code] = false;
});
// Touch controls
const leftButton = document.getElementById('leftButton');
const upButton = document.getElementById('upButton');
const rightButton = document.getElementById('rightButton');
leftButton.addEventListener('touchstart', () => keys['ArrowLeft'] = true);
leftButton.addEventListener('touchend', () => keys['ArrowLeft'] = false);
upButton.addEventListener('touchstart', () => keys['ArrowUp'] = true);
upButton.addEventListener('touchend', () => keys['ArrowUp'] = false);
rightButton.addEventListener('touchstart', () => keys['ArrowRight'] = true);
rightButton.addEventListener('touchend', () => keys['ArrowRight'] = false);
// Water object for simulation parameters
const water = {
waveAmplitude: 20,
waveLength: 100,
waveSpeed: 0.2, // waves per second
time: 0,
};
// Score
let score = 0;
const scoreDisplay = document.getElementById('score');
// Update function to handle game logic
function update(deltaTime) {
// Boat rotation
if (keys['ArrowLeft']) {
boat.angle -= boat.rotationSpeed * deltaTime;
}
if (keys['ArrowRight']) {
boat.angle += boat.rotationSpeed * deltaTime;
}
// Boat acceleration
if (keys['ArrowUp']) {
boat.speed += boat.acceleration * deltaTime;
} else {
// Apply friction
boat.speed *= 0.98;
}
// Limit speed
boat.speed = Math.min(boat.speed, boat.maxSpeed);
// Update boat position
boat.x += Math.cos(boat.angle) * boat.speed * deltaTime;
boat.y += Math.sin(boat.angle) * boat.speed * deltaTime;
// Keep boat within canvas bounds
if (boat.x < -boat.width) boat.x = canvas.width + boat.width;
if (boat.x > canvas.width + boat.width) boat.x = -boat.width;
if (boat.y < -boat.height) boat.y = canvas.height + boat.height;
if (boat.y > canvas.height + boat.height) boat.y = -boat.height;
// Update water simulation
updateWater(deltaTime);
// Get water current at boat's position
const sinFactor = Math.sin((boat.y / water.waveLength + water.time) * Math.PI * 2);
const waterCurrent = sinFactor * 50; // pixels per second
// Apply water current to boat's x position
boat.x += waterCurrent * deltaTime;
// Update score based on distance traveled
score += Math.abs(boat.speed) * deltaTime * 0.1;
scoreDisplay.textContent = 'Score: ' + Math.floor(score);
}
// Draw function to render the game
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw water
drawWater();
// Boat bobbing effect
const bobbing = Math.sin((boat.x / water.waveLength + water.time) * Math.PI * 2) * 5;
// Draw boat
ctx.save();
ctx.translate(boat.x, boat.y + bobbing);
ctx.rotate(boat.angle);
ctx.drawImage(boatImage, -boat.width / 2, -boat.height / 2, boat.width, boat.height);
ctx.restore();
// Add some floating particles (e.g., bubbles)
drawParticles();
}
// Update the water simulation time
function updateWater(deltaTime) {
water.time += deltaTime * water.waveSpeed;
}
// Draw the water with a wave effect
function drawWater() {
// Create a gradient for the water
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, '#1E90FF');
gradient.addColorStop(0.5, '#00BFFF');
gradient.addColorStop(1, '#1E90FF');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw waves
ctx.fillStyle = 'rgba(255, 255, 255, 0.1)';
for (let i = 0; i < 5; i++) {
ctx.beginPath();
const waveOffset = (water.time + i * 0.5) * 50;
ctx.moveTo(0, Math.sin(water.time + i) * 10 + waveOffset);
for (let x = 0; x <= canvas.width; x += 50) {
ctx.lineTo(x, Math.sin((x / 50) + water.time + i) * 10 + waveOffset);
}
ctx.lineTo(canvas.width, canvas.height);
ctx.lineTo(0, canvas.height);
ctx.closePath();
ctx.fill();
}
}
// Draw some particles like bubbles
const particles = [];
function drawParticles() {
// Create new particles
if (Math.random() < 0.1) {
particles.push({
x: Math.random() * canvas.width,
y: canvas.height + 10,
speed: Math.random() * 20 + 10
});
}
// Update and draw particles
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.y -= p.speed * (1 / 60);
ctx.beginPath();
ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
ctx.fill();
if (p.y < -10) {
particles.splice(i, 1);
}
}
}
</script>
</body>
</html>
Mobile Touch Controls:
leftButton, upButton, rightButton) for rotation and acceleration.touchstart and touchend events to control the boat using touch inputs, emulating key presses.Enhanced UI:
boat.png image in the same directory or adjust the src path accordingly.score element to display the current score on the screen, styled for better visibility.resizeCanvas function to reposition the boat when the screen size changes, ensuring it's always visible.Additional Visual Elements:
boat.png):To complete the visual enhancement, you need an image of a boat. You can use the following steps:
boat.png in the same directory as your HTML file..html file (e.g., boatGame.html).boat.png image in the same directory.Enjoy your enhanced boat game with mobile controls and an improved user interface!
Not yet included in a published release.