scg/ch04/NewtonsAppleJustSetup

From FANG

Jump to: navigation, search

01 package scg.ch04;
02 
03 // import fang2.attributes.Location2D;
04 import fang2.core.Game;
05 import fang2.sprites.OvalSprite;
06 import fang2.sprites.RectangleSprite;
07 import fang2.sprites.StringSprite;
08 
09 /**
10  * NewtonsAppleJustSetup - Randomly placed apples fall and
11  * mouse-controlled Newton must be under them when they fall. Score c/d
12  * where c is caught apples, d dropped apples:
13  *
14  <p>NOT YET PLAYABLE
15  */
16 public class NewtonsAppleJustSetup
17   extends Game {
18   // ----- visual representations of the apple and physicist -----
19   /** on screen representation of the apple; a small red circle */
20   private OvalSprite apple;
21 
22   /** on screen representation of Newton; a small green square */
23   private RectangleSprite newton;
24 
25   // ----- keeping and displaying the score -----
26   /** number of apples caught */
27   private int applesCaught;
28 
29   /** number of apples dropped */
30   private int applesDropped;
31 
32   /** on-screen current score; update contents when score changes */
33   private StringSprite displayScore;
34 
35   /**
36    * place the apple at random x-coord at top of screen.
37    */
38   public void dropApple() {
39     apple.setLocation(randomDouble()0.00);
40     applesDropped = applesDropped + 1;// another apple dropped
41   }
42 
43   /**
44    * The method called by FANG before the game starts. Include all
45    * "one-time" instructions in setup.
46    */
47   @Override
48   public void setup() {
49     // initialize the score
50     applesCaught = 0;
51     applesDropped = 0;
52 
53     // The apple is small and red; its initial position is set randomly
54     // in the dropApple routine (called here and when apple bottoms out)
55     apple = new OvalSprite(0.100.10);
56     apple.setColor(getColor("red"));
57     dropApple();
58 
59     // newton is small, green, at the middle bottom of the screen.
60     newton = new RectangleSprite(0.100.10);
61     newton.setColor(getColor("green"));
62     newton.setLocation(0.500.90);
63 
64     displayScore = new StringSprite();
65     displayScore.setLineHeight(0.10);
66     displayScore.setColor(getColor("white"));
67     displayScore.topJustify();// move location to upper-left corner
68     displayScore.leftJustify();
69     displayScore.setLocation(0.000.00);
70 
71     displayScore.setText("Score: " + applesCaught + "/" +
72       applesDropped);
73 
74     // must add all sprites we want drawn (or moved) to the game
75     addSprite(apple);
76     addSprite(newton);
77     addSprite(displayScore);
78   }
79 }
80 
81 //Uploaded on Mon Mar 29 21:41:33 EDT 2010


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