Sure! Below is a complete HTML file that implements a simple version of the Agar.io game using HTML and JavaScript. You can copy and paste this code into a file with an .html extension and open it in your web browser to play the game.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Agar.io Clone</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; background: #f0f0f0; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Player object
const player = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 20,
color: 'blue',
speed: 2,
score: 0
};
// Food particles
const foods = [];
const foodCount = 100;
// Generate food particles
function generateFoods() {
for (let i = 0; i < foodCount; i++) {
foods.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: 5,
color: 'green'
});
}
}
generateFoods();
// Mouse position
const mouse = {
x: canvas.width / 2,
y: canvas.height / 2
};
// Event listener for mouse movement
canvas.addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
});
// Update game objects
function update() {
// Calculate angle towards mouse
const dx = mouse.x - player.x;
const dy = mouse.y - player.y;
const angle = Math.atan2(dy, dx);
// Move player towards mouse
const distance = Math.hypot(dx, dy);
if (distance > 1) {
player.x += Math.cos(angle) * player.speed;
player.y += Math.sin(angle) * player.speed;
}
// Check collision with food
for (let i = foods.length - 1; i >= 0; i--) {
const food = foods[i];
const dist = Math.hypot(player.x - food.x, player.y - food.y);
if (dist < player.radius + food.radius) {
// Increase player size and score
player.radius += 0.5;
player.score += 1;
// Remove food
foods.splice(i, 1);
}
}
// Regenerate food if all are eaten
if (foods.length === 0) {
generateFoods();
}
}
// Draw game objects
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw food
for (let food of foods) {
ctx.beginPath();
ctx.arc(food.x, food.y, food.radius, 0, Math.PI * 2);
ctx.fillStyle = food.color;
ctx.fill();
}
// Draw player
ctx.beginPath();
ctx.arc(player.x, player.y, player.radius, 0, Math.PI * 2);
ctx.fillStyle = player.color;
ctx.fill();
// Draw score
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText('Score: ' + player.score, 10, 30);
}
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();
// Handle window resize
window.addEventListener('resize', function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html>
Instructions:
-
Save the Code:
- Copy the entire code above.
- Paste it into a text editor.
- Save the file with an
.html extension (e.g., agario_clone.html).
-
Run the Game:
- Open the saved HTML file in a modern web browser (e.g., Chrome, Firefox).
-
How to Play:
- Move your mouse to control the blue cell.
- Collect the green food particles to grow larger.
- Your score increases with each food particle you consume.
Features:
- Player Control: The player cell follows the mouse cursor smoothly.
- Food Generation: 100 food particles are randomly placed on the canvas. Once all are consumed, they regenerate.
- Growth Mechanism: The player's cell grows slightly larger with each food particle consumed.
- Score Display: The current score is displayed in the top-left corner.
Note:
- This is a simplified, single-player version of Agar.io.
- It doesn't include other players or enemies.
- For a full multiplayer experience, additional server-side programming and networking would be required, which isn't covered in this single-file implementation.
Enjoy the Game!