Sure! Below is an HTML file that creates a simple top-down 2D football (soccer) game using HTML and JavaScript in one file. This code draws a football field using shapes, adds a moving ball, and includes players represented as circles.
You can copy and paste this code into a single .html file and open it in a web browser to see it in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Top-Down 2D Football Game</title>
<style>
body {
margin: 0;
background-color: #006400; /* Dark green background */
}
canvas {
display: block;
margin: 0 auto;
background-color: #228B22; /* Football pitch green */
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="500"></canvas>
<script>
// Get the canvas element and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const fieldWidth = canvas.width;
const fieldHeight = canvas.height;
// Ball object
const ball = {
x: fieldWidth / 2,
y: fieldHeight / 2,
radius: 8,
color: '#FFFFFF',
dx: 2,
dy: -2
};
// Players array
const players = [];
// Function to draw the football field
function drawField() {
// Draw the outer lines
ctx.strokeStyle = '#FFFFFF';
ctx.lineWidth = 2;
ctx.strokeRect(0, 0, fieldWidth, fieldHeight);
// Center line
ctx.beginPath();
ctx.moveTo(fieldWidth / 2, 0);
ctx.lineTo(fieldWidth / 2, fieldHeight);
ctx.stroke();
// Center circle
ctx.beginPath();
ctx.arc(fieldWidth / 2, fieldHeight / 2, 70, 0, 2 * Math.PI);
ctx.stroke();
// Center spot
ctx.beginPath();
ctx.arc(fieldWidth / 2, fieldHeight / 2, 3, 0, 2 * Math.PI);
ctx.fill();
// Penalty areas
drawPenaltyAreas();
}
// Function to draw penalty areas and penalty spots
function drawPenaltyAreas() {
ctx.strokeStyle = '#FFFFFF';
// Left penalty area
ctx.strokeRect(0, (fieldHeight / 2) - 110, 130, 220);
// Left goal area
ctx.strokeRect(0, (fieldHeight / 2) - 55, 44, 110);
// Left penalty spot
ctx.beginPath();
ctx.arc(88, fieldHeight / 2, 2, 0, 2 * Math.PI);
ctx.fill();
// Right penalty area
ctx.strokeRect(fieldWidth - 130, (fieldHeight / 2) - 110, 130, 220);
// Right goal area
ctx.strokeRect(fieldWidth - 44, (fieldHeight / 2) - 55, 44, 110);
// Right penalty spot
ctx.beginPath();
ctx.arc(fieldWidth - 88, fieldHeight / 2, 2, 0, 2 * Math.PI);
ctx.fill();
// Arcs at the top of the penalty areas
ctx.beginPath();
ctx.arc(130, fieldHeight / 2, 60, 0.29 * Math.PI, 1.71 * Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(fieldWidth - 130, fieldHeight / 2, 60, 1.29 * Math.PI, 0.71 * Math.PI);
ctx.stroke();
}
// Function to create players
function createPlayers() {
const team1Color = '#0000FF'; // Blue team
const team2Color = '#FF0000'; // Red team
const playerRadius = 10;
// Team 1 positions
const team1Positions = [
{ x: fieldWidth / 4, y: fieldHeight / 2 },
{ x: fieldWidth / 4 - 50, y: fieldHeight / 3 },
{ x: fieldWidth / 4 - 50, y: 2 * fieldHeight / 3 },
{ x: fieldWidth / 4 + 50, y: fieldHeight / 3 },
{ x: fieldWidth / 4 + 50, y: 2 * fieldHeight / 3 }
];
// Team 2 positions
const team2Positions = [
{ x: 3 * fieldWidth / 4, y: fieldHeight / 2 },
{ x: 3 * fieldWidth / 4 + 50, y: fieldHeight / 3 },
{ x: 3 * fieldWidth / 4 + 50, y: 2 * fieldHeight / 3 },
{ x: 3 * fieldWidth / 4 - 50, y: fieldHeight / 3 },
{ x: 3 * fieldWidth / 4 - 50, y: 2 * fieldHeight / 3 }
];
// Add team 1 players
team1Positions.forEach(pos => {
players.push({
x: pos.x,
y: pos.y,
radius: playerRadius,
color: team1Color
});
});
// Add team 2 players
team2Positions.forEach(pos => {
players.push({
x: pos.x,
y: pos.y,
radius: playerRadius,
color: team2Color
});
});
}
// Function to draw players
function drawPlayers() {
players.forEach(player => {
ctx.beginPath();
ctx.arc(player.x, player.y, player.radius, 0, 2 * Math.PI);
ctx.fillStyle = player.color;
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = '#FFFFFF';
ctx.stroke();
});
}
// Function to draw the ball
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, 2 * Math.PI);
ctx.fillStyle = ball.color;
ctx.fill();
}
// Function to update game objects
function update() {
// Move the ball
ball.x += ball.dx;
ball.y += ball.dy;
// Check for collisions with the walls
if (ball.x + ball.radius > fieldWidth || ball.x - ball.radius < 0) {
ball.dx = -ball.dx;
}
if (ball.y + ball.radius > fieldHeight || ball.y - ball.radius < 0) {
ball.dy = -ball.dy;
}
}
// Main game loop
function gameLoop() {
// Clear the canvas
ctx.clearRect(0, 0, fieldWidth, fieldHeight);
// Draw the field, players, and ball
drawField();
drawPlayers();
drawBall();
// Update game objects
update();
// Request next animation frame
requestAnimationFrame(gameLoop);
}
// Initialize the game
createPlayers();
gameLoop();
</script>
</body>
</html>
Explanation:
How to Use:
-
Save the Code:
- Copy the entire code and save it in a file with an
.html extension (e.g., football_game.html).
-
Open in Browser:
- Open the HTML file in a modern web browser (e.g., Chrome, Firefox, Edge).
-
Interact:
- The ball will automatically move and bounce off the field boundaries.
- Players are stationary circles on the field.
Customization:
- You can modify player positions, colors, and add movement logic to enhance the game.
- To make players move or interact with the ball, you can add event listeners and additional logic in the
update() function.
Note:
- This is a basic implementation to get you started with creating a top-down 2D football game using HTML and JavaScript in one file.
- Further development can include player controls, scoring, AI opponents, and more complex physics.
Feel free to ask if you have any questions or need assistance with modifications!