Below is a simple implementation of a 4-key rhythm game using HTML, CSS, and JavaScript in a single file. This game uses the keys D, F, J, and K (or the arrow keys) for input. It includes a basic score system, accuracy tracking, and a miss counter.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rhythm Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
.key {
display: inline-block;
width: 50px;
height: 50px;
margin: 10px;
border: 1px solid black;
border-radius: 10px;
line-height: 50px;
font-size: 24px;
cursor: pointer;
}
.key:hover {
background-color: #ccc;
}
.note {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: red;
transition: top 1s;
}
</style>
</head>
<body>
<h1>Rhythm Game</h1>
<div id="score">Score: 0</div>
<div id="accuracy">Accuracy: 0%</div>
<div id="misses">Misses: 0</div>
<div id="keys">
<div class="key" id="key1">D</div>
<div class="key" id="key2">F</div>
<div class="key" id="key3">J</div>
<div class="key" id="key4">K</div>
</div>
<script>
let score = 0;
let accuracy = 0;
let misses = 0;
let totalNotes = 0;
let notes = [];
let keybinds = {
'd': 0,
'f': 1,
'j': 2,
'k': 3,
'ArrowUp': 0,
'ArrowDown': 1,
'ArrowLeft': 2,
'ArrowRight': 3
};
document.addEventListener('keydown', (e) => {
if (e.key in keybinds) {
checkNote(keybinds[e.key]);
}
});
function createNote(key) {
let note = document.createElement('div');
note.className = 'note';
note.style.left = (key * 70 + 50) + 'px';
note.style.top = '0px';
document.body.appendChild(note);
notes.push({ note, key });
}
function updateNotes() {
for (let i = notes.length - 1; i >= 0; i--) {
let note = notes[i].note;
let top = note.offsetTop;
note.style.top = (top + 5) + 'px';
if (top > window.innerHeight) {
document.body.removeChild(note);
notes.splice(i, 1);
misses++;
updateMisses();
}
}
requestAnimationFrame(updateNotes);
}
function checkNote(key) {
for (let i = notes.length - 1; i >= 0; i--) {
if (notes[i].key === key && notes[i].note.offsetTop > window.innerHeight * 0.7) {
document.body.removeChild(notes[i].note);
notes.splice(i, 1);
score++;
totalNotes++;
updateScore();
updateAccuracy();
return;
}
}
}
function updateScore() {
document.getElementById('score').innerText = 'Score: ' + score;
}
function updateAccuracy() {
if (totalNotes === 0) {
accuracy = 0;
} else {
accuracy = ((score / totalNotes) * 100);
}
document.getElementById('accuracy').innerText = 'Accuracy: ' + accuracy.toFixed(2) + '%';
}
function updateMisses() {
document.getElementById('misses').innerText = 'Misses: ' + misses;
}
updateNotes();
setInterval(() => {
let key = Math.floor(Math.random() * 4);
createNote(key);
totalNotes++;
}, 1000);
</script>
</body>
</html>
This code creates a simple rhythm game where notes fall from the top of the screen, and the player must press the corresponding key (D, F, J, K, or the arrow keys) to hit the note. The game tracks the player's score, accuracy, and number of misses. The accuracy is calculated based on the number of successful hits out of the total number of notes that have fallen.