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