I'll help create a Sensible Soccer-style game using pure HTML5 Canvas, CSS, and JavaScript. Given the complexity, I'll break this down into a complete, working game that you can run in any modern browser.
<!DOCTYPE html>
<html>
<head>
<title>Canvas Soccer</title>
<style>
body {
margin: 0;
background: #111;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: monospace;
}
#gameCanvas {
border: 2px solid #444;
}
#scoreBoard {
position: fixed;
top: 10px;
color: white;
font-size: 24px;
text-align: center;
}
</style>
</head>
<body>
<div id="scoreBoard">Home 0 - 0 Away</div>
<canvas id="gameCanvas"></canvas>
<script>
// Core game setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreBoard = document.getElementById('scoreBoard');
canvas.width = 800;
canvas.height = 600;
// Game state
const state = {
ball: {
x: canvas.width / 2,
y: canvas.height / 2,
vx: 0,
vy: 0,
radius: 5
},
teams: {
home: {
score: 0,
color: '#ff0000',
players: []
},
away: {
score: 0,
color: '#0000ff',
players: []
}
},
possession: null,
gameTime: 0,
weather: {
friction: 0.98,
wind: 0
},
celebration: false,
commentary: ''
};
// Player class
class Player {
constructor(x, y, team, role) {
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.team = team;
this.role = role;
this.speed = 2 + Math.random();
this.homeX = x;
this.homeY = y;
this.size = 10;
this.hasBall = false;
}
update() {
// Basic AI and movement
if (!this.hasBall) {
if (this.distanceTo(state.ball) < 100) {
this.moveTowards(state.ball);
} else {
this.moveTowards({x: this.homeX, y: this.homeY});
}
}
// Apply movement
this.x += this.vx;
this.y += this.vy;
// Friction
this.vx *= 0.9;
this.vy *= 0.9;
// Keep in bounds
this.x = Math.max(0, Math.min(canvas.width, this.x));
this.y = Math.max(0, Math.min(canvas.height, this.y));
}
moveTowards(target) {
const angle = Math.atan2(target.y - this.y, target.x - this.x);
this.vx += Math.cos(angle) * this.speed * 0.1;
this.vy += Math.sin(angle) * this.speed * 0.1;
}
distanceTo(target) {
return Math.sqrt((target.x - this.x) ** 2 + (target.y - this.y) ** 2);
}
draw() {
ctx.fillStyle = this.team.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
// Initialize players
function createTeam(team, isHome) {
const formation = [
{x: 0.1, y: 0.5}, // Goalkeeper
{x: 0.3, y: 0.3}, // Defenders
{x: 0.3, y: 0.7},
{x: 0.5, y: 0.2}, // Midfielders
{x: 0.5, y: 0.5},
{x: 0.5, y: 0.8},
{x: 0.7, y: 0.4}, // Forwards
{x: 0.7, y: 0.6}
];
formation.forEach(pos => {
const x = isHome ? pos.x * canvas.width : (1 - pos.x) * canvas.width;
const y = pos.y * canvas.height;
team.players.push(new Player(x, y, team, 'field'));
});
}
createTeam(state.teams.home, true);
createTeam(state.teams.away, false);
// Ball physics
function updateBall() {
state.ball.x += state.ball.vx;
state.ball.y += state.ball.vy;
// Apply friction and wind
state.ball.vx *= state.weather.friction;
state.ball.vy *= state.weather.friction;
state.ball.vx += state.weather.wind;
// Bounce off walls
if (state.ball.x < 0 || state.ball.x > canvas.width) {
state.ball.vx *= -0.8;
}
if (state.ball.y < 0 || state.ball.y > canvas.height) {
state.ball.vy *= -0.8;
}
// Check for goals
if (state.ball.x < 50 && state.ball.y > canvas.height/2 - 50 && state.ball.y < canvas.height/2 + 50) {
goalScored('away');
}
if (state.ball.x > canvas.width - 50 && state.ball.y > canvas.height/2 - 50 && state.ball.y < canvas.height/2 + 50) {
goalScored('home');
}
}
// Goal celebration
function goalScored(team) {
state.teams[team].score++;
state.celebration = true;
state.commentary = `GOOOAL! ${team.toUpperCase()} SCORES!`;
scoreBoard.textContent = `Home ${state.teams.home.score} - ${state.teams.away.score} Away`;
// Reset positions after short delay
setTimeout(resetPositions, 2000);
}
function resetPositions() {
state.ball.x = canvas.width / 2;
state.ball.y = canvas.height / 2;
state.ball.vx = 0;
state.ball.vy = 0;
state.celebration = false;
state.commentary = '';
}
// Main game loop
function gameLoop() {
// Clear canvas
ctx.fillStyle = '#008800';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw field markings
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 2;
// Center circle
ctx.beginPath();
ctx.arc(canvas.width/2, canvas.height/2, 50, 0, Math.PI * 2);
ctx.stroke();
// Center line
ctx.beginPath();
ctx.moveTo(canvas.width/2, 0);
ctx.lineTo(canvas.width/2, canvas.height);
ctx.stroke();
// Goals
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, canvas.height/2 - 50, 10, 100);
ctx.fillRect(canvas.width - 10, canvas.height/2 - 50, 10, 100);
// Update all players
[...state.teams.home.players, ...state.teams.away.players].forEach(player => {
player.update();
player.draw();
});
// Update and draw ball
updateBall();
ctx.fillStyle = '#ffffff';
ctx.beginPath();
ctx.arc(state.ball.x, state.ball.y, state.ball.radius, 0, Math.PI * 2);
ctx.fill();
// Draw celebration text
if (state.celebration) {
ctx.fillStyle = '#ffffff';
ctx.font = '30px Arial';
ctx.fillText(state.commentary, canvas.width/2 - 100, canvas.height/2);
}
requestAnimationFrame(gameLoop);
}
// Player controls
document.addEventListener('keydown', (e) => {
const player = state.teams.home.players[0]; // Control first player
const speed = 3;
switch(e.key) {
case 'ArrowUp':
player.vy = -speed;
break;
case 'ArrowDown':
player.vy = speed;
break;
case 'ArrowLeft':
player.vx = -speed;
break;
case 'ArrowRight':
player.vx = speed;
break;
case ' ': // Shoot/Pass
if (player.distanceTo(state.ball) < 30) {
const angle = Math.atan2(
canvas.width - player.x,
canvas.height/2 - player.y
);
state.ball.vx = Math.sin(angle) * 10;
state.ball.vy = Math.cos(angle) * 10;
}
break;
}
});
// Start the game
gameLoop();
</script>
</body>
</html>