Hangman Game
Description: Learn how to make the classic letter guessing game
Author: John Bezanis
Added: October 1st 2008
Version: Flash 8
Hangman is a popular game which can be coded using a few symbols and a short amount of code. The object of the game is to reveal a word by guessing the letters of the word one by one. The player loses by guessing 5 incorrect letters. A random answer is selected at the start of each game
Start by creating the movie clip that holds the graphics for the hangman. Set the name to missgraphic and the Linkage Identifier to missgraphic. Select Export for Actionscript.

On the first frame, draw the frame and hang a noose from it. In the actions section, add stop(); For frames 2-7, add one piece to the hangman. Return to the main stage. Delete the symbol from the stage, keeping it in the library.

Next we are going to create the buttons which contain the letters for the player to guess. Draw a square with rounded corners. Copy and paste the rectangle. Change the color to white and Modify->Shape->Expand Fill. Set the inset to 5px. Add a text box with type dynamic text. Set the Var to displayletter. Return to the main stage and delete the movieclip from the stage. In the library, select the movieclip, right click Export for Actionscript and set the Linkage Identifier to displayletter.

Next comes the blocks that hold the display of the answer. Start by drawing a 14x2 rectangle. Convert it to a movieclip named letterholder. Move the rectangle to the middle horizontally. Create a dynamic text box. Set the instance name to letter and Var to displayletter. Move back to the main stage and delete the movie clip. In the library, set letterholder's linkage identifier to letterholder and select Export for Actionscript.

The last symbol is the playagain button. Write the text and convert it to a button named playagain. I like to draw a transparent box on a layer below the text to make it easier to click. On frame 2, insert a new keyframe and set the color of the text a bit lighter than the up frame. Return to the main stage and delete the button from the stage. Set the button to Export for Actionscript and set the linkage identifier to playagain.

