scg/ch06/SoloPong

From FANG

Jump to: navigation, search

001 package scg.ch06;
002 
003 import fang2.core.Game;
004 import fang2.sprites.StringSprite;
005 
006 /**
007  * The main game for solo pong. This method keeps track of scoring and
008  * is in charge of "ticking" the clock for all of the objects in the
009  * game. The objects themselves take care of updating their position as
010  * necessary.
011  */
012 public class SoloPong
013   extends Game {
014   /** Seconds for the countdown timer to count */
015   public static final double COUNTDOWN_SECONDS = 3.0;
016 
017   /** The ball */
018   private PongBall ball;
019 
020   /** Countdown timer */
021   private CountdownTimer countdown;
022 
023   /** The paddle */
024   private PongPaddle paddle;
025 
026   /** Press space to start message */
027   private StringSprite pressSpace;
028 
029   /** The display of the score */
030   private StringSprite volleysSprite;
031 
032   /**
033    * Advance the game one tick. This game has three states: Ball moving;
034    * Counting down to ball moving; Waiting to start countdown. The three
035    * states can be separated by seeing whether: ball isVisible();
036    * countdown isVisible(); pressSpace isVisible().
037    */
038   @Override
039   public void advance(double deltaT{
040     if (isWaitingToCountdown()) {
041       if (getKeyPressed() == ' '{
042         beginCountingDown();
043       }
044     } else if (isCountingDown()) {
045       countdown.advance(deltaT);
046       if (!isCountingDown()) {
047         beginPlaying();
048       }
049     } else if (isPlaying()) {
050       paddle.advance(deltaT);
051       ball.advance(deltaT, paddle);
052 
053       if (ball.isOutOfBounds()) {
054         beginWaitingToCountdown();
055       }
056     }
057   }
058 
059   /**
060    * Update the score; this overrides the Game version so we can update
061    * our display of the score on the screen.
062    *
063    @param  score  the new score value
064    */
065   @Override
066   public void setScore(int score{
067     super.setScore(score);
068     volleysSprite.setText(Integer.toString(getScore()));
069   }
070 
071   /**
072    * This game has three states. To make it easy to tell what state we
073    * are in and to easily switch states, these methods hide the details
074    * from the {@code advance} method.
075    */
076 
077   /**
078    * Create all of the sprites and the score. Add all sprites to the
079    * game. Make sure we are waiting for player to press space.
080    */
081   @Override
082   public void setup() {
083     ball = new PongBall(0.10.1);
084     ball.setColor(getColor("white"));
085     addSprite(ball);
086 
087     paddle = new PongPaddle(0.050.20.00.9);
088     paddle.setColor(getColor("green"));
089     paddle.setLocation(0.90.5);
090     addSprite(paddle);
091 
092     countdown = new CountdownTimer();
093     countdown.setLocation(0.50.5);
094     countdown.setScale(0.25);
095     countdown.setColor(getColor("white"));
096     addSprite(countdown);
097 
098     volleysSprite = new StringSprite();
099     volleysSprite.setScale(0.05);
100     volleysSprite.setLocation(0.100.10);
101     volleysSprite.setText(Integer.toString(0));
102     addSprite(volleysSprite);
103 
104     pressSpace = new StringSprite();
105     pressSpace.setScale(0.05);
106     pressSpace.setText("Press <Space> to begin");
107     pressSpace.setColor(getColor("yellow"));
108     pressSpace.setLocation(0.50.5);
109     addSprite(pressSpace);
110 
111     beginWaitingToCountdown();
112   }
113 
114   /**
115    * Whatever state we were in, move us to the counting down state
116    */
117   private void beginCountingDown() {
118     ball.hide();
119     countdown.show();
120     pressSpace.hide();
121     countdown.setTimer(COUNTDOWN_SECONDS);
122     countdown.startTimer();
123   }
124 
125   /**
126    * Whatever state we were in, move us to the moving ball state.
127    */
128   private void beginPlaying() {
129     ball.show();
130     countdown.hide();
131     pressSpace.hide();
132     startBall(ball);
133     setScore(0);// no volleys yet
134   }
135 
136   /**
137    * Whatever state we were in, move us to the waiting to countdown
138    * state.
139    */
140   private void beginWaitingToCountdown() {
141     ball.hide();
142     countdown.hide();
143     pressSpace.show();
144   }
145 
146   /**
147    * Are we in the counting down state?
148    *
149    @return  true if we are, false otherwise
150    */
151   private boolean isCountingDown() {
152     return countdown.isVisible();
153   }
154 
155   /**
156    * Are we in the moving ball state?
157    *
158    @return  true if we are, false otherwise
159    */
160   private boolean isPlaying() {
161     return ball.isVisible();
162   }
163 
164   /**
165    * Are we in the waiting to countdown state?
166    *
167    @return  true if we are, false otherwise
168    */
169   private boolean isWaitingToCountdown() {
170     return pressSpace.isVisible();
171   }
172 
173   /**
174    Position a ball somewhere in the middle of the left half of the
175    * screen.
176    *
177    @param  theBall  the ball to position
178    */
179   private void startBall(PongBall theBall{
180     double xVelocity = randomDouble(0.20.4);
181     double yVelocity = randomDouble(0.20.5);
182 
183     if (randomInt(2== 1{
184       xVelocity = -xVelocity;
185     }
186     if (randomInt(2== 1{
187       yVelocity = -yVelocity;
188     }
189 
190     theBall.setVelocity(xVelocity, yVelocity);
191     double xLocation = randomDouble(0.20.4);
192     double yLocation = randomDouble(0.30.7);
193     theBall.setLocation(xLocation, yLocation);
194   }
195 }
196 //Uploaded on Mon Mar 29 21:42:25 EDT 2010


Download/View scg/ch06/SoloPong.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