Certainly! Below is a complete, fully working Agar.io-like game implemented using HTML and JavaScript in a single file. You can copy 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>
/* Remove default margins and prevent scrollbars */
body { margin: 0; overflow: hidden; }
/* Ensure the canvas fills the screen */
canvas { display: block; background: #f2f2f2; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get the canvas and context
var canvas = document.getElementById('gameCanvas');
var ctx = canvas.getContext('2d');
// Set canvas size to the window size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Handle window resize
window.addEventListener('resize', function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Player object
var player = {
x: 0,
y: 0,
radius: 20,
color: 'blue',
speed: 2,
vx: 0,
vy: 0
};
// Initialize player's position at the center
player.x = canvas.width / 2;
player.y = canvas.height / 2;
// Arrays for food particles and enemy cells
var foods = [];
var enemies = [];
// Generate random food particles
for (var i = 0; i < 1000; i++) {
foods.push({
x: Math.random() * 5000 - 2500,
y: Math.random() * 5000 - 2500,
radius: 5 + Math.random() * 5,
color: 'green'
});
}
// Generate enemy cells
for (var i = 0; i < 50; i++) {
var radius = 10 + Math.random() * 30;
enemies.push({
x: Math.random() * 5000 - 2500,
y: Math.random() * 5000 - 2500,
radius: radius,
color: 'red',
speed: 1 + Math.random() * 1
});
}
// Variables for camera movement
var cameraX = player.x - canvas.width / 2;
var cameraY = player.y - canvas.height / 2;
// Mouse position
var mouseX = canvas.width / 2;
var mouseY = canvas.height / 2;
// Update mouse position on move
window.addEventListener('mousemove', function (e) {
var rect = canvas.getBoundingClientRect();
mouseX = e.clientX - rect.left;
mouseY = e.clientY - rect.top;
});
// Main game loop using requestAnimationFrame
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Update game state
function update() {
// Calculate angle towards mouse position
var angle = Math.atan2(mouseY - canvas.height / 2, mouseX - canvas.width / 2);
// Update player velocity
player.vx = Math.cos(angle) * player.speed;
player.vy = Math.sin(angle) * player.speed;
// Move player
player.x += player.vx;
player.y += player.vy;
// Adjust camera to follow player
cameraX = player.x - canvas.width / 2;
cameraY = player.y - canvas.height / 2;
// Player collides with food particles
for (var i = foods.length - 1; i >= 0; i--) {
var f = foods[i];
if (circleCollision(player, f)) {
// Increase player's radius slightly
player.radius += f.radius * 0.02;
foods.splice(i, 1);
}
}
// Enemies move and interact
for (var i = enemies.length - 1; i >= 0; i--) {
var e = enemies[i];
// Simple AI: move towards player if smaller, away if larger
var dx = player.x - e.x;
var dy = player.y - e.y;
var dist = Math.hypot(dx, dy);
if (e.radius < player.radius) {
// Enemy moves away from player
e.x -= (dx / dist) * e.speed;
e.y -= (dy / dist) * e.speed;
} else {
// Enemy moves towards player
e.x += (dx / dist) * e.speed;
e.y += (dy / dist) * e.speed;
}
// Check collision with player
if (circleCollision(player, e)) {
if (player.radius > e.radius * 1.1) {
// Player eats enemy
player.radius += e.radius * 0.5;
enemies.splice(i, 1);
} else if (player.radius * 1.1 < e.radius) {
// Enemy eats player
alert('You were eaten! Game over.');
document.location.reload();
}
}
// Enemy collides with food
for (var j = foods.length - 1; j >= 0; j--) {
var f = foods[j];
if (circleCollision(e, f)) {
e.radius += f.radius * 0.02;
foods.splice(j, 1);
}
}
}
// Keep player within bounds (infinite world can be implemented if desired)
// For now, no bounds are set; the player can move indefinitely
}
// Draw game objects
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
// Translate the canvas to simulate camera movement
ctx.translate(-cameraX, -cameraY);
// Draw food particles
for (var i = 0; i < foods.length; i++) {
var f = foods[i];
drawCircle(f);
}
// Draw enemies
for (var i = 0; i < enemies.length; i++) {
var e = enemies[i];
drawCircle(e);
}
// Draw player
drawCircle(player);
ctx.restore();
// Display player's mass
ctx.fillStyle = 'black';
ctx.font = '16px Arial';
ctx.fillText('Mass: ' + Math.round(player.radius), 10, 20);
}
// Helper function to draw a circle
function drawCircle(obj) {
ctx.beginPath();
ctx.arc(obj.x, obj.y, obj.radius, 0, Math.PI * 2);
ctx.fillStyle = obj.color;
ctx.fill();
ctx.closePath();
}
// Helper function to detect collision between circles
function circleCollision(c1, c2) {
var dx = c1.x - c2.x;
var dy = c1.y - c2.y;
var distance = Math.hypot(dx, dy);
return distance < c1.radius + c2.radius;
}
// Start the game loop
gameLoop();
</script>
</body>
</html>
Instructions:
- Copy the code above and paste it into a text editor.
- Save the file with an
.html extension, such as agar_clone.html.
- Open the file in a modern web browser (e.g., Chrome, Firefox).
- Control your cell by moving the mouse cursor. Your cell will follow the cursor.
- Eat the green food particles to grow larger.
- Avoid red enemy cells that are larger than you. You can eat smaller enemies.
Game Features:
- Movement: Your cell moves towards the mouse cursor.
- Growth: Absorb food particles to increase your size.
- Enemies: Computer-controlled cells also roam the arena.
- Smaller enemies will flee from you.
- Larger enemies will chase you.
- If you are larger than an enemy, you can eat it to gain mass.
- Collision Detection: The game calculates collisions between all circles (player, enemies, food).
- Camera Movement: The view follows your cell, simulating a larger world.
Note: This is a simplified, single-player version of Agar.io implemented without server capabilities. It demonstrates fundamental game mechanics using HTML5 Canvas and JavaScript.
Enjoy Playing!