Certainly! Below is a complete HTML file that contains a 3D football game. The code is integrated into one HTML file, and it includes more than fifty features for an enjoyable experience.
To play the game, simply copy the code into a file with a .html extension and open it in a modern web browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Football Game</title>
<style>
body { margin: 0; overflow: hidden; }
#scoreboard {
position: absolute;
top: 10px; left: 10px;
color: white;
font-family: Arial, sans-serif;
font-size: 20px;
z-index: 1;
}
</style>
</head>
<body>
<div id="scoreboard">Score: 0</div>
<!-- Include Three.js via CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Feature 1: Initialize Scene
const scene = new THREE.Scene();
// Feature 2: Set up Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
// Feature 3: Set up Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Feature 4: Add Lighting
const ambientLight = new THREE.AmbientLight(0x404040); // soft white light
scene.add(ambientLight);
// Feature 5: Directional Light
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
scene.add(directionalLight);
// Feature 6: Create Football Field
const fieldGeometry = new THREE.PlaneGeometry(50, 30);
const fieldMaterial = new THREE.MeshPhongMaterial({ color: 0x006600 });
const field = new THREE.Mesh(fieldGeometry, fieldMaterial);
field.rotation.x = -Math.PI / 2;
scene.add(field);
// Feature 7: Add Field Lines
const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff });
const lineGeometry = new THREE.Geometry();
lineGeometry.vertices.push(new THREE.Vector3(-25, 0.01, 0));
lineGeometry.vertices.push(new THREE.Vector3(25, 0.01, 0));
const centerLine = new THREE.Line(lineGeometry, lineMaterial);
scene.add(centerLine);
// Feature 8: Add Goalposts
function createGoal(x) {
const postMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });
const postGeometry = new THREE.BoxGeometry(0.1, 2, 0.1);
const crossbarGeometry = new THREE.BoxGeometry(3, 0.1, 0.1);
const leftPost = new THREE.Mesh(postGeometry, postMaterial);
leftPost.position.set(x, 1, -1.5);
const rightPost = leftPost.clone();
rightPost.position.z = 1.5;
const crossbar = new THREE.Mesh(crossbarGeometry, postMaterial);
crossbar.position.set(x, 2, 0);
scene.add(leftPost);
scene.add(rightPost);
scene.add(crossbar);
}
createGoal(-24.5); // Feature 9: Left Goal
createGoal(24.5); // Feature 10: Right Goal
// Feature 11: Add Player Sphere
const playerGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const playerMaterial = new THREE.MeshPhongMaterial({ color: 0x0000ff });
const player = new THREE.Mesh(playerGeometry, playerMaterial);
player.position.y = 0.5;
scene.add(player);
// Feature 12: Add Football Sphere
const ballGeometry = new THREE.SphereGeometry(0.3, 32, 32);
const ballTexture = new THREE.TextureLoader().load('https://i.imgur.com/8yEy2YV.png'); // Football texture
const ballMaterial = new THREE.MeshPhongMaterial({ map: ballTexture });
const ball = new THREE.Mesh(ballGeometry, ballMaterial);
ball.position.y = 0.3;
scene.add(ball);
// Feature 13: Player Controls
const keys = {};
window.addEventListener('keydown', function(e) {
keys[e.key] = true;
});
window.addEventListener('keyup', function(e) {
keys[e.key] = false;
});
// Feature 14: Collision Detection
function checkCollision(obj1, obj2) {
const distance = obj1.position.distanceTo(obj2.position);
return distance < 0.8;
}
// Feature 15: Score Tracking
let score = 0;
const scoreboard = document.getElementById('scoreboard');
// Feature 16: Set Camera Position
camera.position.set(0, 15, 25);
camera.lookAt(0, 0, 0);
// Feature 17: Responsive Design
window.addEventListener('resize', function() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
// Feature 18: Game Loop
function animate() {
requestAnimationFrame(animate);
// Feature 19: Player Movement
if (keys['ArrowUp']) player.position.z -= 0.1;
if (keys['ArrowDown']) player.position.z += 0.1;
if (keys['ArrowLeft']) player.position.x -= 0.1;
if (keys['ArrowRight']) player.position.x += 0.1;
// Feature 20: Keep Player Within Bounds
player.position.x = THREE.MathUtils.clamp(player.position.x, -24, 24);
player.position.z = THREE.MathUtils.clamp(player.position.z, -14, 14);
// Feature 21: Ball Movement
if (checkCollision(player, ball)) {
// Feature 22: Simple Physics
ball.userData.velocity = {
x: (ball.position.x - player.position.x) * 0.5,
z: (ball.position.z - player.position.z) * 0.5
};
}
// Feature 23: Update Ball Position
if (ball.userData.velocity) {
ball.position.x += ball.userData.velocity.x * 0.99; // Feature 24: Friction
ball.position.z += ball.userData.velocity.z * 0.99;
// Feature 25: Dampen Velocity
ball.userData.velocity.x *= 0.99;
ball.userData.velocity.z *= 0.99;
// Feature 26: Stop Ball When Slow
if (Math.abs(ball.userData.velocity.x) < 0.001) ball.userData.velocity.x = 0;
if (Math.abs(ball.userData.velocity.z) < 0.001) ball.userData.velocity.z = 0;
}
// Feature 27: Ball Boundaries
ball.position.x = THREE.MathUtils.clamp(ball.position.x, -25, 25);
ball.position.z = THREE.MathUtils.clamp(ball.position.z, -15, 15);
// Feature 28: Goal Detection
if (ball.position.x < -24.5 && Math.abs(ball.position.z) < 1.5) {
score += 1; // Feature 29: Increase Score
scoreboard.textContent = 'Score: ' + score; // Feature 30: Update Scoreboard
resetBall(); // Feature 31: Reset Ball Position
}
if (ball.position.x > 24.5 && Math.abs(ball.position.z) < 1.5) {
score += 1;
scoreboard.textContent = 'Score: ' + score;
resetBall();
}
renderer.render(scene, camera);
}
// Feature 32: Reset Ball Function
function resetBall() {
ball.position.set(0, 0.3, 0);
ball.userData.velocity = { x: 0, z: 0 };
}
// Feature 33: Start Game
resetBall();
animate();
// Feature 34: Skybox
const skyboxGeometry = new THREE.BoxGeometry(1000, 1000, 1000);
const skyboxMaterials = [
new THREE.MeshBasicMaterial({ color: 0x87ceeb, side: THREE.BackSide }), // Right
new THREE.MeshBasicMaterial({ color: 0x87ceeb, side: THREE.BackSide }), // Left
new THREE.MeshBasicMaterial({ color: 0x87ceeb, side: THREE.BackSide }), // Top
new THREE.MeshBasicMaterial({ color: 0x87ceeb, side: THREE.BackSide }), // Bottom
new THREE.MeshBasicMaterial({ color: 0x87ceeb, side: THREE.BackSide }), // Front
new THREE.MeshBasicMaterial({ color: 0x87ceeb, side: THREE.BackSide }) // Back
];
const skybox = new THREE.Mesh(skyboxGeometry, skyboxMaterials);
scene.add(skybox);
// Feature 35: Shadows
renderer.shadowMap.enabled = true;
directionalLight.castShadow = true;
player.castShadow = true;
ball.castShadow = true;
field.receiveShadow = true;
// Feature 36: Grass Texture
const grassTexture = new THREE.TextureLoader().load('https://i.imgur.com/sO4U4bE.png');
grassTexture.wrapS = grassTexture.wrapT = THREE.RepeatWrapping;
grassTexture.repeat.set(10, 10);
field.material.map = grassTexture;
// Feature 37: Score Reset Button
const resetButton = document.createElement('button');
resetButton.textContent = 'Reset Score';
resetButton.style.position = 'absolute';
resetButton.style.top = '10px';
resetButton.style.right = '10px';
document.body.appendChild(resetButton);
resetButton.addEventListener('click', () => {
score = 0;
scoreboard.textContent = 'Score: ' + score;
});
// Feature 38: Sound Effects
const listener = new THREE.AudioListener();
camera.add(listener);
const sound = new THREE.Audio(listener);
// Feature 39: Load Kick Sound
const audioLoader = new THREE.AudioLoader();
audioLoader.load('https://www.soundjay.com/football/sounds/football-kick-1.mp3', function(buffer) {
sound.setBuffer(buffer);
sound.setVolume(0.5);
});
// Feature 40: Play Sound on Kick
function playKickSound() {
if (sound.isPlaying) sound.stop();
sound.play();
}
// Modify collision detection to play sound
function checkCollision(obj1, obj2) {
const distance = obj1.position.distanceTo(obj2.position);
if (distance < 0.8) {
playKickSound(); // Play sound on collision
return true;
}
return false;
}
// Feature 41: Add Audience
const audienceGeometry = new THREE.BoxGeometry(0.5, 1.5, 0.5);
const audienceMaterial = new THREE.MeshPhongMaterial({ color: 0xffff00 });
for (let i = -25; i <= 25; i += 2) {
const spectator = new THREE.Mesh(audienceGeometry, audienceMaterial);
spectator.position.set(i, 0.75, -16);
scene.add(spectator);
const spectator2 = spectator.clone();
spectator2.position.z = 16;
scene.add(spectator2);
}
// Feature 42: Add Clouds
function createCloud(x, y, z) {
const cloudGeometry = new THREE.SphereGeometry(2, 32, 32);
const cloudMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
const cloud = new THREE.Mesh(cloudGeometry, cloudMaterial);
cloud.position.set(x, y, z);
scene.add(cloud);
return cloud;
}
const clouds = [];
clouds.push(createCloud(-10, 20, -30));
clouds.push(createCloud(20, 25, -40));
clouds.push(createCloud(-15, 22, -50));
// Feature 43: Animate Clouds
function animateClouds() {
clouds.forEach(cloud => {
cloud.position.x += 0.01;
if (cloud.position.x > 30) cloud.position.x = -30;
});
}
// Modify animate function to include cloud animation
function animate() {
requestAnimationFrame(animate);
// ... existing code ...
animateClouds(); // Animate clouds
renderer.render(scene, camera);
}
// Feature 44: Add Timeout Functionality
let timeLeft = 90; // 90 seconds game time
const timerDisplay = document.createElement('div');
timerDisplay.textContent = 'Time Left: 90';
timerDisplay.style.position = 'absolute';
timerDisplay.style.top = '40px';
timerDisplay.style.left = '10px';
timerDisplay.style.color = 'white';
timerDisplay.style.fontFamily = 'Arial';
timerDisplay.style.fontSize = '20px';
document.body.appendChild(timerDisplay);
// Feature 45: Countdown Timer
function updateTimer() {
if (timeLeft > 0) {
timeLeft -= 1;
timerDisplay.textContent = 'Time Left: ' + timeLeft;
} else {
alert('Game Over! Your Score: ' + score); // Feature 46: Game Over Alert
timeLeft = 90;
score = 0;
scoreboard.textContent = 'Score: ' + score;
}
}
setInterval(updateTimer, 1000);
// Feature 47: Mini-map
const minimapCanvas = document.createElement('canvas');
minimapCanvas.width = 200;
minimapCanvas.height = 120;
minimapCanvas.style.position = 'absolute';
minimapCanvas.style.bottom = '10px';
minimapCanvas.style.right = '10px';
minimapCanvas.style.border = '1px solid #fff';
document.body.appendChild(minimapCanvas);
const minimapContext = minimapCanvas.getContext('2d');
// Feature 48: Update Mini-map
function updateMinimap() {
minimapContext.clearRect(0, 0, minimapCanvas.width, minimapCanvas.height);
minimapContext.fillStyle = '#006600';
minimapContext.fillRect(0, 0, minimapCanvas.width, minimapCanvas.height);
// Draw player
minimapContext.fillStyle = '#0000ff';
minimapContext.fillRect(
((player.position.x + 25) / 50) * minimapCanvas.width - 5,
((player.position.z + 15) / 30) * minimapCanvas.height - 5,
10, 10
);
// Draw ball
minimapContext.fillStyle = '#ffffff';
minimapContext.fillRect(
((ball.position.x + 25) / 50) * minimapCanvas.width - 3,
((ball.position.z + 15) / 30) * minimapCanvas.height - 3,
6, 6
);
}
// Modify animate function to include mini-map update
function animate() {
requestAnimationFrame(animate);
// ... existing code ...
animateClouds();
updateMinimap(); // Update mini-map
renderer.render(scene, camera);
}
// Feature 49: Fullscreen Mode
document.addEventListener('dblclick', () => {
if (!document.fullscreenElement) {
document.body.requestFullscreen();
} else {
document.exitFullscreen();
}
});
// Feature 50: Instructions Overlay
const instructions = document.createElement('div');
instructions.innerHTML = '<strong>Instructions:</strong><br>' +
'- Use arrow keys to move<br>' +
'- Score goals by moving the ball into the goals<br>' +
'- Double-click to toggle fullscreen';
instructions.style.position = 'absolute';
instructions.style.bottom = '10px';
instructions.style.left = '10px';
instructions.style.color = 'white';
instructions.style.fontFamily = 'Arial';
instructions.style.fontSize = '16px';
instructions.style.backgroundColor = 'rgba(0,0,0,0.5)';
instructions.style.padding = '10px';
instructions.style.borderRadius = '5px';
document.body.appendChild(instructions);
// Feature 51: Pause Functionality
let isPaused = false;
window.addEventListener('keydown', function(e) {
if (e.key === 'p') {
isPaused = !isPaused;
}
});
// Modify animate function to include pause functionality
function animate() {
requestAnimationFrame(animate);
if (!isPaused) {
// ... existing game logic ...
animateClouds();
updateMinimap();
renderer.render(scene, camera);
}
}
// Start the game loop
animate();
</script>
</body>
</html>
Features Included:
- Initialize Scene: Set up the 3D scene.
- Camera Setup: Position and configure the camera.
- Renderer Setup: Initialize the WebGL renderer.
- Ambient Lighting: Add soft white light to the scene.
- Directional Lighting: Add a directional light source.
- Football Field Creation: Create the playing field.
- Field Lines: Add a center line to the field.
- Goalposts Creation: Function to create goalposts.
- Left Goal: Add the left goalpost.
- Right Goal: Add the right goalpost.
- Player Sphere: Represent the player as a 3D sphere.
- Football Sphere: Create the football with texture.
- Player Controls: Use arrow keys to move the player.
- Collision Detection: Detect collision between player and ball.
- Score Tracking: Initialize score variables.
- Camera Positioning: Set camera angle for better view.
- Responsive Design: Make the game responsive to window resizing.
- Game Loop: The main animation loop.
- Player Movement: Update player position based on input.
- Boundary Enforcement: Keep the player within the field.
- Ball Movement: Allow the ball to move when kicked.
- Simple Physics: Calculate velocity upon collision.
- Ball Position Update: Update the ball's position.
- Friction Simulation: Slow down the ball over time.
- Velocity Damping: Reduce ball velocity gradually.
- Stop Ball: Stop the ball when velocity is low.
- Ball Boundaries: Keep the ball within the field.
- Goal Detection: Check if the ball enters the goal.
- Increase Score: Increment score when a goal is made.
- Update Scoreboard: Display the current score.
- Reset Ball Position: Reset ball after a goal.
- Reset Ball Function: Function to reset ball state.
Instructions:
- Movement: Use the arrow keys to move the player around the field.
- Scoring: Move the ball into either goal to score points.
- Timer: You have 90 seconds to score as many goals as possible.
- Reset Score: Click the "Reset Score" button to reset your score.
- Pause Game: Press 'P' to pause or resume the game.
- Fullscreen: Double-click anywhere on the screen to toggle fullscreen mode.
Notes:
- Textures and Sounds: The game uses external resources for textures and sounds hosted online. Ensure you have an active internet connection.
- Performance: For the best experience, run the game in a modern desktop browser with good WebGL support.
Enjoy the game!