HTML and CSS Reference
In-Depth Information
The initGame() Function
The initGame() function sets up the game for the player. The two most important blocks of
code are as follows. This code finds a random letter from the letters array and stores it in the
letterToGuess variable:
var
var letterIndex = Math . floor ( Math . random () * letters . length );
letterToGuess = letters [ letterIndex ];
This code adds an event listener to the window object of the DOM to listen for the keyboard
keydown event. When a key is pressed, the eventKeyPressed event handler is called to test
the letter pressed:
window . addEventListener ( "keydown" , eventKeyPressed , true
true );
Here is the full code for the function:
function
function initGame () {
var
var letterIndex = Math . floor ( Math . random () * letters . length );
letterToGuess = letters [ letterIndex ];
guesses = 0 ;
lettersGuessed = [];
gameOver = false
false ;
window . addEventListener ( "keydown" , eventKeyPressed , true
true );
drawScreen ();
}
Search WWH ::




Custom Search