Ecco un simulatore di calcio completo in HTML+CSS+JS che rispetta tutte le tue richieste:
- Campo da calcio con 22 giocatori che inseguono la palla
- Personalizzazione: nomi squadre, colori maglie, colore palla
- Velocità regolabile per palla e giocatori
- Goal: scritta "GOAL!" con sparkles e fuochi d'artificio, reset posizioni
- Collisioni: i giocatori non si sovrappongono, rimbalzano leggermente
Copia e incolla tutto in un file .html e aprilo con il browser!
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Simulatore Calcio Avanzato</title>
<style>
:root {
--team-a-color: #e53935;
--team-b-color: #3949ab;
--ball-color: #fff;
}
body { font-family: Arial,sans-serif; background:#f0f0f0; margin:0; padding:20px; }
h1 { margin-bottom:10px; }
.controls {
display:flex; flex-wrap:wrap; gap:20px; justify-content:center;
background:#fff; padding:15px 0; border-radius:10px; margin-bottom:15px;
}
.control-group { display:flex; flex-direction:column; align-items:center; }
.scoreboard {
font-size:22px; font-weight:bold; background:#222; color:#fff;
padding:8px 0; border-radius:8px; margin-bottom:15px; width:420px; margin-left:auto; margin-right:auto;
}
#team-a-display { color:var(--team-a-color); }
#team-b-display { color:var(--team-b-color); }
#field {
position:relative; width:800px; height:480px; margin:0 auto; background:#388e3c;
border:5px solid #fff; border-radius:18px; box-shadow:0 0 10px #aaa;
overflow:hidden;
}
#field::before {
content:""; position:absolute; left:50%; top:0; width:2px; height:100%; background:#fff; z-index:1;
transform:translateX(-1px);
}
#center-circle {
position:absolute; left:50%; top:50%; width:100px; height:100px; border:2px solid #fff;
border-radius:50%; transform:translate(-50%,-50%); z-index:1;
}
.goal {
position:absolute; top:40%; width:10px; height:20%; background:#fff; z-index:2;
}
#goal-a { left:0; }
#goal-b { right:0; }
.player {
position:absolute; width:22px; height:22px; border-radius:50%; z-index:3;
display:flex; align-items:center; justify-content:center; color:#fff; font-size:11px; font-weight:bold;
box-shadow:0 0 4px #2226;
transition:transform 0.1s;
}
.team-a { background:var(--team-a-color); }
.team-b { background:var(--team-b-color); }
.player.collide { transform:scale(1.15); }
#ball {
position:absolute; width:16px; height:16px; border-radius:50%; background:var(--ball-color);
border:2px solid #222; z-index:4; box-shadow:0 0 6px #0008;
}
.speed-control { display:flex; align-items:center; gap:6px; }
.speed-value { min-width:32px; text-align:right; }
#goal-animation {
position:fixed; top:0; left:0; width:100vw; height:100vh; background:rgba(0,0,0,0.7);
display:flex; flex-direction:column; justify-content:center; align-items:center;
z-index:1000; opacity:0; pointer-events:none; transition:opacity 0.3s;
}
#goal-animation.visible { opacity:1; pointer-events:auto; }
#goal-text {
color:gold; font-size:100px; font-weight:bold; text-shadow:0 0 30px #fff,0 0 60px #ff0;
animation:goalPulse 0.5s infinite alternate;
}
@keyframes goalPulse { from{transform:scale(1);} to{transform:scale(1.1);} }
.sparkle {
position:absolute; width:5px; height:5px; background:gold; border-radius:50%;
pointer-events:none; opacity:0.8;
}
.firework {
position:absolute; width:6px; height:6px; border-radius:50%; pointer-events:none;
}
</style>
</head>
<body>
<h1>Simulatore di Calcio Avanzato</h1>
<div class="controls">
<div class="control-group">
<label>Squadra Casa</label>
<input type="text" id="team-a-name" value="Squadra A">
<input type="color" id="team-a-color" value="#e53935">
</div>
<div class="control-group">
<label>Velocità Giocatori</label>
<div class="speed-control">
<input type="range" id="player-speed" min="0.5" max="3" step="0.1" value="1">
<span class="speed-value" id="player-speed-value">1x</span>
</div>
</div>
<div class="control-group">
<label>Velocità Palla</label>
<div class="speed-control">
<input type="range" id="ball-speed" min="0.5" max="3" step="0.1" value="1">
<span class="speed-value" id="ball-speed-value">1x</span>
</div>
</div>
<div class="control-group">
<label>Squadra Ospite</label>
<input type="text" id="team-b-name" value="Squadra B">
<input type="color" id="team-b-color" value="#3949ab">
</div>
<div class="control-group">
<label>Colore Palla</label>
<input type="color" id="ball-color" value="#ffffff">
</div>
</div>
<div class="scoreboard">
<span id="team-a-display">Squadra A</span>
<span id="score">0 - 0</span>
<span id="team-b-display">Squadra B</span>
</div>
<div id="field">
<div id="center-circle"></div>
<div id="goal-a" class="goal"></div>
<div id="goal-b" class="goal"></div>
<div id="ball"></div>
</div>
<div id="goal-animation">
<div id="goal-text">GOAL!</div>
</div>
<script>
const field = document.getElementById('field');
const ball = document.getElementById('ball');
const scoreDisplay = document.getElementById('score');
const goalAnimation = document.getElementById('goal-animation');
const teamAName = document.getElementById('team-a-name');
const teamAColor = document.getElementById('team-a-color');
const teamBName = document.getElementById('team-b-name');
const teamBColor = document.getElementById('team-b-color');
const ballColor = document.getElementById('ball-color');
const playerSpeedInput = document.getElementById('player-speed');
const playerSpeedValue = document.getElementById('player-speed-value');
const ballSpeedInput = document.getElementById('ball-speed');
const ballSpeedValue = document.getElementById('ball-speed-value');
const teamADisplay = document.getElementById('team-a-display');
const teamBDisplay = document.getElementById('team-b-display');
const fieldW = 800, fieldH = 480, playerR = 11, playerD = 22, ballD = 16;
const goalY1 = fieldH*0.4, goalY2 = fieldH*0.6, goalW = 10;
let scoreA = 0, scoreB = 0, paused = false;
let playerBaseSpeed = 1, ballBaseSpeed = 1;
let players = [];
let ballObj = {};
let initialPositions = [];
function createPlayers() {
players = [];
for(let i=0;i<22;i++) {
const el = document.createElement('div');
el.className = 'player ' + (i<11?'team-a':'team-b');
el.textContent = (i%11)+1;
field.appendChild(el);
let pos;
if(i<11) pos = {x:80+Math.random()*120, y:60+i*32};
else pos = {x:fieldW-80-Math.random()*120, y:60+(i-11)*32};
players.push({
el, team: i<11?'a':'b',
x: pos.x, y: pos.y,
vx:0, vy:0
});
initialPositions[i] = {x: pos.x, y: pos.y};
}
}
function resetPositions() {
for(let i=0;i<22;i++) {
players[i].x = initialPositions[i].x;
players[i].y = initialPositions[i].y;
players[i].vx = 0; players[i].vy = 0;
}
ballObj.x = fieldW/2-ballD/2;
ballObj.y = fieldH/2-ballD/2;
ballObj.vx = (Math.random()-0.5)*2*ballBaseSpeed;
ballObj.vy = (Math.random()-0.5)*2*ballBaseSpeed;
}
function updateCustomization() {
document.documentElement.style.setProperty('--team-a-color', teamAColor.value);
document.documentElement.style.setProperty('--team-b-color', teamBColor.value);
document.documentElement.style.setProperty('--ball-color', ballColor.value);
teamADisplay.textContent = teamAName.value;
teamBDisplay.textContent = teamBName.value;
}
teamAName.addEventListener('input', updateCustomization);
teamBName.addEventListener('input', updateCustomization);
teamAColor.addEventListener('input', updateCustomization);
teamBColor.addEventListener('input', updateCustomization);
ballColor.addEventListener('input', updateCustomization);
playerSpeedInput.addEventListener('input',()=>{
playerBaseSpeed = parseFloat(playerSpeedInput.value);
playerSpeedValue.textContent = playerBaseSpeed+'x';
});
ballSpeedInput.addEventListener('input',()=>{
ballBaseSpeed = parseFloat(ballSpeedInput.value);
ballSpeedValue.textContent = ballBaseSpeed+'x';
});
playerSpeedValue.textContent = playerBaseSpeed+'x';
ballSpeedValue.textContent = ballBaseSpeed+'x';
function updateBall() {
ballObj.x += ballObj.vx;
ballObj.y += ballObj.vy;
ballObj.vx *= 0.985;
ballObj.vy *= 0.985;
// Bordo campo
if(ballObj.x<0 || ballObj.x>fieldW-ballD) {
// Goal?
if(ballObj.y+ballD/2>goalY1 && ballObj.y+ballD/2<goalY2) {
if(ballObj.x<0) goal('b');
else goal('a');
return;
} else {
ballObj.vx = -ballObj.vx*0.7;
ballObj.x = ballObj.x<0?0:fieldW-ballD;
}
}
if(ballObj.y<0 || ballObj.y>fieldH-ballD) {
ballObj.vy = -ballObj.vy*0.7;
ballObj.y = ballObj.y<0?0:fieldH-ballD;
}
ball.style.left = ballObj.x+'px';
ball.style.top = ballObj.y+'px';
}
function updatePlayers() {
// Movimento verso la palla
for(let i=0;i<22;i++) {
let p = players[i];
let dx = (ballObj.x+ballD/2)-(p.x+playerD/2);
let dy = (ballObj.y+ballD/2)-(p.y+playerD/2);
let dist = Math.sqrt(dx*dx+dy*dy);
let speed = playerBaseSpeed*1.2;
if(dist>playerD*0.7) {
p.vx += (dx/dist)*0.12*playerBaseSpeed;
p.vy += (dy/dist)*0.12*playerBaseSpeed;
} else {
// Tiro verso porta avversaria
let tx = p.team=='a'?fieldW-20:20, ty = fieldH/2+((Math.random()-0.5)*80);
let tdx = tx-(p.x+playerD/2), tdy = ty-(p.y+playerD/2);
let tdist = Math.sqrt(tdx*tdx+tdy*tdy);
let kick = 3.5*ballBaseSpeed + Math.random()*1.5*ballBaseSpeed;
ballObj.vx = (tdx/tdist)*kick;
ballObj.vy = (tdy/tdist)*kick;
}
// Limita velocità
let vlen = Math.sqrt(p.vx*p.vx+p.vy*p.vy);
if(vlen>speed) { p.vx = p.vx/vlen*speed; p.vy = p.vy/vlen*speed; }
p.x += p.vx;
p.y += p.vy;
// Bordo campo
if(p.x<0) { p.x=0; p.vx*=-0.5; }
if(p.x>fieldW-playerD) { p.x=fieldW-playerD; p.vx*=-0.5; }
if(p.y<0) { p.y=0; p.vy*=-0.5; }
if(p.y>fieldH-playerD) { p.y=fieldH-playerD; p.vy*=-0.5; }
p.el.style.left = p.x+'px';
p.el.style.top = p.y+'px';
p.el.classList.remove('collide');
}
// Collisioni tra giocatori
for(let i=0;i<22;i++) for(let j=i+1;j<22;j++) {
let p1=players[i], p2=players[j];
let dx=p2.x-p1.x, dy=p2.y-p1.y;
let dist=Math.sqrt(dx*dx+dy*dy);
if(dist<playerD-2) {
// Rimbalzo leggero
let overlap=(playerD-dist)/2;
let ox=(dx/dist)*overlap, oy=(dy/dist)*overlap;
p1.x-=ox; p1.y-=oy; p2.x+=ox; p2.y+=oy;
// Piccolo rimbalzo sulle velocità
let rvx=(p2.vx-p1.vx)*0.15, rvy=(p2.vy-p1.vy)*0.15;
p1.vx-=rvx; p1.vy-=rvy; p2.vx+=rvx; p2.vy+=rvy;
p1.el.classList.add('collide'); p2.el.classList.add('collide');
}
}
}
function goal(team) {
if(paused) return;
paused = true;
if(team=='a') scoreA++; else scoreB++;
scoreDisplay.textContent = `${scoreA} - ${scoreB}`;
showGoalAnimation();
setTimeout(()=>{
goalAnimation.classList.remove('visible');
resetPositions();
paused = false;
}, 3000);
}
function showGoalAnimation() {
goalAnimation.classList.add('visible');
// Sparkles
const rect = goalAnimation.getBoundingClientRect();
for(let i=0;i<60;i++) {
let s = document.createElement('div');
s.className = 'sparkle';
let angle = Math.random()*2*Math.PI;
let dist = 80+Math.random()*120;
s.style.left = (rect.width/2+Math.cos(angle)*dist)+'px';
s.style.top = (rect.height/2+Math.sin(angle)*dist)+'px';
s.style.opacity = 0.7+Math.random()*0.3;
s.style.background = 'gold';
s.style.boxShadow = '0 0 10px #fff';
goalAnimation.appendChild(s);
setTimeout(()=>s.remove(), 1200+Math.random()*800);
}
// Fireworks
for(let i=0;i<8;i++) {
setTimeout(()=>{
let fx = Math.random()*rect.width*0.8+rect.width*0.1;
let fy = Math.random()*rect.height*0.5+rect.height*0.1;
for(let j=0;j<18;j++) {
let f = document.createElement('div');
f.className = 'firework';
let a = j/18*2*Math.PI;
let d = 60+Math.random()*30;
f.style.left = fx+'px';
f.style.top = fy+'px';
f.style.background = `hsl(${Math.random()*360},100%,60%)`;
goalAnimation.appendChild(f);
setTimeout(()=>{
f.style.transition = 'all 0.7s cubic-bezier(.5,0,.5,1)';
f.style.left = (fx+Math.cos(a)*d)+'px';
f.style.top = (fy+Math.sin(a)*d)+'px';
f.style.opacity = 0;
},10);
setTimeout(()=>f.remove(), 900);
}
},i*200);
}
}
function gameLoop() {
if(!paused) {
updateBall();
updatePlayers();
}
requestAnimationFrame(gameLoop);
}
function init() {
createPlayers();
ballObj = {x:fieldW/2-ballD/2, y:fieldH/2-ballD/2, vx:0, vy:0};
resetPositions();
updateCustomization();
gameLoop();
}
init();
</script>
</body>
</html>
Tutto in un solo file!
Se vuoi altre migliorie, chiedi pure!