scg/ch10/HangmanWithStub

From FANG

Jump to: navigation, search

001 package scg.ch10;
002 
003 import fang2.core.Game;
004 import fang2.sprites.StringSprite;
005 
006 /**
007  * The Hangman game: During {@code #setup()} a word is generated as are
008  * all the appropriate sprites (score, alphabet selector, hangman, show
009  * word). Game runs with player selecting the letters and the hangman
010  * and show word updating (show word updates if the letter _is_ in the
011  * word; the hangman is updated if the letter _is not_ in the word).
012  Level ends when the player is hung _or_ the word is guessed.
013  */
014 public class HangmanWithStub
015   extends Game {
016   /** the alphabet selector on the screen */
017   AlphabetSelector alphabet;
018 
019   /** represents the hangman (gallows and body) */
020   HangmanSprite hangman;
021 
022   /** sprite representing the score */
023   ScoreSprite scoreSprite;
024 
025   /** sprite representing the show word */
026   GuessableWord gamePhrase;
027 
028   /**
029    * Advance one frame: if game continues, check for selected letter; if
030    * letter is not null character, guess letter in the word to be
031    * guessed; if guess is not good, add a body part to the hangman
032    * sprite and if hangman is dead expose word and end game; if guess
033    * was good, check if word is guessed and if it is, game over. If game
034    * does not continue, keep checking for a press of the spacebar. When
035    * it is pressed, restart the game.
036    */
037   @Override
038   public void advance(double dT{
039     if (isGameOver()) {
040       if (getKeyPressed() == ' '{
041         setGameOver(false);
042         startOver();
043       }
044     } else {
045       char ch = alphabet.selectedChar();
046       if (ch != '\0'{
047         if (gamePhrase.guess(ch)) {
048           if (gamePhrase.isGuessed()) {
049             scoreSprite.win();
050             doneWithGame("Congratulations");
051           }
052         } else {
053           hangman.incrementState();
054           if (hangman.isDead()) {
055             gamePhrase.expose();
056             gamePhrase.setColor(getColor("red"));
057             scoreSprite.lose();
058             doneWithGame("You lose!");
059           }
060         }
061       }
062     }
063   }
064 
065   /**
066    * Setup the sprites: the alphabet selector, the hangman, the score,
067    * and the word.
068    */
069   @Override
070   public void setup() {
071     setupAlphabet();
072     setupHangman();
073     setupScore();
074     setupGamePhrase();
075   }
076 
077   /**
078    * Done with the game. Display an end of game message on the screen
079    * and append instructions on how to restart the game.
080    *
081    @param  endOfGameMessage  the end of game message to display for
082    *                           the player
083    */
084   private void doneWithGame(String endOfGameMessage{
085     StringSprite restartMessage = new StringSprite(endOfGameMessage +
086         "\nPress <space> to play again.");
087     restartMessage.setScale(0.9);
088     restartMessage.setLocation(0.50.75);
089     addSprite(restartMessage);
090     alphabet.hide();
091     setGameOver(true);
092   }
093 
094   /**
095    * Setup the alphabet selector sprite. Almost as wide as the screen,
096    * centered in bottom half of the screen.
097    */
098   private void setupAlphabet() {
099     alphabet = new AlphabetSelector();
100     alphabet.setScale(0.85);
101     alphabet.setLocation(0.50.75);
102     addSprite(alphabet);
103   }
104 
105   /**
106    * Setup the hangman sprite. Centered in top half of the screen.
107    */
108   private void setupHangman() {
109     hangman = new HangmanSprite();
110     hangman.setLocation(0.50.25);
111     hangman.setScale(0.5);
112     hangman.setColor(getColor("misty rose"));
113     addSprite(hangman);
114   }
115 
116   /**
117    * Setup the score sprite in the upper left corner of the screen.
118    */
119   private void setupScore() {
120     scoreSprite = new ScoreSprite();
121     scoreSprite.leftJustify();
122     scoreSprite.topJustify();
123     scoreSprite.setLocation(0.00.0);
124     scoreSprite.setScale(0.1);
125     addSprite(scoreSprite);
126   }
127 
128   /**
129    * Setup the guessable word in the center of the screen. Calls
130    {@link #pickPhrase()} to get the value to play.
131    */
132   private void setupGamePhrase() {
133     String pickedPhrase = pickPhrase();
134     if (pickedPhrase == null{
135       System.err.println("Unable to find a phrase with which to play!");
136       System.exit(1)// error exit code
137     }
138     gamePhrase = new GuessableWord(pickedPhrase);
139     gamePhrase.bottomJustify();
140     gamePhrase.setWidth(0.7);
141     gamePhrase.setLocation(0.50.6);
142     addSprite(gamePhrase);
143   }
144 
145   /**
146    * Load a selection of words and return one, the word to be guessed.
147    *
148    @return  the word to be guessed
149    */
150   private String pickPhrase() {
151     // TODO: Replace with real load/select code.
152     return "cats and dogs";
153   }
154 }
155 
156 //Uploaded on Mon Mar 29 21:40:19 EDT 2010


Download/View scg/ch10/HangmanWithStub.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games