From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch04;
02
03 // import fang2.attributes.Location2D;
04 importimport means to make the classes and/or packages available in this program fang2.core.Game;
05 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
06 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
07 importimport means to make the classes and/or packages available in this program 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 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects NewtonsAppleJustSetup
17 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
18 // ----- visual representations of the apple and physicist -----
19 /** on screen representation of the apple; a small red circle */
20 privateprivate is used to restrict access to the current class only OvalSprite apple;
21
22 /** on screen representation of Newton; a small green square */
23 privateprivate is used to restrict access to the current class only RectangleSprite newton;
24
25 // ----- keeping and displaying the score -----
26 /** number of apples caught */
27 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer applesCaught;
28
29 /** number of apples dropped */
30 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer applesDropped;
31
32 /** on-screen current score; update contents when score changes */
33 privateprivate is used to restrict access to the current class only StringSprite displayScore;
34
35 /**
36 * place the apple at random x-coord at top of screen.
37 */
38 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value dropApple() {open braces start code blocks and must be matched with a close brace
39 apple.setLocation(randomDouble(), 0.00);
40 applesDropped =this assignment operator makes the left side equal to the right side applesDropped +adds two numbers together or concatenates Strings together 1;// another apple dropped
41 }close braces end code blocks and must match an earlier open brace
42
43 /**
44 * The method called by FANG before the game starts. Include all
45 * "one-time" instructions in setup.
46 */
47 @Override
48 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
49 // initialize the score
50 applesCaught =this assignment operator makes the left side equal to the right side 0;
51 applesDropped =this assignment operator makes the left side equal to the right side 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 =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(0.10, 0.10);
56 apple.setColor(getColor("red"));
57 dropApple();
58
59 // newton is small, green, at the middle bottom of the screen.
60 newton =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(0.10, 0.10);
61 newton.setColor(getColor("green"));
62 newton.setLocation(0.50, 0.90);
63
64 displayScore =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor 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.00, 0.00);
70
71 displayScore.setText("Score: " +adds two numbers together or concatenates Strings together applesCaught +adds two numbers together or concatenates Strings together "/" +adds two numbers together or concatenates Strings together
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 }close braces end code blocks and must match an earlier open brace
79 }close braces end code blocks and must match an earlier open brace
80
81 //Uploaded on Mon Mar 29 21:41:33 EDT 2010
|
Download/View scg/ch04/NewtonsAppleJustSetup.java