scg/ch04/NewtonsApple

From FANG

Jump to: navigation, search

001 package scg.ch04;
002 
003 import fang2.attributes.Location2D;
004 import fang2.core.Game;
005 import fang2.sprites.OvalSprite;
006 import fang2.sprites.RectangleSprite;
007 import fang2.sprites.StringSprite;
008 
009 /**
010  * NewtonsApple - Randomly placed apples fall and mouse-controlled
011  * Newton must be under them when they fall. Score c/d where c is caught
012  * apples, d dropped apples
013  */
014 public class NewtonsApple
015   extends Game {
016   // ----- visual representations of the apple and physicist -----
017   /** on screen representation of the apple; a small red circle */
018   private OvalSprite apple;
019 
020   /** on screen representation of Newton; a small green square */
021   private RectangleSprite newton;
022 
023   // ----- keeping and displaying the score -----
024   /** number of apples caught */
025   private int applesCaught;
026 
027   /** number of apples dropped */
028   private int applesDropped;
029 
030   /** on-screen current score; update contents when score changes */
031   private StringSprite displayScore;
032 
033   /**
034    * Called by FANG for each frame. This is where the game checks for
035    * and reacts to user input. 
036    */
037   @Override
038   public void advance(double secondsSinceLastCall{
039     // (1) Move newton so his x-coordinate matches x-coordinate of mouse
040     Location2D position = getMouse2D();
041     if (position != null{
042       newton.setX(position.getX());
043     }
044 
045     // (2) Translate apple down screen; velocity * time = distance
046     apple.translateY(0.33 * secondsSinceLastCall);
047 
048     // (3) Check if newton has "caught" the apple
049     if (apple.intersects(newton)) {
050       applesCaught = applesCaught + 1;// another apple caught
051       displayScore.setText("Score: " + applesCaught + "/" +
052         applesDropped);
053       dropApple();
054     }
055 
056     // (4) Check if the apple has hit the ground (y-coordinate >= 1.0)
057     if (apple.getY() >= 1.0{
058       displayScore.setText("Score: " + applesCaught + "/" +
059         applesDropped);
060       dropApple();
061     }
062   }
063 
064   /**
065    * place the apple at the initial height and a random horizontal
066    * location
067    */
068   public void dropApple() {
069     apple.setLocation(randomDouble()0.00);
070     applesDropped = applesDropped + 1;// another apple dropped
071   }
072 
073   /**
074    * The method called by FANG before the game starts. Include all
075    * "one-time" instructions. Our simple computer games will create and
076    * add Sprites here the game begins. Create and add Sprites; Sprites
077    * are visible things in the game. Once they are added to the game
078    * they are automatically redrawn by the game engine.
079    */
080   @Override
081   public void setup() {
082     // initialize the score
083     applesCaught = 0;
084     applesDropped = 0;
085 
086     // The apple is small and red; its initial position is set randomly
087     // in the dropApple routine (called here and when apple bottoms out)
088     // our apple is a circle (x and y dimensions are the same)
089     apple = new OvalSprite(0.100.10);
090     apple.setColor(getColor("red"));
091     dropApple();
092 
093     // newton is small, green, and begins at the middle bottom of the
094     // screen.
095     newton = new RectangleSprite(0.100.10);
096     newton.setColor(getColor("green"));
097     newton.setLocation(0.500.90);
098 
099     displayScore = new StringSprite();
100     displayScore.setLineHeight(0.10);
101     displayScore.setColor(getColor("white"));
102     displayScore.topJustify();// move location to upper-left corner
103     displayScore.leftJustify();
104     displayScore.setLocation(0.000.00);
105     displayScore.setText("Score: " + applesCaught + "/" +
106       applesDropped);
107 
108     // for the sprites to be part of the game, they must be added to the
109     // game itself.
110     addSprite(apple);
111     addSprite(newton);
112     addSprite(displayScore);
113   }
114 }
115 
116 //Uploaded on Mon Mar 29 21:39:55 EDT 2010


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