Here is a simple implementation of an Agar.io-like game using HTML and JavaScript in a single file. This game allows you to control a cell with your mouse, eat smaller food particles to grow, and avoid enemy cells.
To play the game, simply copy the code below into a file named agario.html and open it in your web browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Agar.io Clone</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #f2fbff;
}
canvas {
display: block;
background-color: #f2fbff;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get the canvas and set its dimensions
const canvas = document.getElementById('gameCanvas');
const context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Player object
const player = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 15,
color: '#007bff',
speed: 3
};
// Arrays to hold food and enemy cells
const foods = [];
const enemies = [];
// Mouse position
const mouse = {
x: player.x,
y: player.y
};
// Generate food particles
function createFood(count) {
for (let i = 0; i < count; i++) {
foods.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: 5,
color: '#ff4757'
});
}
}
// Generate enemy cells
function createEnemies(count) {
for (let i = 0; i < count; i++) {
const radius = 10 + Math.random() * 20;
enemies.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: radius,
color: '#2ed573',
speed: 1 + Math.random() * 2,
direction: Math.random() * Math.PI * 2
});
}
}
// Update player position based on mouse movement
function updatePlayer() {
const dx = mouse.x - player.x;
const dy = mouse.y - player.y;
const distance = Math.hypot(dx, dy);
if (distance > 1) {
player.x += (dx / distance) * player.speed;
player.y += (dy / distance) * player.speed;
}
}
// Move enemies randomly
function updateEnemies() {
enemies.forEach(enemy => {
enemy.x += Math.cos(enemy.direction) * enemy.speed;
enemy.y += Math.sin(enemy.direction) * enemy.speed;
// Change direction if enemy hits the edge of the canvas
if (enemy.x - enemy.radius < 0 || enemy.x + enemy.radius > canvas.width) {
enemy.direction = Math.PI - enemy.direction;
}
if (enemy.y - enemy.radius < 0 || enemy.y + enemy.radius > canvas.height) {
enemy.direction = -enemy.direction;
}
});
}
// Check for collisions
function checkCollisions() {
// Check collision with food
for (let i = foods.length - 1; i >= 0; i--) {
const food = foods[i];
const dx = player.x - food.x;
const dy = player.y - food.y;
const distance = Math.hypot(dx, dy);
if (distance < player.radius + food.radius) {
// Increase player size
player.radius += 0.5;
// Remove food from array
foods.splice(i, 1);
}
}
// Check collision with enemies
for (let i = enemies.length - 1; i >= 0; i--) {
const enemy = enemies[i];
const dx = player.x - enemy.x;
const dy = player.y - enemy.y;
const distance = Math.hypot(dx, dy);
if (distance < player.radius + enemy.radius) {
if (player.radius > enemy.radius) {
// Player eats enemy
player.radius += enemy.radius * 0.2;
enemies.splice(i, 1);
} else {
// Game over
alert('Game Over!');
document.location.reload();
}
}
}
}
// Draw circle
function drawCircle(obj) {
context.beginPath();
context.arc(obj.x, obj.y, obj.radius, 0, Math.PI * 2);
context.fillStyle = obj.color;
context.fill();
context.closePath();
}
// Main game loop
function gameLoop() {
context.clearRect(0, 0, canvas.width, canvas.height);
updatePlayer();
updateEnemies();
checkCollisions();
// Draw foods
foods.forEach(food => drawCircle(food));
// Draw enemies
enemies.forEach(enemy => drawCircle(enemy));
// Draw player
drawCircle(player);
requestAnimationFrame(gameLoop);
}
// Event listener for mouse movement
window.addEventListener('mousemove', function (e) {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
// Adjust canvas size on window resize
window.addEventListener('resize', function () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Initialize the game
createFood(100);
createEnemies(10);
gameLoop();
</script>
</body>
</html>
How to Play:
- Control your cell by moving the mouse. The cell will follow the cursor.
- Eat the red food particles to grow bigger.
- Avoid green enemy cells that are bigger than you.
- Eat smaller enemy cells to grow even bigger.
- Survive and grow as big as possible!
Game Features:
- Dynamic Movement: The player's cell smoothly follows the mouse cursor.
- Growth Mechanism: Eating food particles and smaller enemy cells increases the player's size.
- Enemy AI: Enemy cells move randomly and can be eaten if they are smaller than the player.
- Collision Detection: Accurate detection for eating and being eaten.
Feel free to modify and enhance the game by adjusting parameters such as the number of food particles, enemy cells, and their behaviors. Enjoy playing!