void keyPressed() {
if (gameStarted) {
char keyPressed = Character.toUpperCase(key);
for (int i = 0; i < colorKeys.length; i++) {
if (keyPressed == colorKeys[i]) {
if (keyPressed == getColorChar(circleFillColor)) {
// If player presses the correct key corresponding to the displayed color
points++;
missedColors = 0; // Reset missed colors
gameStarted = false; // Set to false to refresh the color
pickRandomColor(); // Choose a new random color
} else {
missedColors++;
if (missedColors >= 3) {
gameOver();
}
}
break; // Exit loop after handling input
}
}
} else {
// Start the game when the ENTER key is pressed
if (keyCode == ENTER) {
gameStarted = true;
pickRandomColor(); // Choose a random color to start
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter