|
001 packagepackage is used to name the directory or folder a class is in scg.ch10;
002
003 importimport means to make the classes and/or packages available in this program fang2.core.Game;
004 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
005
006 /**
007 * The Hangman game: During {open braces start code blocks and must be matched with a close brace@code #setup()}close braces end code blocks and must match an earlier open brace 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 ifif executes the next statement only if the condition in parenthesis evaluates to true the letter _is_ in the
011 * word; the hangman is updated ifif executes the next statement only if the condition in parenthesis evaluates to true the letter _is not_ in the word).
012 * Level ends when the player is hung _or_ the word is guessed.
013 */
014 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects HangmanWithStub
015 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
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: ifif executes the next statement only if the condition in parenthesis evaluates to true game continues, check forfor is a looping structure for repeatedly executing a block of code selected letter; ifif executes the next statement only if the condition in parenthesis evaluates to true
030 * letter is not nullnull is the value used to refer to a non-existant object character, guess letter in the word to be
031 * guessed; ifif executes the next statement only if the condition in parenthesis evaluates to true guess is not good, add a body part to the hangman
032 * sprite and ifif executes the next statement only if the condition in parenthesis evaluates to true hangman is dead expose word and end game; ifif executes the next statement only if the condition in parenthesis evaluates to true guess
033 * was good, check ifif executes the next statement only if the condition in parenthesis evaluates to true word is guessed and ifif executes the next statement only if the condition in parenthesis evaluates to true it is, game over. If game
034 * does not continuecontinue terminates the current iteration of the loop and continues to the next iteration, keep checking forfor is a looping structure for repeatedly executing a block of code a press of the spacebar. When
035 * it is pressed, restart the game.
036 */
037 @Override
038 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
039 ifif executes the next statement only if the condition in parenthesis evaluates to true (isGameOver()) {open braces start code blocks and must be matched with a close brace
040 ifif executes the next statement only if the condition in parenthesis evaluates to true (getKeyPressed() ==this is the comparison operator which evaluates to true if both sides are the same ' ') {open braces start code blocks and must be matched with a close brace
041 setGameOver(falsefalse is a value for the boolean type and means not true);
042 startOver();
043 }close braces end code blocks and must match an earlier open brace
044 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
045 charchar is the type for a single letter or symbol and it is short for character ch =this assignment operator makes the left side equal to the right side alphabet.selectedChar();
046 ifif executes the next statement only if the condition in parenthesis evaluates to true (ch !=this is the not equals operator which evaluates to true if both sides are different '\0') {open braces start code blocks and must be matched with a close brace
047 ifif executes the next statement only if the condition in parenthesis evaluates to true (gamePhrase.guess(ch)) {open braces start code blocks and must be matched with a close brace
048 ifif executes the next statement only if the condition in parenthesis evaluates to true (gamePhrase.isGuessed()) {open braces start code blocks and must be matched with a close brace
049 scoreSprite.win();
050 doneWithGame("Congratulations");
051 }close braces end code blocks and must match an earlier open brace
052 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
053 hangman.incrementState();
054 ifif executes the next statement only if the condition in parenthesis evaluates to true (hangman.isDead()) {open braces start code blocks and must be matched with a close brace
055 gamePhrase.expose();
056 gamePhrase.setColor(getColor("red"));
057 scoreSprite.lose();
058 doneWithGame("You lose!");
059 }close braces end code blocks and must match an earlier open brace
060 }close braces end code blocks and must match an earlier open brace
061 }close braces end code blocks and must match an earlier open brace
062 }close braces end code blocks and must match an earlier open brace
063 }close braces end code blocks and must match an earlier open brace
064
065 /**
066 * Setup the sprites: the alphabet selector, the hangman, the score,
067 * and the word.
068 */
069 @Override
070 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
071 setupAlphabet();
072 setupHangman();
073 setupScore();
074 setupGamePhrase();
075 }close braces end code blocks and must match an earlier open brace
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 * @paramthis is the Javadoc tag for documenting the purpose of parameters endOfGameMessage the end of game message to display forfor is a looping structure for repeatedly executing a block of code
082 * the player
083 */
084 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value doneWithGame(String endOfGameMessage) {open braces start code blocks and must be matched with a close brace
085 StringSprite restartMessage =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite(endOfGameMessage +adds two numbers together or concatenates Strings together
086 "\nPress <space> to play again.");
087 restartMessage.setScale(0.9);
088 restartMessage.setLocation(0.5, 0.75);
089 addSprite(restartMessage);
090 alphabet.hide();
091 setGameOver(truetrue is the boolean value that is the opposite of false);
092 }close braces end code blocks and must match an earlier open brace
093
094 /**
095 * Setup the alphabet selector sprite. Almost as wide as the screen,
096 * centered in bottom half of the screen.
097 */
098 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupAlphabet() {open braces start code blocks and must be matched with a close brace
099 alphabet =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor AlphabetSelector();
100 alphabet.setScale(0.85);
101 alphabet.setLocation(0.5, 0.75);
102 addSprite(alphabet);
103 }close braces end code blocks and must match an earlier open brace
104
105 /**
106 * Setup the hangman sprite. Centered in top half of the screen.
107 */
108 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupHangman() {open braces start code blocks and must be matched with a close brace
109 hangman =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor HangmanSprite();
110 hangman.setLocation(0.5, 0.25);
111 hangman.setScale(0.5);
112 hangman.setColor(getColor("misty rose"));
113 addSprite(hangman);
114 }close braces end code blocks and must match an earlier open brace
115
116 /**
117 * Setup the score sprite in the upper left corner of the screen.
118 */
119 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupScore() {open braces start code blocks and must be matched with a close brace
120 scoreSprite =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ScoreSprite();
121 scoreSprite.leftJustify();
122 scoreSprite.topJustify();
123 scoreSprite.setLocation(0.0, 0.0);
124 scoreSprite.setScale(0.1);
125 addSprite(scoreSprite);
126 }close braces end code blocks and must match an earlier open brace
127
128 /**
129 * Setup the guessable word in the center of the screen. Calls
130 * {open braces start code blocks and must be matched with a close brace@link #pickPhrase()}close braces end code blocks and must match an earlier open brace to get the value to play.
131 */
132 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupGamePhrase() {open braces start code blocks and must be matched with a close brace
133 String pickedPhrase =this assignment operator makes the left side equal to the right side pickPhrase();
134 ifif executes the next statement only if the condition in parenthesis evaluates to true (pickedPhrase ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
135 System.err.println("Unable to find a phrase with which to play!");
136 System.exit(1); // error exit code
137 }close braces end code blocks and must match an earlier open brace
138 gamePhrase =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor GuessableWord(pickedPhrase);
139 gamePhrase.bottomJustify();
140 gamePhrase.setWidth(0.7);
141 gamePhrase.setLocation(0.5, 0.6);
142 addSprite(gamePhrase);
143 }close braces end code blocks and must match an earlier open brace
144
145 /**
146 * Load a selection of words and returnreturn means to provide the result of the method and/or cease execution of the method immediately one, the word to be guessed.
147 *
148 * @returnnull the word to be guessed
149 */
150 privateprivate is used to restrict access to the current class only String pickPhrase() {open braces start code blocks and must be matched with a close brace
151 // TODO: Replace with real load/select code.
152 returnreturn means to provide the result of the method and/or cease execution of the method immediately "cats and dogs";
153 }close braces end code blocks and must match an earlier open brace
154 }close braces end code blocks and must match an earlier open brace
155
156 //Uploaded on Mon Mar 29 21:40:19 EDT 2010
|