<!DOCTYPE html>
<html>
<head>
<title>Sensible Soccer Clone</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #A0D2EB;
}
canvas {
display: block;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
background-color: rgba(255, 255, 255, 0.7);
padding: 10px;
border-radius: 5px;
}
#score {
font-size: 20px;
font-weight: bold;
}
#timer {
font-size: 16px;
}
#commentary {
font-style: italic;
margin-top: 10px;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="controls">
<div id="score">
Team A: <span id="teamAScore">0</span> - Team B: <span id="teamBScore">0</span>
</div>
<div id="timer">Time: 0:00</div>
<div id="commentary"></div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game Constants
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
const ballRadius = 10;
const playerWidth = 20;
const playerHeight = 40;
const fieldWidth = canvasWidth - 200;
const fieldHeight = canvasHeight - 100;
const ballSpeed = 5;
const playerSpeed = 3;
const tackleDistance = 15;
const shotPower = 10;
const passDistance = 20;
const friction = 0.05;
const gravity = 0.1;
const offsideThreshold = 30; // Simplified
// Game State
let ballX = canvasWidth / 2;
let ballY = canvasHeight / 2;
let ballXSpeed = ballSpeed;
let ballYSpeed = ballSpeed;
let player1X = 50;
let player1Y = canvasHeight / 2 - playerHeight / 2;
let player2X = canvasWidth - 50 - playerWidth;
let player2Y = canvasHeight / 2 - playerHeight / 2;
let player1Direction = 0; // 0: right, 1: left
let player2Direction = 0; // 0: right, 1: left
let teamAScore = 0;
let teamBScore = 0;
let gameTimer = 0;
let gameRunning = true;
let weather = 'clear'; // clear, rain
let commentaryText = "";
// Team Data (Randomly Generated)
function generateTeamData(team) {
return {
formation: ['forward', 'midfielder', 'defender'],
players: []
};
}
// Player Attributes (Randomly Generated)
function generatePlayerAttributes(playerIndex) {
return {
speed: Math.random() * 5 + 2,
strength: Math.random() * 5 + 2,
accuracy: Math.random() * 5 + 2
};
}
// Initialize Teams
let teamA = generateTeamData('A');
let teamB = generateTeamData('B');
for (let i = 0; i < 4; i++) {
teamA.players.push({
id: i,
x: 50 + i * 30,
y: canvasHeight / 2 - playerHeight / 2,
attributes: generatePlayerAttributes(i)
});
teamB.players.push({
id: i,
x: canvasWidth - 50 - i * 30,
y: canvasHeight / 2 - playerHeight / 2,
attributes: generatePlayerAttributes(i)
});
}
// Player Movement
function updatePlayer(player, direction) {
player.x += direction * player.attributes.speed;
if (player.x < 0) player.x = 0;
if (player.x > canvasWidth - playerWidth) player.x = canvasWidth - playerWidth;
}
// Ball Movement
function updateBall() {
ballX += ballXSpeed;
ballY += ballYSpeed;
// Bounce off walls
if (ballX < 0 || ballX > canvasWidth - ballRadius) {
ballXSpeed = -ballXSpeed;
}
if (ballY < 0 || ballY > canvasHeight - ballRadius) {
ballYSpeed = -ballYSpeed;
}
// Gravity
ballYSpeed += gravity;
// Collision with players
for (let i = 0; i < teamA.players.length; i++) {
const player = teamA.players[i];
const dx = ballX - player.x;
const dy = ballY - player.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < ballRadius + playerWidth) {
// Simple bounce
const normalX = dx / distance;
const normalY = dy / distance;
ballXSpeed = ballXSpeed - normalX * ballSpeed;
ballYSpeed = ballYSpeed - normalY * ballSpeed;
}
}
for (let i = 0; i < teamB.players.length; i++) {
const player = teamB.players[i];
const dx = ballX - player.x;
const dy = ballY - player.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < ballRadius + playerWidth) {
const normalX = dx / distance;
const normalY = dy / distance;
ballXSpeed = ballXSpeed - normalX * ballSpeed;
ballYSpeed = ballYSpeed - normalY * ballSpeed;
}
}
}
// Shooting
function shoot() {
if (gameRunning) {
const angle = Math.atan2(ballY - canvasHeight / 2, ballX - canvasWidth / 2);
const power = Math.sqrt(shotPower * shotPower);
ballXSpeed = power * Math.cos(angle);
ballYSpeed = power * Math.sin(angle);
commentaryText = "Shot!";
}
}
// Passing
function pass() {
if (gameRunning) {
const targetPlayer = findNearestPlayer(ballX, ballY);
if (targetPlayer) {
const dx = targetPlayer.x - ballX;
const dy = targetPlayer.y - ballY;
const distance = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx);
const passPower = Math.sqrt(passDistance * passDistance);
ballXSpeed = passPower * Math.cos(angle);
ballYSpeed = passPower * Math.sin(angle);
commentaryText = "Pass!";
}
}
}
// Tackle
function tackle() {
if (gameRunning) {
const targetPlayer = findNearestPlayer(ballX, ballY);
if (targetPlayer && Math.abs(targetPlayer.x - ballX) < tackleDistance && Math.abs(targetPlayer.y - ballY) < tackleDistance) {
// Simple success
commentaryText = "Tackle!";
// Reset player position
targetPlayer.x = canvasWidth - 50 - targetPlayer.id * 30;
targetPlayer.y = canvasHeight / 2 - playerHeight / 2;
}
}
}
// Find Nearest Player
function findNearestPlayer(x, y) {
let nearestPlayer = null;
let minDistance = Infinity;
for (const player of teamA.players) {
const dx = x - player.x;
const dy = y - player.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < minDistance) {
minDistance = distance;
nearestPlayer = player;
}
}
for (const player of teamB.players) {
const dx = x - player.x;
const dy = y - player.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < minDistance) {
minDistance = distance;
nearestPlayer = player;
}