Now that all of the graphics are done, we can start writing the code. All of the code is located on the main stage. Start by creating an array that stores all of the answers and attaching the missgraphic to the stage.
- //Array containing all of the answers
- gameanswers = ['Frosted Flakes', 'Cheerios', 'Honey Combs', 'Lucky Charms', 'Honey Bunches of Oats', 'Apple Jacks', 'Rice Krispies', 'Fruit Loops', 'Count Chocula', 'Boo Berry'];
- //Attach the missgraphic
- this.attachMovie('missgraphic','missgraphic',this.getNextHighestDepth(), {_x:175,_y:10});
- //Function that begins a new game
- function newGame() {
- //select a random answer from the array
- answer = gameanswers[Math.floor(Math.random()*gameanswers.length)];
- //Add all 26 letter buttons to the stage
- for (curindex=0; curindex<26; curindex++) {
- //Use the character code for each letter to attach the letter
- attachMovie('guessletter', 'guess'+String.fromCharCode(curindex+65), this.getNextHighestDepth());
- //set the x position according to its position in the alphabet
- eval('guess'+String.fromCharCode(curindex+65))._x = (curindex%13-6.5)*(eval('guess'+String.fromCharCode(curindex+65))._width+3)+Stage.width/2;
- //set the y position according to its position in the alphabet
- eval('guess'+String.fromCharCode(curindex+65))._y = Stage.height-(2-Math.floor(curindex/13))*(eval('guess'+String.fromCharCode(curindex+65))._height+3);
- //set the character display to the current letter in the loop
- eval('guess'+String.fromCharCode(curindex+65)).displayletter = String.fromCharCode(curindex+65);
- //When the player clicks the button, call the pressLetter function with the letter set
- eval('guess'+String.fromCharCode(curindex+65)).onPress = function() {
- //process the letter
- pressLetter(this.displayletter);
- //remove the button from the stage
- removeMovieClip(this);
- };
- }
- //Determine how many characters are going to be printed on each line.
- //Keep the letters of words from being split onto different lines
- //start the row count at 0
- curline = 0;
- //the start position in the answer is 0
- linestart = 0;
- //store the length of each line into an array
- var linelengths:Array = new Array();
- //traverse all of the characters in the answer
- for (linepos=0; linepos<length(answer); linepos++) {
- //Maximum length of each formatted line
- blanklinelength = 25;
- //check if the linelength is smaller than the max line length or the first word on the line is longer than the max
- if ((linepos-linestart)<blanklinelength || linelengths[curline] == undefined) {
- //if the current character is a space, set a marker so we know how long the line should be
- //it may be updated if another word fits within the current line
- if (answer.charAt(linepos) == ' ') {
- //update the length of the current line
- linelengths[curline] = linepos-linestart+1;
- }
- //else the line has reached its limit, so move to the next line and start at the end of the last word
- } else {
- //store where to start the next line
- linestart = linestart+linelengths[curline];
- //move to that position
- linepos = linestart;
- //move to the next line
- curline++;
- }
- }
- //set the length of the last line
- linelengths[curline] = length(answer)-linestart;
- //Now we are going to add the blank holders to the stage
- //start at the first row
- currow = 0;
- //set the position to 0
- curpos = 0;
- //Loop through the answer and add the blank holders to the stage
- for (curindex=0; curindex<length(answer); curindex++) {
- //If the current position is a space, do not create a blank holder
- if (answer.charAt(curindex) != ' ') {
- //add the letter holder
- attachMovie('letterholder', 'holder'+curindex, this.getNextHighestDepth());
- //set the x position relative to its position on the line and the number of characters on the line
- eval('holder'+curindex)._x = Stage.width/2+((curpos-(linelengths[currow]/2))*20);
- //set the y position according to the currow
- eval('holder'+curindex)._y = 280+(currow-(linelengths.length/2))*20;
- //if the current character is not A-Z, display it
- if (!hiddenCharacter(answer.charAt(curindex))) {
- //display the character, since it is a special character
- eval('holder'+curindex).displayletter = answer.charAt(curindex);
- }
- }
- //If we are at the end of a row, go to the next row
- if (++curpos>=linelengths[currow]) {
- //move to the next row
- currow++;
- //reset the position to the left
- curpos = 0;
- }
- }
- }
- //This function checks if the input character is an A-Z character
- function hiddenCharacter(curchar) {
- //Set of characters that aren't revealed
- hiddenchars = 'abcdefghijklmnopqrstuvwxyz';
- //Loop through the character set
- for (charindex=0; charindex<length(hiddenchars); charindex++) {
- //If the input character is in the list, hide it
- if (curchar.toLowerCase() == hiddenchars.charAt(charindex)) {
- return true;
- }
- }
- //character is not A-Z, so display it
- return false;
- }
- //function called each time a letter button is pressed
- function pressLetter(pressedletter) {
- //check if the pressed letter is in the answer
- if (!inAnswer(pressedletter)) {
- //update the graphic to the next image
- missgraphic.gotoAndStop(missgraphic._currentframe+1);
- //check if the last frame has been reached
- if (missgraphic._currentframe == missgraphic._totalframes) {
- //game lost. remove all of the remaining buttons
- for (curindex=0; curindex<26; curindex++) {
- //if the letter button exists, remove it
- if (eval('guess'+String.fromCharCode(curindex+65))) {
- //delete the button. 65 is the ascii character A
- removeMovieClip('guess'+String.fromCharCode(curindex+65));
- }
- }
- //the game has been lost, so fill in all of the blank spaces with the answers in red
- for (curindex=0; curindex<length(answer); curindex++) {
- //if there is a space or the letter has been guessed already do nothing
- if (answer.charAt(curindex) != ' ' && eval('holder'+curindex).displayletter == undefined) {
- //the letter has not been guesed, so reveal it and make it red
- eval('holder'+curindex).displayletter = answer.charAt(curindex);
- eval('holder'+curindex).letter.textColor = '0xFF0000';
- }
- }
- //Display the button to start a new game
- showPlayAgainButton();
- }
- }
- }
- function inAnswer(pressedletter) {
- //initialize goodletter to false.
- //we are going to loop through the answer to see if the guessed letter is in the answer
- goodletter = false;
- //check if there are any letters in the answer that haven't been guessed yet
- blankspace = 0;
- //loop through the answer
- for (curindex=0; curindex<length(answer); curindex++) {
- //check if the guessed letter is the character in the current position of the answer
- if (answer.charAt(curindex).toLowerCase() == pressedletter.toLowerCase()) {
- //display the character
- eval('holder'+curindex).displayletter = answer.charAt(curindex);
- //setting goodletter to true prevents the image from going to the next frame
- goodletter = true;
- //if the character at the current position hasn't been guessed, reveal it
- } else if (answer.charAt(curindex) != ' ' && eval('holder'+curindex).displayletter == undefined) {
- //there is at least one letter that hasn't been guessed
- blankspace = 1;
- }
- }
- //if every letter has been guessed, the player wins
- if (!blankspace) {
- //remove all of the guess buttons
- for (curindex=0; curindex<26; curindex++) {
- //if the button at the current position exists, remove it
- if (eval('guess'+String.fromCharCode(curindex+65))) {
- removeMovieClip('guess'+String.fromCharCode(curindex+65));
- }
- }
- //loop through all of the characters and set their color to green
- for (curindex=0; curindex<length(answer); curindex++) {
- //if the current character isn't a space, set its color to green
- if (answer.charAt(curindex) != ' ') {
- eval('holder'+curindex).letter.textColor = '0x00FF00';
- }
- }
- //show the play again button
- showPlayAgainButton();
- }
- //return whether or not the guessed letter exists in the answer
- return (goodletter);
- }
- //Show the button to play again
- function showPlayAgainButton() {
- //attach the button
- attachMovie('playagain', 'playagain', this.getNextHighestDepth());
- //move it to the middle of the screen
- playagain._x = Stage.width/2;
- //move it towards the bottom
- playagain._y = 370;
- //Add a listener for when the button is pressed. When pressed, start a new game
- playagain.onPress = function() {
- //Reset the hangman graphic to the first frame
- missgraphic.gotoAndStop(1);
- //loop through all of the letter holders of the answer on the screen and delete them
- for (curindex=0; curindex<length(answer); curindex++) {
- //if the current position in the answer isn't a blank, remove it
- if (answer.charAt(curindex) != ' ') {
- removeMovieClip('holder'+curindex);
- }
- }
- //start a new game
- newGame();
- //delete this button
- removeMovieClip('playagain');
- };
- }
- newGame();
Download Source File
Download Demo SWF
Comments
Thanks for the post. I created another Hangman game in Flex on http://www.guessroom.com
November 17th 2008 03:11PM - Erwin Ravau
Hey thanks for the post! I had to make a game for homework due in a few weeks and this made it so easy.
November 22nd 2008 11:11AM - El Taco
its a funny game. but i think the player should get some hint about the phrase. there must be some provision in the script
December 1st 2008 06:12AM - nilesh
your name isnt simon
December 3rd 2008 02:12PM - teddy
bad
December 15th 2008 01:12PM - wit
no time limit, no question, and suck graphic????
May 15th 2009 02:05AM - hot critique
epic fail! D: i followed the instructions precisely and ended up with a muddle on my screen you were clear enough!
May 26th 2009 11:05PM - anon
"Next we are going to create the buttons which contain the letters for the player to guess. Draw a square with rounded corners. Copy and paste the rectangle. Change the color to white and Modify->Shape->Expand Fill. Set the inset to 5px. Add a text box with type dynamic text. Set the Var to displayletter. Return to the main stage and delete the movieclip from the stage. In the library, select the movieclip, right click Export for Actionscript and set the Linkage Identifier to displayletter. "
Like since WHEN did we create the dynamic text buttons as a movieclip?!?!?
Like since WHEN did we create the dynamic text buttons as a movieclip?!?!?
June 20th 2009 03:06AM - --
"Return to the main stage and delete the movieclip from the stage. In the library, select the movieclip, right click Export for Actionscript and set the Linkage Identifier to displayletter. "
Like since WHEN did we create the dynamic text buttons as a movieclip?!?!?
Like since WHEN did we create the dynamic text buttons as a movieclip?!?!?
June 20th 2009 03:06AM - --
nice
November 6th 2009 10:11AM - vigrx
i never guess out the exact wording behind the dashes, always lose.
November 6th 2009 08:11PM - sticker printing
how do u make the buttons? theres a error in the instructions isn't there?
November 16th 2009 05:11PM - Jack
Remember to set the Var to displayletter. Return to the main stage and delete the movieclip from the stage.
November 20th 2009 11:11AM - dodge charger
where do I paste the codes?
November 26th 2009 06:11PM - funny games
I couldn't follow it, first there were some problems in opening the file but after much perseverance, i managed to open it( every time i had carried out instructions rightly) and when i started to play it , it was all confusing, no criteria, no hints.
December 1st 2009 12:12PM - news script
Thanks for your code.
December 7th 2009 01:12AM - farmville cheats
Thanks for your code.
December 7th 2009 01:12AM - farmville cheats
The words are very difficult in the back end, i could not even win this game for a single time, sometimes after loosing i saw words that i never heard of LOLz
December 8th 2009 07:12PM - sticker printing
thanks for this great app. i'm not sure how i'll ever get any work done.
December 9th 2009 07:12PM - jeff paul scam
That is an awesome cube, thanks for sharing.
December 13th 2009 07:12AM - dental
Thanks a lot
December 19th 2009 08:12AM - streamming
Thanks a lot!
December 19th 2009 08:12AM - nakhonsithammarat
Nice Flash Game. Thanks for the Code.
December 22nd 2009 06:12AM - Magento Development India
OMG, i don't i won the game and completed the wording "Apple Jacks" on the first time. Woo
December 28th 2009 04:12PM - SEO Services
Nice flash game, thank you for sharing it with us.
December 29th 2009 09:12AM - Magento Development India
Thanks for sharing
December 29th 2009 10:12AM - game
Another great share. Thank you for your help.
December 29th 2009 11:12PM - sniper games
how do u make the buttons? theres a error in the instructions isn't there?
January 1st 2010 09:01PM - Cialis
lol i played many time this one ... i love it
January 3rd 2010 11:01AM - sticker printing
Excellent game... one of the old classics :-)
January 3rd 2010 02:01PM - Online Slimming
I have thought this is the complete Hangman Game code and it is useful for me.
January 4th 2010 02:01AM - jobs
how do u make the buttons? theres a error in the instructions isn't there?
January 5th 2010 03:01PM - click here
Excellent game... one of the old classics :-)
January 5th 2010 03:01PM - how to give an orgasm
I have thought this is the complete Hangman Game code and it is useful for me.
January 5th 2010 03:01PM - how to give a blowjob
crap
January 7th 2010 11:01PM - how to fuck yur aunt
tnx:)
January 9th 2010 01:01PM - evija
Fantastic, can't wait to get started on this as a project. Great resource site too!
January 11th 2010 03:01AM - Humax Foxsat HDR
lol i played many time this one ... i love it
January 11th 2010 06:01PM - Ares
thanks
January 14th 2010 10:01AM - projeksiyon
Buy wellbutrin online now! No prescription needed!
January 14th 2010 05:01PM - Buy Wellbutrin SR
I like very much the writings and pictures and explanations in your adress so I look forward to see your next writings.
January 15th 2010 08:01PM - evening dresses
Thanks for that - you even provided the code - nice..
January 17th 2010 05:01AM - Warcraft Millionaire
Fantastic, can't wait to get started on this as a project. Great resource site too!
January 17th 2010 05:01AM - wow gold guide
hello admin, I found your blog from yahoo and read a few of your other posts.They are awesome. Please keep it up!!
January 17th 2010 05:01AM - Xbox 360 Repair
I have thought this is the complete Hangman Game code and it is useful for me.
January 17th 2010 05:01AM - Copy Xbox 360 Games
Wow, I sure would like to know the meaning behind the symbols. I suspect they carry a message. How creative.
January 17th 2010 05:01AM - 3d girl
Pretty cool game. I'll try this one out and let you know how I get on with it here. Thanks.
January 17th 2010 12:01PM - Driver Update Software
Great code! Can someone help me out, I added a loser mc to the end of the missgraphic mc, works great as long as you keep losing. Also added a winner mc that works. Only after the first game you win it will show up along with the start again button the first time you hit a wrong letter. Please contact me if you can help with this code.
roz@ufonies.com
roz@ufonies.com
January 17th 2010 02:01PM - Roz
simple game and killing code.. you rock man
January 18th 2010 12:01PM - Acai Berry
I think this the great tutorial because it's easy and clear.
January 18th 2010 09:01PM - film
Comment of hangman game make code ease to extend.
January 18th 2010 10:01PM - book
The hangman game is pretty tool. Last time I played the game was on paper with a girl I took out to dinner. She spelled the words "thank you for dinner." It was pretty cool.
January 20th 2010 08:01PM - Cash for Gold
Thank you for infomation.
January 24th 2010 02:01AM - postwebfree
Thanks for that - you even provided the code - nice.
January 24th 2010 02:01AM - livescore
Fantastic, can't wait to get started on this as a project. Great resource site too!
January 24th 2010 02:01AM - 7m
lol i played many time this one ... i love it
January 24th 2010 02:01AM - polball
Thank a lot. I like it.
January 24th 2010 02:01AM - big4club
Thank a lot. I like it.
January 24th 2010 06:01AM - evening dresses
Thank you for this useful information
January 25th 2010 06:01PM - <a href ="http://
I like it , thank you
January 25th 2010 06:01PM - prom dresses
This is useful information
January 25th 2010 06:01PM - formal dresses
That’s really cheap to see the <a href="http://www.topdissertations.com">custom dissertation</a> take the superior note reffering to this post and utilize for <a href="http://www.topdissertations.com">thesis</a>. And the custom writing services want to thanks you for that!
February 5th 2010 05:02AM - Lisa26Ps
Special thank for good post.
February 5th 2010 09:02PM - โหลดเพลงà
Thank for the information
February 6th 2010 07:02AM - โหลดเพลงm
Hope we can add more such words..
February 9th 2010 04:02AM - Acai Berry 15
It's really a cool game..
February 9th 2010 04:02AM - Ares 11
Special thank for good post.
February 10th 2010 04:02AM - โหลดเพลงà
Special thank for good post.
February 10th 2010 08:02AM - โหลดเพลงà
omg , this blog is GREAT ! you have so much things to learn from here :D:D i`ll bookmark this,keep it up
February 24th 2010 08:02AM - communication
Thank you very much but i like informaiton to you.
March 7th 2010 10:03PM - âËÅ´à¾Å§¿ÃÕ
Add a Comment





