cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="messages">
  <div id="alert-box">
    <div id="alert">
      <h2 id="alert-copy">YOU WIN!</h2>
    </div>
  </div>
</div>

<div class="container" id="game">
  <div class="row">
    <div class="col-xs-12" id="holder">
      <h1 id="theWord"></h1>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-4">
      <img src="http://www.mikemorkes.com/codepen/hangman/hangman1.png" class="img-responsive" alt="hangman art" id="img">
    </div>
    <div class="col-xs-7">
      <div id="form">
        <form>
          <div class="form-group">
            <div class="container">
              <div class="row">
                <div class="col-sm-12">
                  <h3>GUESS A LETTER</h3></div>
              </div>
              <div class="row">
                <div id="letters" class="col-sm-7">
                <button type="button" class="btn btn-default letterButtons" id="A">A</button>
                <button type="button" class="btn btn-default letterButtons" id="B">B</button>
                <button type="button" class="btn btn-default letterButtons" id="C">C</button> 
                <button type="button" class="btn btn-default letterButtons" id="D">D</button> 
                <button type="button" class="btn btn-default letterButtons" id="E">E</button>  
                <button type="button" class="btn btn-default letterButtons" id="F">F</button>
                <button type="button" class="btn btn-default letterButtons" id="G">G</button>
                <button type="button" class="btn btn-default letterButtons" id="H">H</button> 
                <button type="button" class="btn btn-default letterButtons" id="I">I</button> 
                <button type="button" class="btn btn-default letterButtons" id="J">J</button>    
                <button type="button" class="btn btn-default letterButtons" id="K">K</button>
                <button type="button" class="btn btn-default letterButtons" id="L">L</button>
                <button type="button" class="btn btn-default letterButtons" id="M">M</button> 
                <button type="button" class="btn btn-default letterButtons" id="N">N</button> 
                <button type="button" class="btn btn-default letterButtons" id="O">O</button>  
                <button type="button" class="btn btn-default letterButtons" id="P">P</button>
                <button type="button" class="btn btn-default letterButtons" id="Q">Q</button>
                <button type="button" class="btn btn-default letterButtons" id="R">R</button> 
                <button type="button" class="btn btn-default letterButtons" id="S">S</button> 
                <button type="button" class="btn btn-default letterButtons" id="T">T</button>     
                <button type="button" class="btn btn-default letterButtons" id="U">U</button>  
                <button type="button" class="btn btn-default letterButtons" id="V">V</button>
                <button type="button" class="btn btn-default letterButtons" id="W">W</button>
                <button type="button" class="btn btn-default letterButtons" id="X">X</button> 
                <button type="button" class="btn btn-default letterButtons" id="Y">Y</button> 
                <button type="button" class="btn btn-default letterButtons" id="Z">Z</button> 
                </div>
              </div>
              <div class="row">
                <div class="col-sm-12">
                  <p id="guessedLetters">&nbsp;</p><br />
                  <button type="submit" class="btn btn-danger" id="reset">RESET GAME</button>
                </div>
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
    <div class="col-xs-1"></div>
  </div>
  <div class="row">
    <div class="col-xs-2"></div>
    <div class="col-xs-8">

    </div>
    <div class="col-xs-2"></div>
  </div>
</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Maiden+Orange');

body {
  font-family: 'Maiden Orange', serif;
  color: #1f0902;
  background: #c3a583 url('http://www.mikemorkes.com/codepen/hangman/parchment.jpg');
  background-position: 0 0;
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;  
}

#holder {
  text-align: center;
  color: #221003;
  padding: 50px 0;
}

#theWord {
  font-family: 'Maiden Orange', serif;
  font-size: 70px;
}

.form-group {
  width: 100%;
}

.float {
  float: left;
  margin-right: 20px;
}

#letterGuess {
  padding-bottom: 20px;
  height: 45px;
  width: 55px;
  font-size: 40px;
  margin-right: 10px;
}

h3 {
  font-size: 30px;
}

#messages {
  text-align: center;
  display: none;
}

#alert-box {
  position: absolute;
  top: 100px;
  left: 50%;
  z-index: 100000; 
}

#alert {
  color: #ffffff;
  position: relative;
  left: -50%;
  background: url('http://www.mikemorkes.com/codepen/hangman/alert-parchment.png');
  background-position: 0 0;
  background-repeat: no-repeat;
  padding: 100px 50px 50px;
  width: 640px;
  height: 439px;
}

#alert-copy {
  font-size: 90px;
}

#submit {
  background: #1f0902;
  border: none;
}

.letterButtons {
  background-color: #351308 !important;
  color: #fff;
  font-size: 20px;
  border: none !important;
  margin-top: 10px;
  margin-right: 5px;
  width: 50px;
  height: 45px;  
}

.letterButtonsGuessed {
  background: #a4908a !important;
  border: none !important;
  margin-top: 10px;
  margin-right: 5px;
  width: 50px;
  height: 45px;
}

input:focus,
I select:focus,
textarea:focus,
button:focus {
	outline: none !important;
  background-color: #351308;
  color: #fff !important;
}
            
          
!
            
              $(document).ready(function () {
  preload([
    'http://www.mikemorkes.com/codepen/hangman/parchment.jpg',
    'http://www.mikemorkes.com/codepen/hangman/alert-parchment.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman1.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman2.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman3.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman4.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman5.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman6.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman7.png',
    'http://www.mikemorkes.com/codepen/hangman/hangman8.png'
  ]);
});

var word;
var wordArray;
var guesses = [];
var hangmanParts = 1;
var joinLetters;
var joinLettersGapped;
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

