From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch09;
02
03 importimport means to make the classes and/or packages available in this program fang2.core.*;
04 importimport means to make the classes and/or packages available in this program fang2.sprites.*;
05 importimport means to make the classes and/or packages available in this program fang2.transformers.*;
06
07 /**
08 * This is a shell of a game with a caterpillar that follows the mouse.
09 *
10 * @authorthis is the Javadoc tag for documenting who created the source code Jam Jenkins
11 */
12 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 Caterpillar
13 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
14 /** an invisible sprite the caterpillar's head follows */
15 privateprivate is used to restrict access to the current class only Sprite follow;
16
17 /**
18 * makes the follow sprite locate where the mouse is
19 */
20 @Override
21 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance() {open braces start code blocks and must be matched with a close brace
22 follow.setLocation(getMouse2D());
23 }close braces end code blocks and must match an earlier open brace
24
25 /**
26 * makes and adds the caterpillar and invisible sprite
27 */
28 @Override
29 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
30 // follow is invisible because it is not added
31 follow =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(0.01, 0.01);
32 follow.setColor(getColor("Yellow"));
33 follow.setLocation(0.1, 0.1);
34
35 // the first segment tracks follow
36 Sprite previous =this assignment operator makes the left side equal to the right side follow;
37 // make 10 segments each following each other
38 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer k =this assignment operator makes the left side equal to the right side 0; k < 10; k++this is the increment operator, which increases the variable by 1) {open braces start code blocks and must be matched with a close brace
39 Sprite segment =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(0.1, 0.1);
40 segment.setLocation(0.5 +adds two numbers together or concatenates Strings together (0.05 * k), 0.5);
41 segment.setColor(getColor("Green"));
42 AttractTransformer attractor =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor AttractTransformer(0.2);
43 attractor.setTarget(previous);
44 // make sure the segments don't get too close
45 attractor.setMinDistance(0.05);
46 segment.addTransformer(attractor);
47 addSprite(segment);
48 // the current segment should be followed next
49 previous =this assignment operator makes the left side equal to the right side segment;
50 }close braces end code blocks and must match an earlier open brace
51 }close braces end code blocks and must match an earlier open brace
52 }close braces end code blocks and must match an earlier open brace
53
54 //Uploaded on Mon Mar 29 21:41:25 EDT 2010
|
Download/View scg/ch09/Caterpillar.java