From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch07;
02
03 importimport means to make the classes and/or packages available in this program 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.RectangleSprite;
06
07 /**
08 * Demonstration of multi-player game techniques. Constructor sets
09 * number of required players to 2. When 2 players have joined, game can
10 * be started. Game looks forfor is a looping structure for repeatedly executing a block of code mouse clicks and draws random rectangles.
11 * Note that rectangles are the same on both screens.
12 */
13 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 ColoredRectanglesMP
14 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
15 /**
16 * Construct a newnew is used to create objects by calling the constructor ColoredRectangleMP object; initialize the Game part
17 * and then set the player count to 2 (make it multi-player).
18 */
19 publicpublic is used to indicate unrestricted access (any other class can have access) ColoredRectanglesMP() {open braces start code blocks and must be matched with a close brace
20 super();
21 setNumberOfPlayers(2);
22 players =this assignment operator makes the left side equal to the right side 2;
23 }close braces end code blocks and must match an earlier open brace
24
25 /**
26 * Advance one frame: iterate through all players looking forfor is a looping structure for repeatedly executing a block of code mouse
27 * clicks. If there is a mouse click, create a random rectangle
28 * centered at click location.
29 */
30 @Override
31 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 dT) {open braces start code blocks and must be matched with a close brace
32 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 p =this assignment operator makes the left side equal to the right side 0; p !=this is the not equals operator which evaluates to true if both sides are different getNumberOfPlayers(); ++this is the increment operator, which increases the variable by 1p) {open braces start code blocks and must be matched with a close brace
33 Location2D rectangleLocation =this assignment operator makes the left side equal to the right side getClick2D(p);
34
35 ifif executes the next statement only if the condition in parenthesis evaluates to true (rectangleLocation !=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// was mouse(p) clicked?
36 RectangleSprite rectangle =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(randomDouble(),
37 randomDouble());
38 rectangle.setColor(randomColor());
39 rectangle.setLocation(rectangleLocation);
40 addSprite(rectangle);
41 }close braces end code blocks and must match an earlier open brace
42 }close braces end code blocks and must match an earlier open brace
43 }close braces end code blocks and must match an earlier open brace
44 }close braces end code blocks and must match an earlier open brace
45
46 //Uploaded on Mon Mar 29 21:40:35 EDT 2010
|
Download/View scg/ch07/ColoredRectanglesMP.java