From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch04;
002
003 importimport means to make the classes and/or packages available in this program fang2.core.Game;
004 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
005 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
007
008 /**
009 * NewtonsAppleFallOnce - Randomly placed apple falls slowly down the
010 * screen one time.
011 */
012 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 NewtonsAppleFall
013 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
014 // ----- visual representations of the apple and physicist -----
015 /** on screen representation of the apple; a small red circle */
016 privateprivate is used to restrict access to the current class only OvalSprite apple;
017
018 /** on screen representation of Newton; a small green square */
019 privateprivate is used to restrict access to the current class only RectangleSprite newton;
020
021 // ----- keeping and displaying the score -----
022 /** number of apples caught */
023 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;
024
025 /** number of apples dropped */
026 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;
027
028 /**
029 * on-screen text displaying the current score; contents are updated
030 * whenever the score changes
031 */
032 privateprivate is used to restrict access to the current class only 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 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions secondsSinceLastCall) {open braces start code blocks and must be matched with a close brace
042 apple.translateY(0.30 * secondsSinceLastCall);
043 ifif executes the next statement only if the condition in parenthesis evaluates to true (apple.getY() >=this evaluates to true if the left side is not less than the right side 1.0) {open braces start code blocks and must be matched with a close brace
044 dropApple();
045 }close braces end code blocks and must match an earlier open brace
046 }close braces end code blocks and must match an earlier open brace
047
048 /**
049 * place the apple at the initial height and a random horizontal
050 * location
051 */
052 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
053 apple.setLocation(randomDouble(), 0.00);
054 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
055 }close braces end code blocks and must match an earlier open brace
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 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
066 // initialize the score
067 applesCaught =this assignment operator makes the left side equal to the right side 0;
068 applesDropped =this assignment operator makes the left side equal to the right side 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 =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);
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 =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);
080 newton.setColor(getColor("green"));
081 newton.setLocation(0.50, 0.90);
082
083 displayScore =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor 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.00, 0.00);
090 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
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 }close braces end code blocks and must match an earlier open brace
099 }close braces end code blocks and must match an earlier open brace
100
101 //Uploaded on Mon Mar 29 21:41:20 EDT 2010
|
Download/View scg/ch04/NewtonsAppleFall.java