Make a 3D shooter game in HTML based on the olympics. The target should be 30m away, and…
- User
Make a 3D shooter game in HTML based on the olympics. The target should be 30m away, and there should be a round and score system.
- Assistant
Make a 3D shooter game in HTML based on the olympics. The target should be 30m away, and there should be a round and score system.
Here's a simple 3D shooter game implemented in HTML using Three.js—a popular 3D library for JavaScript. In this game, you aim to hit a target placed 30 meters away. The game includes rounds and a scoring system.
Note: To run this game, you'll need to have internet access to load the Three.js library from a CDN, or you can download the library and include it locally.
Create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Olympic 3D Shooter Game</title>
<style>
body { margin: 0; overflow: hidden; }
#scoreboard {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-family: Arial, sans-serif;
font-size: 18px;
background: rgba(0, 0, 0, 0.5);
padding: 10px;
border-radius: 5px;
}
#instructions {
position: absolute;
bottom: 10px;
left: 10px;
color: white;
font-family: Arial, sans-serif;
font-size: 16px;
background: rgba(0, 0, 0, 0.5);
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="scoreboard">Score: 0 | Round: 1</div>
<div id="instructions">Click to shoot at the target.</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.154.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.154.0/examples/js/controls/PointerLockControls.js"></script>
<script src="game.js"></script>
</body>
</html>
Create a game.js file in the same directory with the following content:
// Variables for Three.js
let scene, camera, renderer;
let controls;
let target;
let raycaster;
let score = 0;
let round = 1;
let totalRounds = 5;
// Initialize the Scene
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Sky blue background
// Camera
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(0, 1.6, 0); // Human eye level height
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
const light = new THREE.HemisphereLight(0xffffff, 0x444444, 1.0);
light.position.set(0, 200, 0);
scene.add(light);
// Ground
const groundGeometry = new THREE.PlaneGeometry(100, 100);
const groundMaterial = new THREE.MeshPhongMaterial({ color: 0x228B22 });
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
// Target
createTarget();
// Controls
controls = new THREE.PointerLockControls(camera, renderer.domElement);
document.addEventListener('click', () => {
controls.lock();
}, false);
// Raycaster for shooting
raycaster = new THREE.Raycaster();
// Event Listener for Shooting
document.addEventListener('click', onShoot, false);
// Handle Window Resize
window.addEventListener('resize', onWindowResize, false);
animate();
}
// Function to Create the Target
function createTarget() {
if (target) scene.remove(target);
const targetGeometry = new THREE.CircleGeometry(1, 32);
const targetMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
target = new THREE.Mesh(targetGeometry, targetMaterial);
target.position.set(0, 1.6, -30); // 30 meters away
target.rotation.y = Math.PI;
scene.add(target);
}
// Shooting Function
function onShoot() {
if (!controls.isLocked) return;
// Update the raycaster
raycaster.set(camera.position, camera.getWorldDirection(new THREE.Vector3()));
// Check if the target is hit
const intersects = raycaster.intersectObject(target);
if (intersects.length > 0) {
// Hit
score += 10;
updateScoreboard();
nextRound();
} else {
// Miss
nextRound();
}
}
// Proceed to the Next Round
function nextRound() {
round++;
if (round > totalRounds) {
endGame();
} else {
createTarget();
updateScoreboard();
}
}
// Update Scoreboard UI
function updateScoreboard() {
const scoreboard = document.getElementById('scoreboard');
scoreboard.textContent = `Score: ${score} | Round: ${round > totalRounds ? totalRounds : round}`;
}
// End Game Function
function endGame() {
document.removeEventListener('click', onShoot, false);
const instructions = document.getElementById('instructions');
instructions.textContent = `Game Over! Final Score: ${score}`;
}
// Handle Window Resize
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Animation Loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
// Start the Game
init();
Open the index.html file in a modern web browser (like Chrome or Firefox). Ensure that you have a local web server running if your browser blocks local file executions.
Enjoy your Olympic 3D Shooter Game!