//preload images
function preload(images) {
    $(images).each(function(){
        $('<images/>')[0].src = this;
    });
}

// grab first random word and set up board
grabWord();


// function that runs when letter is guessed. 
function guessLetter(letterValue) {  
  var pos = word.indexOf(letterValue);
  
  if (pos == -1) {
    hangmanParts++;
    document.getElementById("img").src = "http://www.mikemorkes.com/codepen/hangman/hangman" + hangmanParts + ".png";
    checkLose();   
  } else {
    for (b = 0; b < word.length; b++) {
      if (wordArray[b] == letterValue) {
        guesses[b] = letterValue;
      }
    }
    join();
    
  }  

}

// function to randomly pick word from database at start of each round
function grabWord() {
      $.getJSON("https://raw.githubusercontent.com/jacksonrayhamilton/wordlist-english/master/english-words.json", function(json) {
        
        var randNum = Math.floor(Math.random() * (107566 - 1 + 1));
        
        word = json[randNum].toUpperCase();
        console.log(word);
        
        wordArray = [...word];
        
        underScores();
        join();

      });  
  
}

// function to find random word's length and display correct # of underscores at top for each game
function underScores() {
  guesses = [];

  for (a = 0; a < word.length; a++) {
    guesses.push("_");  
  }

}

// function to redraw the word when a letter is correctly guessed and run checkWin
function join() {
  joinLettersGapped = guesses.join(" ");
  joinLetters = guesses.join("");
  document.getElementById("theWord").innerHTML = joinLettersGapped;
  checkWin();
}

// function to check for win when a letter is correctly guessed
function checkWin() {
  if (word == joinLetters) {
    document.getElementById("alert-copy").innerHTML = "YOU'RE A WINNER!";
    document.getElementById("messages").style.display = "block";
    setTimeout(function() {
      document.getElementById("messages").style.display = "none";
      resetAll();
    }, 2000);
  } else {}

}


// function to check for a full hangman when letter guess is wrong, and reset if full hangman
function checkLose() {
  if (hangmanParts == 8) {
    document.getElementById("alert-copy").innerHTML = "NOOSE FOR YOU!";
    setTimeout(function() {
      document.getElementById("messages").style.display = "block";
    }, 750);      
    setTimeout(function() {
      document.getElementById("messages").style.display = "none";
      resetAll();
    }, 2750);
  }
}

// function to switch buttons to guessed state when pressed
function guessedLetter(buttonID) {
  console.log(buttonID);
  document.getElementById(buttonID).className = "btn btn-success btn-lg letterButtonsGuessed";
}



//buttons

function letterHolder(guess) {
  guessLetter(guess);
  guessedLetter(guess); 
  //prevent browser's default action
  evt.preventDefault();  
}

$('#A').click(function(evt) {
  letterHolder("A");
}); //end A button 

$('#B').click(function(evt) {
    letterHolder("B");
}); //end B button 

$('#C').click(function(evt) {
    letterHolder("C");
}); //end C button 

$('#D').click(function(evt) {
    letterHolder("D");
}); //end D button 

$('#E').click(function(evt) {
    letterHolder("E");
}); //end E button 

$('#F').click(function(evt) {
    letterHolder("F");
}); //end F button 

$('#G').click(function(evt) {
    letterHolder("G");
}); //end G button 

$('#H').click(function(evt) {
    letterHolder("H");
}); //end H button 

$('#I').click(function(evt) {
    letterHolder("I");
}); //end I button 

$('#J').click(function(evt) {
    letterHolder("J");
}); //end J button 

$('#K').click(function(evt) {
    letterHolder("K");
}); //end K button 

$('#L').click(function(evt) {
    letterHolder("L");
}); //end L button 

$('#M').click(function(evt) {
    letterHolder("M");
}); //end M button 

$('#N').click(function(evt) {
    letterHolder("N");
}); //end N button 

$('#O').click(function(evt) {
    letterHolder("O");
}); //end O button 

$('#P').click(function(evt) {
    letterHolder("P");
}); //end P button 

$('#Q').click(function(evt) {
    letterHolder("Q");
}); //end Q button 

$('#R').click(function(evt) {
    letterHolder("R");
}); //end R button 

$('#S').click(function(evt) {
    letterHolder("S");
}); //end S button 

$('#T').click(function(evt) {
    letterHolder("T");
}); //end T button 

$('#U').click(function(evt) {
    letterHolder("U");
}); //end U button 

$('#V').click(function(evt) {
    letterHolder("V");
}); //end V button 

$('#W').click(function(evt) {
    letterHolder("W");
}); //end W button 

$('#X').click(function(evt) {
    letterHolder("X");
}); //end X button 

$('#Y').click(function(evt) {
    letterHolder("Y");
}); //end W button 

$('#Z').click(function(evt) {
    letterHolder("Z");
}); //end X button 


$('#reset').click(function(evt) {

  resetAll();

  //prevent browser's default action
  evt.preventDefault();
}); //end submit button 


// function to reset game
function resetAll() {
  // RESET ILLUSTRATION
  document.getElementById("img").src = "http://www.mikemorkes.com/codepen/hangman/hangman1.png";
  
  // RESET THE LETTER BUTTONS 
  for (var i=0; i<alphabet.length; i++) {
    var letter = alphabet[i];
    document.getElementById(letter).className = "btn btn-default btn-lg letterButtons";
  }
    
  hangmanParts = 1;
  
  grabWord();
    
  for (a = 0; a < word.length; a++) {
    guesses[a] = "_";
  }  
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console