From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch01;
02
03 importimport means to make the classes and/or packages available in this program scg.ch01.spacesprites.Ship;
04 importimport means to make the classes and/or packages available in this program fang2.attributes.Location2D;
05 importimport means to make the classes and/or packages available in this program fang2.attributes.Vector2D;
06 importimport means to make the classes and/or packages available in this program fang2.core.Game;
07 importimport means to make the classes and/or packages available in this program fang2.transformers.VelocityTransformer;
08
09
10 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 ShipShootingTheGame
11 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
12 /**
13 * the scale of the enemy ships in screens; 0.1 is size of player's
14 * ship
15 */
16 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions ESCALE =this assignment operator makes the left side equal to the right side 0.1;
17
18 /** number of lives per enemy ship */
19 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer ELIVES =this assignment operator makes the left side equal to the right side 1;
20
21 /** player's sprite on the screen */
22 privateprivate is used to restrict access to the current class only Ship playersShip;
23
24 /**
25 * Create a newnew is used to create objects by calling the constructor ship forfor is a looping structure for repeatedly executing a block of code target practice. Made a newnew is used to create objects by calling the constructor command so we can
26 * easily create any number of ships
27 */
28 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value addOneEnemyShip() {open braces start code blocks and must be matched with a close brace
29 // enemy ship is part of this game, has random color, the constant
30 // scale, and the fixed number of lives; enemy ships don't show
31 // their life count
32 Ship enemy1 =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Ship(thisthis means the current object (the implicit parameter), randomColor(), ESCALE, ELIVES);
33 // random starting location
34 enemy1.setLocation(Location2D.randomLocation());
35 // random moving direction
36 Vector2D heading =this assignment operator makes the left side equal to the right side Vector2D.randomVector();
37 enemy1.setRotationDegrees(heading.getDegrees());
38 enemy1.addTransformer(newnew is used to create objects by calling the constructor VelocityTransformer(heading));
39 addSprite(enemy1);
40 }close braces end code blocks and must match an earlier open brace
41
42 /**
43 * Setup the elements in the game. Make the player's ship and then
44 * construct as many enemy ships as we want to.
45 */
46 @Override
47 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
48 playersShip =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Ship(thisthis means the current object (the implicit parameter)); // build one ship
49 playersShip.setLocation(0.5, 0.5); // place in center of screen
50 playersShip.addCollision(Ship.classclass is a group of fields and methods used for making objects); // what can run into the ship
51 playersShip.setTarget(Ship.classclass is a group of fields and methods used for making objects); // what can ship shoot
52 playersShip.showLives(); // show life count
53 playersShip.showScore(); // show score
54 addSprite(playersShip);
55
56 /* add four enemy ships; random placement and color will be
57 * different forfor is a looping structure for repeatedly executing a block of code each ship.
58 */
59 addOneEnemyShip();
60 addOneEnemyShip();
61 addOneEnemyShip();
62 addOneEnemyShip();
63 }close braces end code blocks and must match an earlier open brace
64 }close braces end code blocks and must match an earlier open brace
65
66 //Uploaded on Mon Mar 29 21:42:36 EDT 2010
|
Download/View scg/ch01/ShipShootingTheGame.java