Okay, this is a substantial project, and I'll provide a foundational HTML, CSS, and JavaScript structure to get you started. It's a complex task to build a fully polished FPS trainer, but this will give you a solid base to expand upon. I'll focus on the core mechanics and the "Doom-like" aesthetic.
Important Considerations:
- Performance: JavaScript-based animation can be demanding. Optimize your code, especially the target movement and hit detection. Consider using
requestAnimationFrame for smoother animations.
- Hit Detection: Precise hit detection is crucial. I'll use a simple bounding box approach, but you might want to explore more advanced techniques (e.g., pixel-perfect collision).
- User Experience: Provide clear visual feedback for hits, misses, and control panel interactions.
- Mobile: This is designed for desktop. Mobile will require significant adaptation.
- Security: Be mindful of security if you plan to allow user input that affects the game state.
Here's the code (HTML, CSS, and JavaScript):
1. HTML (index.html):
<!DOCTYPE html>
<html>
<head>
<title>Doom-Inspired FPS Trainer</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game-container">
<div id="background"></div>
<div id="shooting-range">
<div id="control-panel">
<h2>Control Panel</h2>
<label for="speed">Target Speed:</label>
<input type="range" id="speed" min="1" max="10" value="5">
<span id="speed-value">5</span>
<br>
<button id="clear-targets">Clear Targets</button>
<button id="reset-targets">Reset Targets</button>
<button id="increase-size">Increase Size</button>
<button id="decrease-size">Decrease Size</button>
</div>
<div id="target-area">
<!-- Targets will be dynamically added here -->
</div>
<div id="stats">
<p>Hits: <span id="hit-count">0</span></p>
<p>Accuracy: <span id="accuracy">0.00%</span></p>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. CSS (style.css):
body {
font-family: monospace;
background-color: #111;
color: #eee;
margin: 0;
overflow: hidden; /* Hide scrollbars */
}
#game-container {
width: 100vw;
height: 100vh;
position: relative;
}
#background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('doom_background.jpg'); /* Replace with your image */
background-size: cover;
background-position: center;
filter: grayscale(80%);
z-index: -1;
}
#shooting-range {
position: relative;
width: 80%;
height: 80%;
margin: 10% auto;
border: 2px solid #888;
padding: 20px;
background-color: rgba(0, 0, 0, 0.7);
}
#control-panel {
position: absolute;
top: 0;
left: 0;
width: 200px;
padding: 10px;
background-color: rgba(0, 0, 0, 0.8);
border: 1px solid #555;
}
#target-area {
position: relative;
width: 100%;
height: 70%;
overflow: hidden; /* Prevent targets from overflowing */
}
.target {
position: absolute;
width: 30px;
height: 30px;
background-color: red;
border-radius: 50%;
cursor: crosshair;
}
#stats {
position: absolute;
bottom: 0;
left: 0;
padding: 10px;
background-color: rgba(0, 0, 0, 0.8);
border: 1px solid #555;
}
3. JavaScript (script.js):
const targetArea = document.getElementById('target-area');
const hitCountElement = document.getElementById('hit-count');
const accuracyElement = document.getElementById('accuracy');
const speedControl = document.getElementById('speed');
const speedValueElement = document.getElementById('speed-value');
const clearTargetsButton = document.getElementById('clear-targets');
const resetTargetsButton = document.getElementById('reset-targets');
const increaseSizeButton = document.getElementById('increase-size');
const decreaseSizeButton = document.getElementById('decrease-size');
let hitCount = 0;
let shotsFired = 0;
let targets = [];
let targetSize = 30;
let targetSpeed = 5;
function createTarget() {
const target = document.createElement('div');
target.classList.add('target');
target.style.width = targetSize + 'px';
target.style.height = targetSize + 'px';
const randomX = Math.random() * (targetArea.offsetWidth - targetSize);
const randomY = Math.random() * (targetArea.offsetHeight - targetSize);
target.style.left = randomX + 'px';
target.style.top = randomY + 'px';
target.addEventListener('click', handleHit);
targetArea.appendChild(target);
targets.push(target);
}
function handleHit(event) {
const target = event.target;
hitCount++;
shotsFired++;
updateStats();
target.remove();
targets = targets.filter(t => t !== target); // Remove from array
createTarget(); // Create a new target
}
function updateStats() {
hitCountElement.textContent = hitCount;
const accuracy = (hitCount / shotsFired) * 100;
accuracyElement.textContent = accuracy.toFixed(2) + '%';
}
function clearTargets() {
targets.forEach(target => target.remove());
targets = [];
}
function resetTargets() {
clearTargets();
for (let i = 0; i < 5; i++) { // Create 5 initial targets
createTarget();
}
}
function increaseSize() {
targetSize += 5;
targets.forEach(target => {
target.style.width = targetSize + 'px';
target.style.height = targetSize + 'px';
});
}
function decreaseSize() {
if (targetSize > 10) {
targetSize -= 5;
targets.forEach(target => {
target.style.width = targetSize + 'px';
target.style.height = targetSize + 'px';
});
}
}
// Event Listeners
speedControl.addEventListener('input', () => {
targetSpeed = parseInt(speedControl.value);
speedValueElement.textContent = targetSpeed;
});
clearTargetsButton.addEventListener('click', clearTargets);
resetTargetsButton.addEventListener('click', resetTargets);
increaseSizeButton.addEventListener('click', increaseSize);
decreaseSizeButton.addEventListener('click', decreaseSize);
// Initial Setup
resetTargets();
// Target Movement (using setInterval)
setInterval(() => {
targets.forEach(target => {
let x = parseFloat(target.style.left);
let y = parseFloat(target.style.top);
// Simple random movement
const dx = (Math.random() - 0.5) * targetSpeed;
const dy = (Math.random() - 0.5) * targetSpeed;
let newX = x + dx;
let newY = y + dy;
// Keep targets within the target area
newX = Math.max(0, Math.min(newX, targetArea.offsetWidth - targetSize));
newY = Math.max(0, Math.min(newY, targetArea.offsetHeight - targetSize));
target.style.left = newX + 'px';
target.style.top = newY + 'px';
});
}, 20); // Adjust interval for speed
Key Improvements and Explanations:
- Doom Aesthetic: The CSS uses a dark color scheme, monospace font, and suggests a background image (
doom_background.jpg). You'll need to find a suitable image. Grayscale filter adds to the gritty feel.
- Control Panel: The HTML includes a control panel with a slider for target speed, buttons for clearing, resetting, and changing target size.
- Dynamic Targets: Targets are created and removed dynamically using JavaScript.
- Hit Detection: The
handleHit function is called when a target is clicked. It increments the hit count, shots fired, and updates the stats.
- Stats Tracking: The code tracks hit count and accuracy.
- Target Movement: The
setInterval function moves the targets randomly within the target area.
- Target Size Control: The size of the targets can be adjusted using the buttons.
- Clear/Reset Functionality: The buttons clear or reset the targets.
- Comments: I've added comments to explain the code.
To Run This:
- Save the files: Save the HTML as
index.html, the CSS as style.css, and the JavaScript as script.js in the same directory.
- Get a background image: Find a suitable Doom-like background image and save it as
doom_background.jpg in the same directory.
- Open in a browser: Open
index.html in your web browser.
Next Steps (Expanding the Trainer):
- More Realistic Movement: Implement more sophisticated target movement patterns (e.g., sinusoidal, circular, random walks).
- Difficulty Levels: Add difficulty levels that adjust target speed, size, and number of targets.
- Scoring System: Implement a scoring system based on accuracy and speed.
- Sound Effects: Add sound effects for hits, misses, and menu interactions.
- Crosshair: Add a crosshair to the center of the screen.
- Weapon Selection: Allow the user to select different "weapons" (which could affect the size of the crosshair or the hit detection area).
- Timer: Add a timer to track the training session duration.
- High Scores: Store and display high scores.
- Optimization: Profile the code and optimize performance, especially for target movement and hit detection. Consider using
requestAnimationFrame instead of setInterval for smoother animations.
- Mobile Responsiveness: Adapt the layout and controls for mobile devices.
This is a starting point. Building a full-fledged FPS trainer is a significant undertaking, but this provides a solid foundation to build upon. Good luck! Let me know if you have any specific questions as you develop it further.