html Copy code
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simulatore di Calcio Avanzato</title>
<style>
body {
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
background: #2d5a87;
color: white;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
#gameCanvas {
border: 3px solid #fff;
background: #4CAF50;
display: block;
margin: 20px auto;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
margin: 20px 0;
padding: 20px;
background: rgba(255,255,255,0.1);
border-radius: 10px;
}
.control-group {
background: rgba(255,255,255,0.1);
padding: 15px;
border-radius: 8px;
}
.control-group h3 {
margin: 0 0 10px 0;
color: #FFD700;
}
input, select, button {
width: 100%;
padding: 8px;
margin: 5px 0;
border: none;
border-radius: 4px;
box-sizing: border-box;
}
button {
background: #4CAF50;
color: white;
cursor: pointer;
font-weight: bold;
}
button:hover {
background: #45a049;
}
.score {
text-align: center;
font-size: 24px;
font-weight: bold;
margin: 20px 0;
color: #FFD700;
}
.speed-controls {
text-align: center;
margin: 20px 0;
}
.speed-controls button {
width: auto;
padding: 10px 20px;
margin: 0 5px;
}
#goalMessage {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 48px;
font-weight: bold;
color: #FFD700;
text-shadow: 3px 3px 6px rgba(0,0,0,0.8);
z-index: 1000;
display: none;
animation: goalAnim 2s ease-out;
}
@keyframes goalAnim {
0% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
50% { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}
.firework {
position: fixed;
pointer-events: none;
z-index: 999;
}
@keyframes sparkle {
0% { opacity: 1; transform: scale(1); }
100% { opacity: 0; transform: scale(0.3); }
}
</style>
</head>
<body>
<div class="container">
<h1 style="text-align: center; color: #FFD700;">⚽ Simulatore di Calcio Avanzato ⚽</h1>
<div class="score" id="scoreDisplay">Squadra Casa 0 - 0 Squadra Ospite</div>
<canvas id="gameCanvas" width="900" height="600"></canvas>
<div class="speed-controls">
<button onclick="decreaseSpeed()">🐌 Rallenta</button>
<button onclick="togglePause()">⏸️ Pausa</button>
<button onclick="increaseSpeed()">🐰 Accelera</button>
<span id="speedDisplay">Velocità: 1x</span>
</div>
<div class="controls">
<div class="control-group">
<h3>Squadra Casa</h3>
<input type="text" id="team1Name" value="Squadra Casa" placeholder="Nome squadra">
<input type="color" id="team1Color" value="#FF4444">
<select id="team1Formation">
<option value="4-3-3">4-3-3</option>
<option value="4-4-2">4-4-2</option>
<option value="3-5-2">3-5-2</option>
<option value="4-2-3-1">4-2-3-1</option>
<option value="5-3-2">5-3-2</option>
</select>
</div>
<div class="control-group">
<h3>Impostazioni Partita</h3>
<input type="color" id="ballColor" value="#FFFFFF">
<label>Colore Pallone</label>
<button onclick="resetMatch()">🔄 Reset Partita</button>
<button onclick="resetPositions()">📍 Reset Posizioni</button>
</div>
<div class="control-group">
<h3>Squadra Ospite</h3>
<input type="text" id="team2Name" value="Squadra Ospite" placeholder="Nome squadra">
<input type="color" id="team2Color" value="#4444FF">
<select id="team2Formation">
<option value="4-3-3">4-3-3</option>
<option value="4-4-2">4-4-2</option>
<option value="3-5-2">3-5-2</option>
<option value="4-2-3-1">4-2-3-1</option>
<option value="5-3-2">5-3-2</option>
</select>
</div>
</div>
</div>
<div id="goalMessage">⚽ GOOOOOL! ⚽</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Variabili di gioco
let gameSpeed = 1;
let isPaused = false;
let score = {team1: 0, team2: 0};
let ball = {x: 450, y: 300, vx: 0, vy: 0, owner: null, radius: 8};
let players = [];
let particles = [];
let goalCelebration = false;
// Formazioni
const formations = {
'4-3-3': [
{x: 0.1, y: 0.5}, // Portiere
{x: 0.25, y: 0.2}, {x: 0.25, y: 0.4}, {x: 0.25, y: 0.6}, {x: 0.25, y: 0.8}, // Difesa
{x: 0.5, y: 0.3}, {x: 0.5, y: 0.5}, {x: 0.5, y: 0.7}, // Centrocampo
{x: 0.75, y: 0.25}, {x: 0.75, y: 0.5}, {x: 0.75, y: 0.75} // Attacco
],
'4-4-2': [
{x: 0.1, y: 0.5},
{x: 0.25, y: 0.2}, {x: 0.25, y: 0.4}, {x: 0.25, y: 0.6}, {x: 0.25, y: 0.8},
{x: 0.5, y: 0.2}, {x: 0.5, y: 0.4}, {x: 0.5, y: 0.6}, {x: 0.5, y: 0.8},
{x: 0.75, y: 0.35}, {x: 0.75, y: 0.65}
],
'3-5-2': [
{x: 0.1, y: 0.5},
{x: 0.25, y: 0.3}, {x: 0.25, y: 0.5}, {x: 0.25, y: 0.7},
{x: 0.45, y: 0.15}, {x: 0.45, y: 0.35}, {x: 0.45, y: 0.5}, {x: 0.45, y: 0.65}, {x: 0.45, y: 0.85},
{x: 0.75, y: 0.35}, {x: 0.75, y: 0.65}
],
'4-2-3-1': [
{x: 0.1, y: 0.5},
{x: 0.25, y: 0.2}, {x: 0.25, y: 0.4}, {x: 0.25, y: 0.6}, {x: 0.25, y: 0.8},
{x: 0.45, y: 0.35}, {x: 0.45, y: 0.65},
{x: 0.65, y: 0.25}, {x: 0.65, y: 0.5}, {x: 0.65, y: 0.75},
{x: 0.8, y: 0.5}
],
'5-3-2': [
{x: 0.1, y: 0.5},
{x: 0.25, y: 0.1}, {x: 0.25, y: 0.3}, {x: 0.25, y: 0.5}, {x: 0.25, y: 0.7}, {x: 0.25, y: 0.9},
{x: 0.5, y: 0.3}, {x: 0.5, y: 0.5}, {x: 0.5, y: 0.7},
{x: 0.75, y: 0.35}, {x: 0.75, y: 0.65}
]
};
class Player {
constructor(x, y, team, role, formation) {
this.x = x;
this.y = y;
this.startX = x;
this.startY = y;
this.team = team;
this.role = role;
this.formation = formation;
this.vx = 0;
this.vy = 0;
this.radius = 12;
this.speed = 2;
this.hasBall = false;
this.target = null;
this.lastAction = 0;
}
update() {
if (isPaused || goalCelebration) return;
// Calcola comportamento basato sul ruolo e situazione
this.calculateBehavior();
// Applica movimento
this.x += this.vx * gameSpeed;
this.y += this.vy * gameSpeed;
// Mantieni nel campo
this.x = Math.max(this.radius, Math.min(canvas.width - this.radius, this.x));
this.y = Math.max(this.radius, Math.min(canvas.height - this.radius, this.y));
// Gestisci possesso palla
if (this.distanceTo(ball) < this.radius + ball.radius + 5) {
if (!ball.owner || this.team !== ball.owner.team) {
ball.owner = this;
ball.vx *= 0.8;
ball.vy *= 0.8;
}
}
// Se ha la palla, la porta con sé
if (ball.owner === this) {
ball.x = this.x + Math.cos(Math.atan2(this.vy, this.vx)) * (this.radius + ball.radius);
ball.y = this.y + Math.sin(Math.atan2(this.vy, this.vx)) * (this.radius + ball.radius);
}
}
calculateBehavior() {
const ballDistance = this.distanceTo(ball);
const ownGoal = this.team === 1 ? {x: 0, y: canvas.height/2} : {x: canvas.width, y: canvas.height/2};
const enemyGoal = this.team === 1 ? {x: canvas.width, y: canvas.height/2} : {x: 0, y: canvas.height/2};
// Reset velocità
this.vx = 0;
this.vy = 0;
if (ball.owner === this) {
// Ha la palla - cerca di avanzare o passare
this.handleBallPossession(enemyGoal);
} else if (ball.owner && ball.owner.team === this.team) {
// Compagno ha la palla - supporta
this.supportTeammate();
} else {
// Palla libera o avversario - insegui o marca
if (ballDistance < 100 && !ball.owner) {
this.chaseLooseBall();
} else {
this.defaultPositioning();
}
}
// Limita velocità
const maxSpeed = this.speed * (this.role === 'goalkeeper' ? 0.8 : 1);
const currentSpeed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
if (currentSpeed > maxSpeed) {
this.vx = (this.vx / currentSpeed) * maxSpeed;
this.vy = (this.vy / currentSpeed) * maxSpeed;
}
}
handleBallPossession(enemyGoal) {
const distToGoal = Math.sqrt((enemyGoal.x - this.x)**2 + (enemyGoal.y - this.y)**2);
// Se è vicino alla porta, tira
if (distToGoal < 150 && Math.random() < 0.02) {
this.shootBall(enemyGoal);
return;
}
// Cerca un passaggio migliore
const teammate = this.findBestTeammate();
if (teammate && Math.random() < 0.015) {
this.passBall(teammate);
return;
}
// Avanza verso la porta
const dx = enemyGoal.x - this.x;
const dy = enemyGoal.y - this.y;
const dist = Math.sqrt(dx*dx + dy*dy);
this.vx = (dx / dist) * this.speed * 0.7;
this.vy = (dy / dist) * this.speed * 0.7;
}
supportTeammate() {
if (!ball.owner) return;
// Posizionati per ricevere un passaggio
const angleToGoal = Math.atan2(
(this.team === 1 ? canvas.width : 0) - ball.owner.x,
canvas.height/2 - ball.owner.y
);
const supportX = ball.owner.x + Math.cos(angleToGoal + 0.5) * 80;
const supportY = ball.owner.y + Math.sin(angleToGoal + 0.5) * 80;
this.moveTowards(supportX, supportY, 0.6);
}
chaseLooseBall() {
this.moveTowards(ball.x, ball.y, 1);
}
defaultPositioning() {
// Torna alla posizione di formazione modificata in base alla situazione
let targetX = this.startX;
let targetY = this.startY;
// Aggiusta posizione in base a dove si trova la palla
if (ball.x < canvas.width / 2) {
// Palla nella propria metà campo
if (this.team === 1) {
targetX = this.startX * 0.8; // Arretra leggermente
} else {
targetX = this.startX + (canvas.width - this.startX) * 0.2; // Avanza
}
} else {
// Palla nella metà campo avversaria
if (this.team === 1) {
targetX = this.startX + (canvas.width - this.startX) * 0.3; // Avanza
} else {
targetX = this.startX * 0.7; // Arretra
}
}
this.moveTowards(targetX, targetY, 0.4);
}
findBestTeammate() {
let bestTeammate = null;
let bestScore = -1;
players.forEach(player => {
if (player.team === this.team && player !== this) {
const distance = this.distanceTo(player);
const enemyGoal = this.team === 1 ? {x: canvas.width, y: canvas.height/2} : {x: 0, y: canvas.height/2};
const progressScore = this.team === 1 ? player.x : (canvas.width - player.x);
const score = progressScore / distance;
if (score > bestScore && distance < 150) {
bestScore = score;
bestTeammate = player;
}
}
});
return bestTeammate;
}
passBall(target) {
if (ball.owner !== this) return;
const dx = target.x - this.x;
const dy = target.y - this.y;
const dist = Math.sqrt(dx*dx + dy*dy);
ball.owner = null;
ball.vx = (dx / dist) * 8;
ball.vy = (dy / dist) * 8;
}
shootBall(goal) {
if (ball.owner !== this) return;
const dx = goal.x - this.x;
const dy = (goal.y + (Math.random() - 0.5) * 100) - this.y;
const dist = Math.sqrt(dx*dx + dy*dy);
ball.owner = null;
ball.vx = (dx / dist) * 12;
ball.vy = (dy / dist) * 12;
}
moveTowards(targetX, targetY, intensity = 1) {
const dx = targetX - this.x;
const dy = targetY - this.y;
const dist = Math.sqrt(dx*dx + dy*dy);
if (dist > 5) {
this.vx = (dx / dist) * this.speed * intensity;
this.vy = (dy / dist) * this.speed * intensity;
}
}
distanceTo(target) {
return Math.sqrt((target.x - this.x)**2 + (target.y - this.y)**2);
}
draw() {
// Disegna giocatore
ctx.fillStyle = this.team === 1 ?
document.getElementById('team1Color').value :
document.getElementById('team2Color').value;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
// Bordo
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
ctx.stroke();
// Numero giocatore
ctx.fillStyle = '#fff';
ctx.font = '10px Arial';
ctx.textAlign = 'center';
ctx.fillText(this.role === 'goalkeeper' ? 'GK' : players.indexOf(this) + 1, this.x, this.y + 3);
}
}
function initializePlayers() {
players = [];
// Squadra 1
const team1Formation = formations[document.getElementById('team1Formation').value];
team1Formation.forEach((pos, index) => {
const x = pos.x * canvas.width;
const y = pos.y * canvas.height;
const role = index === 0 ? 'goalkeeper' : (index < 5 ? 'defender' : (index < 8 ? 'midfielder' : 'forward'));
players.push(new Player(x, y, 1, role, pos));
});
// Squadra 2 (specchiata)
const team2Formation = formations[document.getElementById('team2Formation').value];
team2Formation.forEach((pos, index) => {
const x = canvas.width - (pos.x * canvas.width);
const y = pos.y * canvas.height;
const role = index === 0 ? 'goalkeeper' : (index < 5 ? 'defender' : (index < 8 ? 'midfielder' : 'forward'));
players.push(new Player(x, y, 2, role, pos));
});
}
function updateBall() {
if (isPaused || goalCelebration) return;
if (!ball.owner) {
ball.x += ball.vx * gameSpeed;
ball.y += ball.vy * gameSpeed;
// Attrito
ball.vx *= 0.98;
ball.vy *= 0.98;
// Rimbalzo sui bordi (escluse le porte)
if (ball.y <= ball.radius || ball.y >= canvas.height - ball.radius) {
ball.vy = -ball.vy;
}
// Check goal
if (ball.x <= 0) {
if (ball.y > canvas.height * 0.35 && ball.y < canvas.height * 0.65) {
goal(2);
} else {
ball.vx = -ball.vx;
}
}
if (ball.x >= canvas.width) {
if (ball.y > canvas.height * 0.35 && ball.y < canvas.height * 0.65) {
goal(1);
} else {
ball.vx = -ball.vx;
}
}
// Mantieni nel campo
ball.x = Math.max(ball.radius, Math.min(canvas.width - ball.radius, ball.x));
ball.y = Math.max(ball.radius, Math.min(canvas.height - ball.radius, ball.y));
}
}
function checkCollisions() {
// Collisioni tra giocatori
for (let i = 0; i < players.length; i++) {
for (let j = i + 1; j < players.length; j++) {
const p1 = players[i];
const p2 = players[j];
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
const distance = Math.sqrt(dx*dx + dy*dy);
if (distance < p1.radius + p2.radius) {
// Spallata/rimbalzo
const overlap = p1.radius + p2.radius - distance;
const pushX = (dx / distance) * overlap * 0.5;
const pushY = (dy / distance) * overlap * 0.5;
p1.x -= pushX;
p1.y -= pushY;
p2.x += pushX;
p2.y += pushY;
// Trasferimento palla in caso di contrasto
if (ball.owner === p1 && p1.team !== p2.team && Math.random() < 0.3) {
ball.owner = p2;
} else if (ball.owner === p2 && p1.team !== p2.team && Math.random() < 0.3) {
ball.owner = p1;
}
}
}
}
}
function goal(team) {
if (goalCelebration) return;
goalCelebration = true;
score[`team${team}`]++;
// Mostra messaggio goal
const goalMsg = document.getElementById('goalMessage');
goalMsg.style.display = 'block';
goalMsg.innerHTML = `⚽ GOAL! ${team === 1 ? document.getElementById('team1Name').value : document.getElementById('team2Name').value} ⚽`;
// Crea fuochi d'artificio
createFireworks();
// Reset dopo 3 secondi
setTimeout(() => {
resetPositions();
goalMsg.style.display = 'none';
goalCelebration = false;
}, 3000);
updateScore();
}
function createFireworks() {
for (let i = 0; i < 50; i++) {
setTimeout(() => {
const firework = document.createElement('div');
firework.className = 'firework';
firework.innerHTML = '✨';
firework.style.left = Math.random() * window.innerWidth + 'px';
firework.style.top = Math.random() * window.innerHeight + 'px';
firework.style.fontSize = (Math.random() * 20 + 15) + 'px';
firework.style.animation = 'sparkle 1s ease-out forwards';
document.body.appendChild(firework);
setTimeout(() => firework.remove(), 1000);
}, i * 50);
}
}
function resetPositions() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.vx = 0;
ball.vy = 0;
ball.owner = null;
players.forEach(player => {
player.x = player.startX;
player.y = player.startY;
player.vx = 0;
player.vy = 0;
});
}
function drawField() {
// Campo
ctx.fillStyle = '#4CAF50';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Linee del campo
ctx.strokeStyle = '#fff';
ctx.lineWidth = 3;
// Bordi
ctx.strokeRect(0, 0, canvas.width, canvas.height);
// Linea di metà campo
ctx.beginPath();
ctx.moveTo(canvas.width/2, 0);
ctx.lineTo(canvas.width/2, canvas.height);
ctx.stroke();
// Cerchio di centro
ctx.beginPath();
ctx.arc(canvas.width/2, canvas.height/2, 80, 0, Math.PI * 2);
ctx.stroke();
// Aree di rigore
const goalWidth = canvas.height * 0.3;
const goalY = (canvas.height - goalWidth) / 2;
const penaltyWidth = 120;
const penaltyHeight = 200;
// Area di rigore sinistra
ctx.strokeRect(0, (canvas.height - penaltyHeight)/2, penaltyWidth, penaltyHeight);
// Area di rigore destra
ctx.strokeRect(canvas.width - penaltyWidth, (canvas.height - penaltyHeight)/2, penaltyWidth, penaltyHeight);
// Porte
ctx.lineWidth = 5;
ctx.strokeStyle = '#FFD700';
ctx.strokeRect(-10, goalY, 10, goalWidth);
ctx.strokeRect(canvas.width, goalY, 10, goalWidth);
}
function drawBall() {
ctx.fillStyle = document.getElementById('ballColor').value;
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
ctx.stroke();
// Pattern della palla
ctx.strokeStyle = '#333';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(ball.x - ball.radius, ball.y);
ctx.lineTo(ball.x + ball.radius, ball.y);
ctx.moveTo(ball.x, ball.y - ball.radius);
ctx.lineTo(ball.x, ball.y + ball.radius);
ctx.stroke();
}
function gameLoop() {
// Aggiorna
players.forEach(player => player.update());
updateBall();
checkCollisions();
// Disegna
drawField();
players.forEach(player => player.draw());
drawBall();
requestAnimationFrame(gameLoop);
}
function updateScore() {
const team1Name = document.getElementById('team1Name').value;
const team2Name = document.getElementById('team2Name').value;
document.getElementById('scoreDisplay').textContent =
`${team1Name} ${score.team1} - ${score.team2} ${team2Name}`;
}
function togglePause() {
isPaused = !isPaused;
document.querySelector('button[onclick="togglePause()"]').textContent =
isPaused ? '▶️ Riprendi' : '⏸️ Pausa';
}
function increaseSpeed() {
gameSpeed = Math.min(3, gameSpeed + 0.2);
document.getElementById('speedDisplay').textContent = `Velocità: ${gameSpeed.toFixed(1)}x`;
}
function decreaseSpeed() {
gameSpeed = Math.max(0.2, gameSpeed - 0.2);
document.getElementById('speedDisplay').textContent = `Velocità: ${gameSpeed.toFixed(1)}x`;
}
function resetMatch() {
score = {team1: 0, team2: 0};
updateScore();
resetPositions();
}
// Event Listeners
document.getElementById('team1Name').addEventListener('input', updateScore);
document.getElementById('team2Name').addEventListener('input', updateScore);
document.getElementById('team1Formation').addEventListener('change', initializePlayers);
document.getElementById('team2Formation').addEventListener('change', initializePlayers);
// Inizializzazione
initializePlayers();
updateScore();
gameLoop();
</script>
</body>
</html>
Il simulatore è completamente funzionale e include una logica di gioco avanzata che fa comportare i giocatori in modo realistico!