<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sensible Soccer Clone</title>
<style>
body { margin: 0; background: #000; color: #fff; font-family: Arial, sans-serif; }
canvas { display: block; margin: 0 auto; background: #228B22; } /* Green pitch */
#overlay { position: absolute; top: 10px; left: 10px; z-index: 10; }
#commentary { position: absolute; bottom: 10px; left: 10px; z-index: 10; font-size: 20px; color: yellow; }
#score { font-size: 24px; }
#timer { font-size: 18px; }
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
<div id="overlay">
<div id="score">Home 0 - 0 Away</div>
<div id="timer">00:00</div>
</div>
<div id="commentary"></div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
// Game constants
const FIELD_WIDTH = 1200; // Larger than canvas for scrolling
const FIELD_HEIGHT = 800;
const GOAL_WIDTH = 100;
const GOAL_HEIGHT = 20;
const PLAYER_RADIUS = 10;
const BALL_RADIUS = 5;
const MAX_SPEED = 5;
const ACCEL = 0.2;
const FRICTION = 0.98;
const MATCH_TIME = 90 * 60; // 90 minutes in seconds
// Weather (random)
const weather = Math.random() > 0.5 ? 'rain' : 'clear';
const SLIPPERY_FACTOR = weather === 'rain' ? 0.95 : 0.98; // Less friction in rain
// Teams
class Team {
constructor(name, color, isHome) {
this.name = name;
this.color = color;
this.isHome = isHome;
this.score = 0;
this.players = [];
this.formation = this.randomFormation();
this.generatePlayers();
}
randomFormation() {
const formations = ['4-4-2', '4-3-3', '3-5-2'];
return formations[Math.floor(Math.random() * formations.length)];
}
generatePlayers() {
for (let i = 0; i < 11; i++) {
const attrs = {
speed: 3 + Math.random() * 2,
strength: 3 + Math.random() * 2,
stamina: 80 + Math.random() * 20
};
this.players.push(new Player(this, i === 0 ? 'GK' : `P${i}`, attrs)); // First is GK
}
}
}
class Player {
constructor(team, name, attrs) {
this.team = team;
this.name = name;
this.attrs = attrs;
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
this.hasBall = false;
this.fatigue = 0;
this.state = 'idle';
}
update() {
this.vx *= SLIPPERY_FACTOR * FRICTION;
this.vy *= SLIPPERY_FACTOR * FRICTION;
this.x += this.vx;
this.y += this.vy;
this.fatigue += 0.01; // Simple fatigue
if (this.fatigue > 100) this.attrs.speed *= 0.8; // Slow down
// Boundaries
this.x = Math.max(PLAYER_RADIUS, Math.min(FIELD_WIDTH - PLAYER_RADIUS, this.x));
this.y = Math.max(PLAYER_RADIUS, Math.min(FIELD_HEIGHT - PLAYER_RADIUS, this.y));
}
draw() {
ctx.fillStyle = this.team.color;
ctx.beginPath();
ctx.arc(this.x - camera.x, this.y - camera.y, PLAYER_RADIUS, 0, Math.PI * 2);
ctx.fill();
if (this === controlledPlayer) {
ctx.strokeStyle = '#fff';
ctx.stroke();
}
}
}
class Ball {
constructor() {
this.x = FIELD_WIDTH / 2;
this.y = FIELD_HEIGHT / 2;
this.vx = 0;
this.vy = 0;
this.spin = 0; // For curve
}
update() {
this.vx *= SLIPPERY_FACTOR * FRICTION;
this.vy *= SLIPPERY_FACTOR * FRICTION;
this.x += this.vx + this.spin * 0.1; // Simple curve
this.y += this.vy;
// Bounce off boundaries
if (this.x < BALL_RADIUS || this.x > FIELD_WIDTH - BALL_RADIUS) this.vx *= -0.8;
if (this.y < BALL_RADIUS || this.y > FIELD_HEIGHT - BALL_RADIUS) this.vy *= -0.8;
this.x = Math.max(BALL_RADIUS, Math.min(FIELD_WIDTH - BALL_RADIUS, this.x));
this.y = Math.max(BALL_RADIUS, Math.min(FIELD_HEIGHT - BALL_RADIUS, this.y));
}
draw() {
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.arc(this.x - camera.x, this.y - camera.y, BALL_RADIUS, 0, Math.PI * 2);
ctx.fill();
}
}
// Game objects
const homeTeam = new Team('Home', '#f00', true);
const awayTeam = new Team('Away', '#00f', false);
const ball = new Ball();
let controlledPlayer = homeTeam.players[1]; // Start with a player
let camera = { x: 0, y: 0 };
let time = 0;
let gameState = 'kickoff';
let commentaryText = '';
let commentaryTimer = 0;
// Position players in formation (simplified)
function positionPlayers() {
const positions = { // Example for 4-4-2
home: [
{x: 100, y: FIELD_HEIGHT/2}, // GK
{x: 200, y: 100}, {x: 200, y: 300}, {x: 200, y: 500}, {x: 200, y: 700}, // Def
{x: 500, y: 100}, {x: 500, y: 300}, {x: 500, y: 500}, {x: 500, y: 700}, // Mid
{x: 800, y: 300}, {x: 800, y: 500} // Fwd
],
away: positions.home.map(p => ({x: FIELD_WIDTH - p.x, y: p.y})) // Mirror
};
homeTeam.players.forEach((p, i) => { p.x = positions.home[i].x; p.y = positions.home[i].y; });
awayTeam.players.forEach((p, i) => { p.x = positions.away[i].x; p.y = positions.away[i].y; });
}
positionPlayers();
// Input
const keys = {};
window.addEventListener('keydown', e => keys[e.key] = true);
window.addEventListener('keyup', e => keys[e.key] = false);
// Game loop
function loop() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
// Update camera to follow ball
camera.x = Math.max(0, Math.min(FIELD_WIDTH - WIDTH, ball.x - WIDTH / 2));
camera.y = Math.max(0, Math.min(FIELD_HEIGHT - HEIGHT, ball.y - HEIGHT / 2));
// Draw field
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.strokeRect(0 - camera.x, 0 - camera.y, FIELD_WIDTH, FIELD_HEIGHT);
// Center circle
ctx.beginPath();
ctx.arc(FIELD_WIDTH/2 - camera.x, FIELD_HEIGHT/2 - camera.y, 100, 0, Math.PI*2);
ctx.stroke();
// Goals
ctx.strokeRect(0 - camera.x, (FIELD_HEIGHT - GOAL_WIDTH)/2 - camera.y, GOAL_HEIGHT, GOAL_WIDTH);
ctx.strokeRect(FIELD_WIDTH - GOAL_HEIGHT - camera.x, (FIELD_HEIGHT - GOAL_WIDTH)/2 - camera.y, GOAL_HEIGHT, GOAL_WIDTH);
// Update and draw ball
ball.update();
ball.draw();
// Update players
[homeTeam, awayTeam].forEach(team => {
team.players.forEach(p => {
p.update();
p.draw();
});
});
// Player control
if (controlledPlayer) {
let dx = 0, dy = 0;
if (keys['ArrowUp']) dy = -controlledPlayer.attrs.speed;
if (keys['ArrowDown']) dy = controlledPlayer.attrs.speed;
if (keys['ArrowLeft']) dx = -controlledPlayer.attrs.speed;
if (keys['ArrowRight']) dx = controlledPlayer.attrs.speed;
controlledPlayer.vx += dx * ACCEL;
controlledPlayer.vy += dy * ACCEL;
// Actions
if (keys['s'] && controlledPlayer.hasBall) { // Shoot
ball.vx = controlledPlayer.vx * 2 + (Math.random() - 0.5);
ball.vy = controlledPlayer.vy * 2 + (Math.random() - 0.5);
ball.spin = (Math.random() - 0.5) * 0.5; // Random curve
controlledPlayer.hasBall = false;
commentary('Shot!');
}
if (keys['p'] && controlledPlayer.hasBall) { // Pass
// Find nearest teammate
let nearest = findNearestPlayer(controlledPlayer, homeTeam.players.filter(p => p !== controlledPlayer));
if (nearest) {
let dist = Math.hypot(nearest.x - controlledPlayer.x, nearest.y - controlledPlayer.y);
ball.vx = (nearest.x - controlledPlayer.x) / dist * 5;
ball.vy = (nearest.y - controlledPlayer.y) / dist * 5;
controlledPlayer.hasBall = false;
commentary('Pass!');
}
}
if (keys['t']) { // Tackle
let opponent = findNearestPlayer(controlledPlayer, awayTeam.players, 20);
if (opponent && opponent.hasBall && Math.random() < 0.7) { // 70% success
opponent.hasBall = false;
controlledPlayer.hasBall = true;
commentary('Tackle!');
} else {
commentary('Missed tackle!');
}
}
if (keys[' '] && !controlledPlayer.hasBall) { // Switch player
controlledPlayer = findNearestPlayer(ball, homeTeam.players);
}
}
// Ball possession
[homeTeam, awayTeam].forEach(team => {
team.players.forEach(p => {
if (!p.hasBall && Math.hypot(p.x - ball.x, p.y - ball.y) < PLAYER_RADIUS + BALL_RADIUS) {
p.hasBall = true;
ball.vx = p.vx;
ball.vy = p.vy;
// Stick ball to player
ball.x = p.x;
ball.y = p.y;
}
});
});
// AI
aiUpdate(homeTeam, awayTeam);
aiUpdate(awayTeam, homeTeam);
// Goal detection
if (ball.y > (FIELD_HEIGHT - GOAL_WIDTH)/2 && ball.y < (FIELD_HEIGHT + GOAL_WIDTH)/2) {
if (ball.x < GOAL_HEIGHT) {
awayTeam.score++;
commentary('Goal for Away! Celebration: 🎉');
resetForKickoff();
} else if (ball.x > FIELD_WIDTH - GOAL_HEIGHT) {
homeTeam.score++;
commentary('Goal for Home! Celebration: 🎉');
resetForKickoff();
}
}
// Set pieces (simplified)
if (ball.x <= 0 || ball.x >= FIELD_WIDTH) { // Throw-in
ball.x = ball.x <= 0 ? 10 : FIELD_WIDTH - 10;
commentary('Throw-in!');
}
if (ball.y <= 0 || ball.y >= FIELD_HEIGHT) { // Corner/goal kick
if (ball.x < FIELD_WIDTH/2) {
commentary('Corner for Away!');
} else {
commentary('Corner for Home!');
}
ball.y = ball.y <= 0 ? 10 : FIELD_HEIGHT - 10;
}
// Offside (simplified: if forward is ahead of last defender)
homeTeam.players.slice(9).forEach(fwd => { // Forwards
let lastDef = Math.max(...awayTeam.players.map(p => p.x));
if (fwd.x > lastDef && fwd.hasBall) {
commentary('Offside!');
resetForKickoff(); // Free kick, but simplified
}
});
// Timer
time++;
if (time >= MATCH_TIME) gameState = 'ended';
document.getElementById('timer').innerText = `${Math.floor(time/60)}:${(time%60).toString().padStart(2,'0')}`;
document.getElementById('score').innerText = `Home ${homeTeam.score} - ${awayTeam.score} Away`;
// Commentary
if (commentaryTimer > 0) {
document.getElementById('commentary').innerText = commentaryText;
commentaryTimer--;
} else {
document.getElementById('commentary').innerText = '';
}
// Weather effect
if (weather === 'rain') {
ctx.fillStyle = 'rgba(0,0,255,0.1)';
for (let i = 0; i < 50; i++) {
ctx.fillRect(Math.random()*WIDTH, Math.random()*HEIGHT, 2, 10);
}
}
requestAnimationFrame(loop);
}
function aiUpdate(team, opponents) {
team.players.forEach(p => {
if (p === controlledPlayer) return;
// Simple AI: move towards ball if not in possession, else towards goal
let targetX = ball.x, targetY = ball.y;
if (p.hasBall) {
targetX = team.isHome ? FIELD_WIDTH : 0;
targetY = FIELD_HEIGHT / 2;
} else {
// Position in formation (drift towards position)
const formPos = team.isHome ? {x: p.x, y: p.y} : {x: FIELD_WIDTH - p.x, y: p.y}; // Placeholder
targetX = (targetX + formPos.x) / 2;
targetY = (targetY + formPos.y) / 2;
}
let dx = targetX - p.x;
let dy = targetY - p.y;
let dist = Math.hypot(dx, dy);
if (dist > 1) {
p.vx += (dx / dist) * p.attrs.speed * ACCEL * (Math.random() * 0.5 + 0.5); // Vary for realism
p.vy += (dy / dist) * p.attrs.speed * ACCEL * (Math.random() * 0.5 + 0.5);
}
// GK special
if (p.name === 'GK') {
// Dive towards ball if close
if (Math.hypot(p.x - ball.x, p.y - ball.y) < 50 && Math.abs(ball.vx) > 1) {
p.vx = ball.vx * 1.5;
p.vy = ball.vy * 1.5;
if (Math.random() < 0.8) { // Save chance
ball.vx *= -1;
ball.vy *= -1;
commentary('Great save!');
}
}
}
// Tackle if close to opponent with ball
let oppWithBall = opponents.players.find(op => op.hasBall);
if (oppWithBall && Math.hypot(p.x - oppWithBall.x, p.y - oppWithBall.y) < 20 && Math.random() < 0.5) {
oppWithBall.hasBall = false;
p.hasBall = true;
commentary('AI Tackle!');
}
// Shoot/pass if has ball
if (p.hasBall && Math.random() < 0.01) { // Random actions
if (Math.random() < 0.5) {
ball.vx = (team.isHome ? 10 : -10) + (Math.random() - 0.5) * 2;
ball.vy = (Math.random() - 0.5) * 2;
p.hasBall = false;
commentary('AI Shot!');
} else {
let nearest = findNearestPlayer(p, team.players.filter(tp => tp !== p));
if (nearest) {
let dist = Math.hypot(nearest.x - p.x, nearest.y - p.y);
ball.vx = (nearest.x - p.x) / dist * 5;
ball.vy = (nearest.y - p.y) / dist * 5;
p.hasBall = false;
commentary('AI Pass!');
}
}
}
});
}
function findNearestPlayer(target, players, maxDist = Infinity) {
let nearest = null;
let minDist = maxDist;
players.forEach(p => {
let dist = Math.hypot(p.x - target.x, p.y - target.y);
if (dist < minDist) {
minDist = dist;
nearest = p;
}
});
return nearest;
}
function resetForKickoff() {
ball.x = FIELD_WIDTH / 2;
ball.y = FIELD_HEIGHT / 2;
ball.vx = 0;
ball.vy = 0;
positionPlayers();
controlledPlayer = homeTeam.players[1];
[homeTeam, awayTeam].forEach(t => t.players.forEach(p => p.hasBall = false));
}
function commentary(text) {
commentaryText = text;
commentaryTimer = 120; // 2 seconds at 60fps
}
// Start game
commentary('Kick off! Weather: ' + weather);
loop();
</script>
</body>
</html>