// Function to draw the game area, walls, and fish
function drawGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw walls
ctx.fillRect(0, 0, wallWidth, wallHeight);
ctx.fillRect(wallWidth + 1, 0, canvas.width - 2 * wallWidth, wallHeight);
ctx.fillRect(0, wallHeight, wallWidth, canvas.height - 2 * wallHeight);
ctx.fillRect(wallWidth + 1, canvas.height - 2 * wallHeight, canvas.width - 2 * wallWidth, 2 * wallHeight);
// Draw player
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.arc(playerX, playerY, 15, 0, Math.PI * 2);
ctx.fill();
// Draw red fish
redFishes.forEach(fish => {
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(fish.x, fish.y, 10, 0, Math.PI * 2);
ctx.fill();
});
// Update score and fish speed
document.getElementById('score').textContent = 'Score: ' + score;
if (score >= 10) {
fishSpeed += 1;
}
// Update player movement
if (window.event.type === 'mousemove') {
playerX = event.clientX - playerX;
if (playerX < 0 || playerX > canvas.width - 30) {
playerX = playerX < 0 ? 0 : playerX > canvas.width - 30 ? canvas.width - 30 : playerX;
}
}
// Update fish positions (random directions, faster as score increases)
redFishes.forEach(fish => {
fish.x += Math.random() * 5 - 2.5;
fish.y += Math.random() * 3 - 1.5;
if (fish.x < 0 || fish.x > canvas.width - 15 || fish.y < 0 || fish.y > canvas.height - 15) {
// Regenerate fish
generateNewFish();
}
});
}
// Function to create and update red fish
function generateNewFish() {
const newFishX = Math.random() * (canvas.width - 30);
const newFishY = Math.random() * (canvas.height - 30);
const newFish = {
x: newFishX,
y: newFishY,
speed: Math.max(1, fishSpeed),
};
redFishes.push(newFish);
}
// Event listeners
canvas.addEventListener('mousemove', drawGame);
canvas.addEventListener('keydown', event => {
if (event.keyCode === 37) { // Left arrow
playerX -= playerSpeed;
} else if (event.keyCode === 38) { // Up arrow
playerY -= playerSpeed;
} else if (event.keyCode === 39) { // Right arrow
playerX += playerSpeed;
} else if (event.keyCode === 40) { // Down arrow
playerY += playerSpeed;
}
});
// Start the game
drawGame();
</script>