scg/ch10/HangmanSprite

From FANG

Jump to: navigation, search

001 package scg.ch10;
002 
003 import java.awt.Color;
004 import java.util.ArrayList;
005 
006 import fang2.core.Game;
007 import fang2.core.Sprite;
008 import fang2.sprites.*;
009 
010 /**
011  * The gallows and hanging victim for 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 true as long as there are more parts
014  * to be hung.
015  */
016 public class HangmanSprite
017   extends CompositeSprite {
018   /** list of the body parts to show */
019   private final ArrayList<Sprite> bodyParts;
020 
021   /** number of body parts showing */
022   private int numberOfPartsHung;
023 
024   /**
025    * Construct a new HangmanSprite: create gallows and victim; hide
026    * victim parts and put them in bodyParts in the order they should be
027    * revealed.
028    */
029   public HangmanSprite() {
030     bodyParts = new ArrayList<Sprite>();
031 
032     setupGallows();
033     setupVictim();
034     setState(0);
035   }
036 
037   /**
038    * Reset state to starting state (0).
039    */
040   public void clear() {
041     setState(0);
042   }
043 
044   /**
045    * Increment the state. Add a body part to the hanging victim.
046    */
047   public void incrementState() {
048     if (isDead()) {
049       setState(numberOfPartsHung + 1);
050     }
051   }
052 
053   /**
054    Set the color. Color for 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    @param  color  the new color
059    */
060   @Override
061   public void setColor(Color color{
062     for (int bodyPartIndex = 0; bodyPartIndex != bodyParts.size();
063         ++bodyPartIndex{
064       Sprite curr = bodyParts.get(bodyPartIndex);
065       curr.setColor(color);
066     }
067   }
068 
069   /**
070    * Is the player dead? They are alive as long as not all the parts
071    * have been exposed, dead if they have.
072    *
073    @return  true if the player is dead; false otherwise
074    */
075   public boolean isDead() {
076     return numberOfPartsHung == bodyParts.size();
077   }
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    @param  numberOfPartsHung  the new number of body parts hung
085    */
086   private void setState(int numberOfPartsHung{
087     if ((<= numberOfPartsHung&&
088         (numberOfPartsHung < bodyParts.size())) {
089       this.numberOfPartsHung = numberOfPartsHung;
090       updateDisplayState();
091     }
092   }
093 
094   /**
095    * Create the gallows. Victim is centered at 0, 0 and about 0.25 wide
096    * by 0.9 high.
097    */
098   private void setupGallows() {
099     PolyLineSprite gallows;
100     gallows = new PolyLineSprite(0.00.50.50.50.250.50.25,
101         -0.50.0, -0.5);
102 
103     gallows.setLineThickness(0.05);
104     gallows.setColor(Game.getColor("slate gray"));
105     addSprite(gallows);
106   }
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 this method is called).
112    */
113   private void setupVictim() {
114     OvalSprite head = new OvalSprite(0.250.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 = new LineSprite(0.0, -0.250.00.20);
125     addSprite(body);
126     bodyParts.add(body);
127 
128     LineSprite leftArm = new LineSprite(0.00.0, -0.20, -0.20);
129     addSprite(leftArm);
130     bodyParts.add(leftArm);
131 
132     LineSprite rightArm = new LineSprite(0.00.00.20, -0.20);
133     addSprite(rightArm);
134     bodyParts.add(rightArm);
135 
136     LineSprite leftLeg = new LineSprite(0.00.20, -0.200.40);
137     addSprite(leftLeg);
138     bodyParts.add(leftLeg);
139 
140     LineSprite rightLeg = new LineSprite(0.00.200.200.40);
141     addSprite(rightLeg);
142     bodyParts.add(rightLeg);
143   }
144 
145   /**
146    * Update the display to match the current state value. Turn on and
147    * off the different body parts.
148    */
149   private void updateDisplayState() {
150     for (int bodyPartIndex = 0; bodyPartIndex != bodyParts.size();
151         ++bodyPartIndex{
152       Sprite curr = bodyParts.get(bodyPartIndex);
153       if (bodyPartIndex < numberOfPartsHung{
154         curr.show();
155       } else {
156         curr.hide();
157       }
158     }
159   }
160 }
161 
162 //Uploaded on Mon Mar 29 21:42:03 EDT 2010


Download/View scg/ch10/HangmanSprite.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