:

hangman

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 /* VARIABLES */ var alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var guessword = "HELLO"; var hint = "salutation"; var max = 6; var wrong = 0; var done = 0; /* GAME ALGORITHM*/ $(document).ready(function(){ $('.alfa').click(function(){ var selected = $(this).attr('id'); //alert(selected); for(x = 0; x < guessword.length; x++){ if(guessword.charAt(x) == selected){ $('#' + x).fadeOut(200, function() { $(this).text(selected); }).fadeIn(200); $(this).css('background', 'green'); done++; } } if(guessword.indexOf(selected)== -1){ $(this).css('background', 'red'); wrong++; // update the image of gallows $('#gallows').fadeOut(300, function() { $(this).load(function() { $(this).fadeIn(300); }); $(this).attr('src', './images/_' + wrong + '.gif'); }); // update the number of wrong guesses left; $('#wrong').text((max - wrong) + ' tries left'); } if(done == guessword.length){ //showGameWin(); } if(wrong == max){ //showGameLose(); } }); }); /* FUNCTIONS */ function showGallows(){ document.write('
'); } function showWrongN(){ document.write('
' + (max - wrong) + ' tries left
'); } function showHint(){ document.write('
' + hint + '
'); } function showGuessArea(){ for(var x = 0; x < guessword.length; x++){ if(guessword.charAt(x) == ' '){ document.write('
  
'); }else{ document.write('
');; } } document.write('
'); } function showAlfa(){ document.write('
'); for(var x = 0; x < alfa.length; x++){ if(x == 13){ document.write('

'); } document.write('
' + alfa[x] + '
'); } document.write('
') } function startGame(w,h){ guessword = w.toUpperCase(); hint = h; showGallows(); showWrongN(); showHint(h) showGuessArea() showAlfa(); }
Author:Bling King
Published:Jul 3rd 2014
Modified:Jul 3rd 2014

This website is powered by Spruz