Every Programmer Must Be Able To Create This Simple Game
If you’re interested in game development but don’t quite know where to start, you’ve come to the right place. Game development extends far beyond game programming which extends far beyond regular programming.
The mistake too many newbies make is to try to go from ‘Hello World’ programming to attempting to make a complex strategy game which incorporates all form of advanced graphics, then facing big challenges with it and eventually giving up.
You aren’t a game developer until you’ve released a completed game
And it doesn’t matter how many half-done game demos you’ve written, you aren’t a game developer until you’ve released an actual completed game. So you’re better off taking your time and starting slow with a simple game then advancing at a pace you’re comfortable with.
Al Sweigart has a better list of games that you can make to practice programming. Not all are beginner level but its a great list to look at!
Below is my short list of games to start with because they require simple logic and minimal graphics to implement.
For this post, I will be creating a game of Hangman which I consider the quickest to implement. If you want to be a game developer, be a programmer first. And every programmer must be able to create this simple game.
1. The computer chooses a word and player tries to guess what it is one letter at a time.
2. The computer prints number of asterisks(*) equivalent to the number of letters in the word.
3. If the guessing player suggests a letter that occurs in the word, the computer fills in the blanks with that letter in the right places.
4. If the word does not contain the suggested letter, the computer draws one element of a hangman’s gallows.
5. As the game progresses, a segment of the gallows and of a victim is added for every suggested letter not in the word.
6. If the completed hangman’s gallows is drawn then the player loses.
7. Otherwise, if all the right letters are guessed before this then the player wins.
Step 1: The variables
First we declare the variables we will be using. We use static keyword so that the variables are associated with the class as a whole.
- String words: This array list is the list of words which the computer will choose at random to play the game.
- String word: The selected random word will be held in this variable.
- String asterisk: Holds the String of asterisks based on the length of the chosen word.
- int count: Keeps track of how many guesses the player has had.
Step 2: The main method
We use the Scanner method to ask the user to input their guess. A while loop is used to make sure the game is repeated until;
- A player fails 7 times (i.e the hangman’s gallows is fully drawn), or,
- The ‘asterisk’ variable no longer contains any more *‘s but all are replaced by correct letters. The idea is, with every correct guess of a letter, the corresponding * in the ‘asterisk’ variable is replaced by the correct letter. So no more *‘s means all the letters have been guessed.
Step 3: The hang method
We introduce a new variable ‘newasterisk’ which will hold the new value of ‘asterisk’ if a correct letter is guessed.
The for loop checks each letter of the word to see if it has the guessed letter. If it does, it sets the character at that position in ‘newasterisk’ to the guessed letter. Otherwise it just puts an *.
After a wrong guess, the value of ‘asterisk’ and ‘newasterisk’ will be the same so the counter is increased. Otherwise the value of ‘asterisk’ is now ‘newasterisk’.
Step 4: The hangman image
We use if statements for each part of the hangman image.
Below is the completed code. Give it a run in your IDE and see how it works.
What do you think of my implementation? Can you think of ways to optimize it? Or create a more advanced version of the game? Why don’t you give it some practice by creating some of the other simple games on the list? Leave your comments below.