Typing Game
This section goes over the complete implementation of a keyboard typing game.
DOM Elements
The script starts by initializing references to various DOM elements required for the functionality:
script.js
This script manages keyboard input handling and dynamic UI updates for a typing game. It dynamically updates the display based on user input, calculates typing speed and accuracy, and handles key press animations. The script starts by initializing references to various DOM elements required for the functionality:
NOTES:
theTextBox
: The element where the typed text is displayed.allTheKeys
: The container for the visual representation of the keyboard.cursor
: A visual cursor element in the theTextBox.changeKeys
: A collection of keys that change when Shift is pressed.capsLockKey
: The Caps Lock key element.shiftKey
: The Shift key element.textToType
: The text the user needs to type.
Statistics Elements
Additional elements for displaying typing statistics:
script.js
NOTES:
accuracyElement
: Displays the typing accuracy.timeElement
: Displays the elapsed time.wpmElement
: Displays words per minute (WPM).
Variables
script.js
NOTES:
currentIndex
,correctCount
,startTime
,timerInterval
: General variables used for statisticscurrentIndex
: Tracks the current position in the textToTypecorrectCount
: Counts the number of correctly typed charactersstartTime
: Records the start time of the typing session.timerInterval
: Manages the timer interval for updating the time.
Constants
Defined key codes for better readability:
script.js
NOTES:
SHIFT_KEY_CODE
,CAPS_LOCK_KEY_CODE
,BACKSPACE_KEY_CODE
,TAB_KEY_CODE
: Key codes for special keys.SHIFTER_ARRAY
: Characters to display when Shift is pressed.originalShifterArray
: Stores original values of the shifter keys for restoring after Shift release.
Event Listeners
Event listeners for handling key presses and clicks:
NOTES:
document.addEventListener('keydown', handleKeyPress);
: Event listener to listen for key being pressed downdocument.addEventListener('keyup', handleKeyUp);
: Event listener to listen for when key is releaseddocument.addEventListener('click', clearText);
: Event listener to listen when screen is clcked to reset text
Functions
handleKeyPress()
- Handles key press events and updates the UI
script.js
NOTES:
- Starts the timer on the first key press.
- Highlights the pressed key.
- Handles special keys
Shift
Caps Lock
Backspace
Tab
. - Adds typed characters to the text box and updates the index and correct count.
handleKeyUp()
- Handles key release events:
script.js
NOTES:
- Removes highlighting from the released key.
- Restores original values of shifter keys after Shift release.
addCharacter()
- Adds a character to the text box
script.js
NOTES:
- Creates a span element for the character and inserts it before the cursor.
clearText()
- Clears the text box and resets variables
script.js
NOTES:
- Resets the text box, cursor, index, correct count, and timer.
updateStats()
- Updates accuracy and WPM
script.js
NOTES:
- Calls functions to update accuracy and WPM.
updateWPM()
- Calculates and updates words per minute
script.js
NOTES:
- Calculates WPM based on correct characters and elapsed time.
updateTime()
- Updates the elapsed time
script.js
NOTES:
- Updates the time display every second.
updateAccuracy()
- Calculates and updates accuracy
script.js
NOTES:
- Calculates accuracy based on correct characters and total typed characters.
endGame()
- Ends the game and stops the timer:
script.js
NOTES:
- Clears the timer interval and removes event listeners.
Complet Code
index.html