Hangman is a fun and simple game that everyone knows how to play. This tutorial will show how easy it is to create a Hangman game in Revolution.
Creating a game is a great way of starting out with Revolution as it covers many features
And at the end you have a great looking game that is fun to play and can be extended in any number of ways.
Once we have decided that we are going to create a Hangman game the first step in building our application is planning and design. We need to decide how we want our game to look, and think about exactly what it needs to do.
You can download the accompanying Hangman Project. This project contains annotated code to guide you through creating it.
We will start with the design of our user interface - so what do we need to include:
That's a good start, so now we will draw a few sketches of how our game might look. We have also decided to go for a Halloween theme so we will include this in our sketch. So a design might look something like this:
We can use this later as a guide when we create our user interface (GUI).
We also need to think about what our application needs to be able to do:
These are the main things that our application needs to do. If we are sure we have thought of everything we can move on to the next stage and start creating the application. First we will create the user interface and then we will start writing our code.
Since we already have a design this is relatively simple. Revolution Media allows you to drag controls from the Tools Palettes straight on to your stack and position them however you want.
This GUI is made up of
We ensure that we name all our controls sensibly. The buttons and lines in the answer group are named for their position in the group eg button "letter11" is the button in the first column of the first line. It is always good practice to name controls well but in this case it is particularly important as we need to use the names of the controls to set up our games.
Now that we have our GUI in place we can start writing code to make our game playable. Earlier we decided what the game needed to do. Now we will plan this further to make the game as easy to code as possible. There are things to consider, such as are there any actions that we might want to do in more than one place? If there are it is best to write a handler to do these actions which we can then call whenever it is needed without rewriting code. We also want to consider how best to write the code to make it simple and readable.
A good example of this is clearing the board. There are a number of actions we need to do to clear the board and reset the game state so we would include these in a handler which we can call whenever this needs done. Here is some pseudocode for clearing the board
on resetBoard
hide all graphics in group "skeleton"
set all the button in group "alphabet" to show as unused
clear the puzzle/answer area
reset properties storing the game state
end resetBoard
Other things to consider before starting to code are
Here is the handler which sets up an answer.
First we display the relevant image for the hint. Then we use repeat loops to step through each word in the answer and each character of that word. Due to the naming of the buttons and underlines in group "answer" we can set the custom property, icon and visibility of the necessary controls. Each word in the answer is represented by a line in the group.
on setUpAnswer pAnswer local tAnswer, tHint, tButtonName, tUnderlineName, \ tCharacter, tImageName # Show the Hint image put empty into sAnswerCharacters set the itemDelimiter to tab put item 1 of pAnswer into tAnswer put item 2 of pAnswer into tHint show image tHint # Set a custom property on each necessary button of group # "Answer" stating what letter is represents # Show the relevant underlines to display the puzzle # This is why naming of the buttons and underlines was # important, # they are named relative to their positions in the answer # Set the icon of the button to the ID of the relevant # letter image # When the player chooses that letter the button is made # visible and the letter is displayed # sAnswerCharacters stores the number of characters in the # answer # This is checked against to identify when the puzzle is # complete repeat with x = 1 to the number of words of tAnswer add the number of chars of word x of tAnswer to \ sAnswerCharacters repeat with y = 1 to the number of characters of word x \ of tAnswer put "letter" & x & y into tButtonName put "underline" & x & y into tUnderlineName put character y of word x of tAnswer into tCharacter put tCharacter & "Icon" into tImageName set the cLetter of button tButtonName of group \ "Answer" to tCharacter set the icon of button tButtonName to the id of image tImageName of card "Image Store" set the visible of graphic tUnderlineName of group \ "Answer" to true end repeat end repeat end setUpAnswer
Once you have written all your code and tested your game and are happy with it you can add more images to make it look even better. We have used images for the letter buttons and to display the answer, for the hints and for the New Game and Quit buttons.
To use images on buttons we store the images out of sight on a second card. Here we import all the images we want using the Import as Controls option in the file menu. We then set the icon of the button to the ID of the image we want to appear on it. In Revolution Media buttons can have normal icons, disabled icons, hover icons (shown when the mouse is over the button), Hilite icons (show when the mouse is down on the button) and others but these are the icons we will use.
To ensure our icons display correctly we turn off a number of properties of the buttons including Show Name, Opaque, Three D, Border and Hilite Border.
The background image can be imported directly onto the card as an image. Don't forget to set it to be the bottom layer so all your other controls appear on top of it.
And there you have it. A fun and great looking game. This game could be extended in a number of ways, more answers could be added under more categories. The game could be modified into a 2 player game or a game that allows you to choose the number of player. With Revolution Media the properties are endless.
Custom Property
Definition: An object property you define. A custom property can hold any data you want to associate with the object.
If statement
Type: control structure
Syntax: if condition then statement [else elseStatement]
Summary: Executes a list of statements if a condition is true.
Repeat Loop
Type: control structure
Syntax: repeat loopForm statementList end repeat
Summary: Executes a set of statements repeatedly.
Switch statement
Type: control structure
Syntax: switch [switchExpression] case {caseValue | caseCondition} [statementList] [default defaultStatementList] end switch
Summary: Chooses among several possible values for an expression, and executes a set of statements that depends on the value.
Icon
Type: property
Syntax: set the icon to {imageID | imageName} set the icon of {button | stack} to {imageID | imageName}
Objects (or Types): global, button, stack
Summary: Specifies an image that is displayed in a button, or used as the desktop icon of a stack file or application.
For more information see the Revolution dictionary.