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.attributes.Location2D;
004 importimport means to make the classes and/or packages available in this program fang2.core.Game;
005 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
007 importimport means to make the classes and/or packages available in this program 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 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 NewtonsApple
015 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
016 // ----- visual representations of the apple and physicist -----
017 /** on screen representation of the apple; a small red circle */
018 privateprivate is used to restrict access to the current class only OvalSprite apple;
019
020 /** on screen representation of Newton; a small green square */
021 privateprivate is used to restrict access to the current class only RectangleSprite newton;
022
023 // ----- keeping and displaying the score -----
024 /** number of apples caught */
025 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;
026
027 /** number of apples dropped */
028 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;
029
030 /** on-screen current score; update contents when score changes */
031 privateprivate is used to restrict access to the current class only StringSprite displayScore;
032
033 /**
034 * Called by FANG forfor is a looping structure for repeatedly executing a block of code each frame. This is where the game checks forfor is a looping structure for repeatedly executing a block of code
035 * and reacts to user input.
036 */
037 @Override
038 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
039 // (1) Move newton so his x-coordinate matches x-coordinate of mouse
040 Location2D position =this assignment operator makes the left side equal to the right side getMouse2D();
041 ifif executes the next statement only if the condition in parenthesis evaluates to true (position !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
042 newton.setX(position.getX());
043 }close braces end code blocks and must match an earlier open brace
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 ifif executes the next statement only if the condition in parenthesis evaluates to true (apple.intersects(newton)) {open braces start code blocks and must be matched with a close brace
050 applesCaught =this assignment operator makes the left side equal to the right side applesCaught +adds two numbers together or concatenates Strings together 1;// another apple caught
051 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
052 applesDropped);
053 dropApple();
054 }close braces end code blocks and must match an earlier open brace
055
056 // (4) Check if the apple has hit the ground (y-coordinate >= 1.0)
057 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
058 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
059 applesDropped);
060 dropApple();
061 }close braces end code blocks and must match an earlier open brace
062 }close braces end code blocks and must match an earlier open brace
063
064 /**
065 * place the apple at the initial height and a random horizontal
066 * location
067 */
068 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
069 apple.setLocation(randomDouble(), 0.00);
070 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
071 }close braces end code blocks and must match an earlier open brace
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 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
082 // initialize the score
083 applesCaught =this assignment operator makes the left side equal to the right side 0;
084 applesDropped =this assignment operator makes the left side equal to the right side 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 =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);
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 =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);
096 newton.setColor(getColor("green"));
097 newton.setLocation(0.50, 0.90);
098
099 displayScore =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor 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.00, 0.00);
105 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
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 }close braces end code blocks and must match an earlier open brace
114 }close braces end code blocks and must match an earlier open brace
115
116 //Uploaded on Mon Mar 29 21:39:55 EDT 2010
|
Download/View scg/ch04/NewtonsApple.java