scg/ch06/GeneralPong

From FANG

Jump to: navigation, search

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


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