Sure! Here is the entire code for the 2D game you described, written in HTML and JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Yellow Fish</title>
<style>
#game-container {
width: 600px;
height: 600px;
border: 1px solid black;
position: relative;
}
#game-canvas {
width: 100%;
height: 100%;
position: absolute;
}
#yellow-fish {
position: absolute;
left: 50px;
top: 50px;
width: 50px;
height: 50px;
background-color: yellow;
border-radius: 50%;
z-index: 10;
}
#red-fish {
position: absolute;
left: 50px + (Math.random() * 100) - 50;
top: 50px + (Math.random() * 100) - 50;
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
z-index: 5;
}
</style>
</head>
<body onkeydown="moveYellowFish(event)">
<div id="game-container">
<canvas id="game-canvas" width="600" height="600"></canvas>
</div>
<script>
const canvas = document.getElementById("game-canvas");
const ctx = canvas.getContext("2d");
const yellowFish = { x: 50, y: 50 };
const redFish = [];
let score = 0;
function moveYellowFish(event) {
const dx = event.clientX - yellowFish.x;
const dy = event.clientY - yellowFish.y;
yellowFish.x += dx;
yellowFish.y += dy;
}
function generateRedFish() {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const speed = Math.random() * 2 + 1;
redFish.push({ x, y, speed });
}
function updateRedFish() {
redFish.forEach((fish) => {
fish.y += fish.speed;
if (fish.y > canvas.height) {
fish.y = 0;
fish.x = Math.random() * canvas.width;
}
});
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "yellow";
ctx.fillRect(yellowFish.x, yellowFish.y, 50, 50);
redFish.forEach((fish) => {
ctx.fillStyle = "red";
ctx.fillRect(fish.x, fish.y, 20, 20);
});
ctx.font = "24px Arial";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.fillText(`Score: ${score}`, canvas.width / 2, 20);
updateRedFish();
yellowFish.x += 2;
}
function gameLoop() {
requestAnimationFrame(gameLoop);
draw();
}
gameLoop();
document.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowUp") {
yellowFish.y -= 10;
} else if (key === "ArrowDown") {
yellowFish.y += 10;
} else if (key === "ArrowLeft") {
yellowFish.x -= 10;
} else if (key === "ArrowRight") {
yellowFish.x += 10;
}
});
</script>
</body>
</html>
Here's a brief explanation of the code:
- The HTML code creates a simple game container and a canvas element inside it.
- The CSS code styles the game elements and sets up the game area.
- The JavaScript code defines the game objects, such as the yellow fish and the red fish, and sets up the game loop.
- The
moveYellowFish() function updates the position of the yellow fish based on user input.
- The
generateRedFish() function procedurally generates new red fish in the game area.
- The
updateRedFish() function updates the position of the red fish based on their speed.
- The
draw() function draws the game elements on the canvas.
- The
gameLoop() function runs the game loop and updates the game state.
- The
document.addEventListener("keydown") function listens for user input and updates the yellow fish's position accordingly.
Note that this is a basic implementation and there are many ways to improve it, such as adding collision detection, scoring, and game over logic.