|
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 java.awt.Color;
004 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
005
006 importimport means to make the classes and/or packages available in this program fang2.core.Game;
007 importimport means to make the classes and/or packages available in this program fang2.core.Sprite;
008 importimport means to make the classes and/or packages available in this program fang2.sprites.*;
009
010 /**
011 * The gallows and hanging victim forfor is a looping structure for repeatedly executing a block of code a game of Hangman. The victim is
012 * initially hidden; as the number of parts hung goes up, the parts are
013 * revealed. stillKicking returns truetrue is the boolean value that is the opposite of false as longlong is the type for whole numbers (they have a larger range than int) as there are more parts
014 * to be hung.
015 */
016 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 HangmanSprite
017 extendsextends means to customize or extend the functionality of a class CompositeSprite {open braces start code blocks and must be matched with a close brace
018 /** list of the body parts to show */
019 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<Sprite> bodyParts;
020
021 /** number of body parts showing */
022 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer numberOfPartsHung;
023
024 /**
025 * Construct a newnew is used to create objects by calling the constructor HangmanSprite: create gallows and victim; hide
026 * victim parts and put them in bodyParts in the order they should be
027 * revealed.
028 */
029 publicpublic is used to indicate unrestricted access (any other class can have access) HangmanSprite() {open braces start code blocks and must be matched with a close brace
030 bodyParts =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Sprite>();
031
032 setupGallows();
033 setupVictim();
034 setState(0);
035 }close braces end code blocks and must match an earlier open brace
036
037 /**
038 * Reset state to starting state (0).
039 */
040 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value clear() {open braces start code blocks and must be matched with a close brace
041 setState(0);
042 }close braces end code blocks and must match an earlier open brace
043
044 /**
045 * Increment the state. Add a body part to the hanging victim.
046 */
047 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value incrementState() {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 (isDead()) {open braces start code blocks and must be matched with a close brace
049 setState(numberOfPartsHung +adds two numbers together or concatenates Strings together 1);
050 }close braces end code blocks and must match an earlier open brace
051 }close braces end code blocks and must match an earlier open brace
052
053 /**
054 * Set the color. Color forfor is a looping structure for repeatedly executing a block of code our purposes is the color of the body.
055 * Sets the color of all body parts (visible or not) to the given
056 * color.
057 *
058 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the newnew is used to create objects by calling the constructor color
059 */
060 @Override
061 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setColor(Color color) {open braces start code blocks and must be matched with a close brace
062 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer bodyPartIndex =this assignment operator makes the left side equal to the right side 0; bodyPartIndex !=this is the not equals operator which evaluates to true if both sides are different bodyParts.size();
063 ++this is the increment operator, which increases the variable by 1bodyPartIndex) {open braces start code blocks and must be matched with a close brace
064 Sprite curr =this assignment operator makes the left side equal to the right side bodyParts.get(bodyPartIndex);
065 curr.setColor(color);
066 }close braces end code blocks and must match an earlier open brace
067 }close braces end code blocks and must match an earlier open brace
068
069 /**
070 * Is the player dead? They are alive as longlong is the type for whole numbers (they have a larger range than int) as not all the parts
071 * have been exposed, dead ifif executes the next statement only if the condition in parenthesis evaluates to true they have.
072 *
073 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the player is dead; falsefalse is a value for the boolean type and means not true otherwise
074 */
075 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isDead() {open braces start code blocks and must be matched with a close brace
076 returnreturn means to provide the result of the method and/or cease execution of the method immediately numberOfPartsHung ==this is the comparison operator which evaluates to true if both sides are the same bodyParts.size();
077 }close braces end code blocks and must match an earlier open brace
078
079 /**
080 * Directly set the state to the given value. If number is out of
081 * range, the number of body parts hung is unchanged. Method updates
082 * the state and the display of the state.
083 *
084 * @paramthis is the Javadoc tag for documenting the purpose of parameters numberOfPartsHung the newnew is used to create objects by calling the constructor number of body parts hung
085 */
086 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setState(intint is the type for whole numbers and it is short for integer numberOfPartsHung) {open braces start code blocks and must be matched with a close brace
087 ifif executes the next statement only if the condition in parenthesis evaluates to true ((0 <=this evaluates to true if the left side is not more than the right side numberOfPartsHung) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&)
088 (numberOfPartsHung < bodyParts.size())) {open braces start code blocks and must be matched with a close brace
089 thisthis means the current object (the implicit parameter).numberOfPartsHung =this assignment operator makes the left side equal to the right side numberOfPartsHung;
090 updateDisplayState();
091 }close braces end code blocks and must match an earlier open brace
092 }close braces end code blocks and must match an earlier open brace
093
094 /**
095 * Create the gallows. Victim is centered at 0, 0 and about 0.25 wide
096 * by 0.9 high.
097 */
098 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupGallows() {open braces start code blocks and must be matched with a close brace
099 PolyLineSprite gallows;
100 gallows =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor PolyLineSprite(0.0, 0.5, 0.5, 0.5, 0.25, 0.5, 0.25,
101 -0.5, 0.0, -0.5);
102
103 gallows.setLineThickness(0.05);
104 gallows.setColor(Game.getColor("slate gray"));
105 addSprite(gallows);
106 }close braces end code blocks and must match an earlier open brace
107
108 /**
109 * Create all the parts of the victim. Each part is added to the
110 * sprite and to the bodyParts list (bodyParts MUST be initialized
111 * before thisthis means the current object (the implicit parameter) method is called).
112 */
113 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupVictim() {open braces start code blocks and must be matched with a close brace
114 OvalSprite head =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(0.25, 0.25);
115 head.setLocation(0.0, -0.375);
116 head.hide();
117 addSprite(head);
118 bodyParts.add(head);
119
120 // set the initial thickness of created line sprites (avoid
121 // setThickness in each one)
122 LineSprite.setInitialLineThickness(0.03);
123
124 LineSprite body =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(0.0, -0.25, 0.0, 0.20);
125 addSprite(body);
126 bodyParts.add(body);
127
128 LineSprite leftArm =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(0.0, 0.0, -0.20, -0.20);
129 addSprite(leftArm);
130 bodyParts.add(leftArm);
131
132 LineSprite rightArm =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(0.0, 0.0, 0.20, -0.20);
133 addSprite(rightArm);
134 bodyParts.add(rightArm);
135
136 LineSprite leftLeg =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(0.0, 0.20, -0.20, 0.40);
137 addSprite(leftLeg);
138 bodyParts.add(leftLeg);
139
140 LineSprite rightLeg =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(0.0, 0.20, 0.20, 0.40);
141 addSprite(rightLeg);
142 bodyParts.add(rightLeg);
143 }close braces end code blocks and must match an earlier open brace
144
145 /**
146 * Update the display to match the current state value. Turn on and
147 * off the different body parts.
148 */
149 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateDisplayState() {open braces start code blocks and must be matched with a close brace
150 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer bodyPartIndex =this assignment operator makes the left side equal to the right side 0; bodyPartIndex !=this is the not equals operator which evaluates to true if both sides are different bodyParts.size();
151 ++this is the increment operator, which increases the variable by 1bodyPartIndex) {open braces start code blocks and must be matched with a close brace
152 Sprite curr =this assignment operator makes the left side equal to the right side bodyParts.get(bodyPartIndex);
153 ifif executes the next statement only if the condition in parenthesis evaluates to true (bodyPartIndex < numberOfPartsHung) {open braces start code blocks and must be matched with a close brace
154 curr.show();
155 }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
156 curr.hide();
157 }close braces end code blocks and must match an earlier open brace
158 }close braces end code blocks and must match an earlier open brace
159 }close braces end code blocks and must match an earlier open brace
160 }close braces end code blocks and must match an earlier open brace
161
162 //Uploaded on Mon Mar 29 21:42:03 EDT 2010
|