scg/ch01/ShipShootingTheGame

From FANG

Jump to: navigation, search

01 package scg.ch01;
02 
03 import scg.ch01.spacesprites.Ship;
04 import fang2.attributes.Location2D;
05 import fang2.attributes.Vector2D;
06 import fang2.core.Game;
07 import fang2.transformers.VelocityTransformer;
08 
09 
10 public class ShipShootingTheGame
11   extends Game {
12   /**
13    * the scale of the enemy ships in screens; 0.1 is size of player's
14    * ship
15    */
16   public static final double ESCALE = 0.1;
17 
18   /** number of lives per enemy ship */
19   public static final int ELIVES = 1;
20 
21   /** player's sprite on the screen */
22   private Ship playersShip;
23 
24   /**
25    * Create a new ship for target practice. Made a new command so we can
26    * easily create any number of ships
27    */
28   public void addOneEnemyShip() {
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 = new Ship(this, randomColor(), ESCALE, ELIVES);
33     // random starting location
34     enemy1.setLocation(Location2D.randomLocation());
35     // random moving direction
36     Vector2D heading = Vector2D.randomVector();
37     enemy1.setRotationDegrees(heading.getDegrees());
38     enemy1.addTransformer(new VelocityTransformer(heading));
39     addSprite(enemy1);
40   }
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   public void setup() {
48     playersShip = new Ship(this);           // build one ship
49     playersShip.setLocation(0.50.5);       // place in center of screen
50     playersShip.addCollision(Ship.class);   // what can run into the ship
51     playersShip.setTarget(Ship.class)// 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 for each ship.
58      */
59     addOneEnemyShip();
60     addOneEnemyShip();
61     addOneEnemyShip();
62     addOneEnemyShip();
63   }
64 }
65 
66 //Uploaded on Mon Mar 29 21:42:36 EDT 2010


Download/View scg/ch01/ShipShootingTheGame.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games