home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / de86gnzn / examples / boinkaroids / boinkaroids.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.9 KB  |  209 lines

  1.  
  2. /**
  3.  *
  4.  * Boinkaroids.java
  5.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  6.  * @version    0.8
  7.  * Mar 27/1996
  8.  *
  9.  * Boinkaroids is an Asteroids type game developed for the purpose of showing
  10.  * off the Gamelet Toolkit.  It makes use of all of the features within the
  11.  * Gamelet Toolkit.
  12.  *
  13.  */
  14.  
  15. import java.awt.*;
  16. import java.io.*;
  17. import java.net.*;
  18. import java.util.*;
  19.  
  20. import com.next.gt.*;
  21.  
  22. public class Boinkaroids extends Gamelet implements BonusHandler, EventHandler{
  23.  
  24.   //
  25.   // Boinkaroids variables
  26.   //
  27.   public Ship        player;
  28.   public int        numShips;
  29.   public int        badGuyCount;
  30.   public int        level;
  31.   
  32.   //
  33.   // Should a player be created?
  34.   //
  35.   private boolean    createNewPlayer;
  36.   
  37.   //
  38.   // Score label.
  39.   //
  40.   private Label        myLabel;
  41.   
  42.   //
  43.   // This area that has to be clear for a ship to enter level.
  44.   //
  45.   private int        SAFETY_ZONE_WIDTH= 128;
  46.   private int        SAFETY_ZONE_HEIGHT= 128;
  47.   
  48.   
  49. /**
  50.  * Initialize.
  51.  */
  52. public void init() {
  53.   //
  54.   // cache images
  55.   //
  56.   new ImageManager(this);  
  57.   
  58.   //
  59.   // initialize the score manager
  60.   //
  61.   scoreManager= new ScoreManager();
  62.   scoreManager.registerForBonusNotification(this, 20000);
  63.   
  64.   
  65.   myLabel= new Label("Score: 00000000" + "   Ships: " + numShips
  66.                       + "   Level: "+ level);
  67.   add("South",myLabel);
  68.   
  69.   this.newGame();
  70.  
  71.  
  72.  //
  73.   // register for events
  74.   //
  75.   eventManager.
  76.     registerForSingleEventNotification(this,Event.KEY_ACTION_RELEASE);
  77.   
  78.   //
  79.   // paint background image
  80.   //
  81.   displayManager.setBackgroundTile (getImage(getCodeBase(), "images/background.gif"));
  82.  
  83. } /*init*/
  84.  
  85.  
  86.  
  87. /**
  88.  * Set up the new game.
  89.  */
  90. public void newGame() {
  91.   scoreManager.setScore (0);
  92.   numShips= 3;
  93.   badGuyCount= 0;
  94.   level= 0;
  95.   player= null;
  96.   actorManager.removeAllActors();
  97.   this.createActors();
  98. } /*newGame*/
  99.  
  100.  
  101.  
  102. /**
  103.  * Advance levels.
  104.  */
  105. public void newLevel() {
  106.   level++;
  107.   this.createActors();
  108. } /*newLevel*/
  109.  
  110.  
  111. /**
  112.  * Create the actors for this scene.
  113.  */
  114. public void createActors() {
  115.   for (int i= 0; i< 2*level; i++) {
  116.      actorManager.addActor (new Asteroid(this, null, "gumball", Asteroid.LARGE_SIZE));
  117.      badGuyCount++; 
  118.   } /*nexti*/ 
  119.   for (int i= 0; i< 0.5*level; i++) {
  120.     actorManager.addActor (new Bigoobie(this));
  121.     badGuyCount++; 
  122.   } /*next_i*/
  123.   this.createPlayer();
  124. } /*createActors*/
  125.  
  126.  
  127.  
  128. /**
  129.  * Create the player object.
  130.  */
  131. public void createPlayer() {
  132.   if (player!=null) {
  133.     actorManager.removeActor(player);
  134.   } /*endif*/
  135.   player= new Ship(this);
  136.   actorManager.addActor (player);
  137. } /*createPlayer*/
  138.  
  139.  
  140.  
  141. /**
  142.  * Override tick to test for specific game events.
  143.  */
  144. public void tick() {
  145.  
  146. super.tick();
  147.  
  148.  if (badGuyCount <= 0) {
  149.    this.newLevel();
  150.  } /*endif*/
  151.  if (createNewPlayer) {
  152.    Rectangle myRectangle= new Rectangle((int)this.size().width/2 - SAFETY_ZONE_WIDTH/2,
  153.                                         (int)this.size().height/2 - SAFETY_ZONE_HEIGHT/2,
  154.                                         SAFETY_ZONE_WIDTH,
  155.                                         SAFETY_ZONE_HEIGHT);
  156.    if (!actorManager.isActorIn(myRectangle)) {
  157.      createNewPlayer= false;
  158.      this.createPlayer();
  159.    } /*endif*/
  160.  } /*endif*/
  161. } /*tick*/
  162.  
  163.  
  164.  
  165. /**
  166.  * Handle keyboard events to restart game.
  167.  */
  168. public boolean handleRequestedEvent (Event theEvent) {
  169.   switch(theEvent.id) {
  170.   case Event.KEY_ACTION_RELEASE:
  171.     switch(theEvent.key) {
  172.       case Event.F1:
  173.        this.newGame();
  174.         return true;
  175.     } /*endSwitch*/
  176.   } /*endSwitch*/
  177.   return false;
  178. } /*handleRequestedEvent8/
  179.  
  180.  
  181.  
  182. /**
  183.  * Override paint to display score bar.
  184.  */
  185. public void paint(Graphics g) {
  186.   super.paint(g);
  187.   myLabel.setText("Score: " + scoreManager.score + "   Ships: " + numShips
  188.                       + "   Level: "+ level );
  189. } /*paint*/
  190.  
  191.  
  192.  
  193. /**
  194.  * Give bonus ship for getting a certain score.
  195.  */
  196. public void didAchieveBonus() {
  197.   numShips++;
  198. } /*didAchieveBonus*/
  199.  
  200.  
  201.  
  202. /**
  203.  * Player must have died, decrement ship count.
  204.  */
  205. public void decrementShipCount() {
  206.   if (numShips-- > 0) {
  207.     createNewPlayer= true;
  208.     player= null;
  209.   } /*endif*/
  210. } /*decrementShipCount*/
  211. } /*Boinkaroids*/
  212.