scg/ch04/NewtonsAppleFall

From FANG

Jump to: navigation, search

001 package scg.ch04;
002 
003 import fang2.core.Game;
004 import fang2.sprites.OvalSprite;
005 import fang2.sprites.RectangleSprite;
006 import fang2.sprites.StringSprite;
007 
008 /**
009  * NewtonsAppleFallOnce - Randomly placed apple falls slowly down the
010  * screen one time.
011  */
012 public class NewtonsAppleFall
013   extends Game {
014   // ----- visual representations of the apple and physicist -----
015   /** on screen representation of the apple; a small red circle */
016   private OvalSprite apple;
017 
018   /** on screen representation of Newton; a small green square */
019   private RectangleSprite newton;
020 
021   // ----- keeping and displaying the score -----
022   /** number of apples caught */
023   private int applesCaught;
024 
025   /** number of apples dropped */
026   private int applesDropped;
027 
028   /**
029    * on-screen text displaying the current score; contents are updated
030    * whenever the score changes
031    */
032   private StringSprite displayScore;
033 
034   /**
035    * Update the game state: only game state here is where the apple is;
036    * let it fall each frame. Velocity is 0.30 screen/second. When the
037    * apple reaches the bottom of the screen, call dropApple to restart
038    * it at the top of the screen.
039    */
040   @Override
041   public void advance(double secondsSinceLastCall{
042     apple.translateY(0.30 * secondsSinceLastCall);
043     if (apple.getY() >= 1.0{
044       dropApple();
045     }
046   }
047 
048   /**
049    * place the apple at the initial height and a random horizontal
050    * location
051    */
052   public void dropApple() {
053     apple.setLocation(randomDouble()0.00);
054     applesDropped = applesDropped + 1;// another apple dropped
055   }
056 
057   /**
058    * The method called by FANG before the game starts. Include all
059    * "one-time" instructions. Our simple computer games will create and
060    * add Sprites here the game begins. Create and add Sprites; Sprites
061    * are visible things in the game. Once they are added to the game
062    * they are automatically redrawn by the game engine.
063    */
064   @Override
065   public void setup() {
066     // initialize the score
067     applesCaught = 0;
068     applesDropped = 0;
069 
070     // The apple is small and red; its initial position is set randomly
071     // in the dropApple routine (called here and when apple bottoms out)
072     // our apple is a circle (x and y dimensions are the same)
073     apple = new OvalSprite(0.100.10);
074     apple.setColor(getColor("red"));
075     dropApple();
076 
077     // newton is small, green, and begins at the middle bottom of the
078     // screen.
079     newton = new RectangleSprite(0.100.10);
080     newton.setColor(getColor("green"));
081     newton.setLocation(0.500.90);
082 
083     displayScore = new StringSprite();
084     displayScore.setLineHeight(0.10);
085     displayScore.setText("Score: ");
086     displayScore.setColor(getColor("white"));
087     displayScore.topJustify();// move location to upper-left corner
088     displayScore.leftJustify();
089     displayScore.setLocation(0.000.00);
090     displayScore.setText("Score: " + applesCaught + "/" +
091       applesDropped);
092 
093     // for the sprites to be part of the game, they must be added to the
094     // game itself.
095     addSprite(apple);
096     addSprite(newton);
097     addSprite(displayScore);
098   }
099 }
100 
101 //Uploaded on Mon Mar 29 21:41:20 EDT 2010


Download/View scg/ch04/NewtonsAppleFall.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