home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / Boinkaroids.java < prev    next >
Encoding:
Java Source  |  1998-04-20  |  4.3 KB  |  238 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.awt.event.*;
  17. import java.util.*;
  18.  
  19. import com.next.gt.*;
  20.  
  21. public class Boinkaroids extends Gamelication implements BonusHandler, KeyListener{
  22.  
  23.     //
  24.     // Boinkaroids variables
  25.     //
  26.     public Ship        player = null;
  27.     public int        numShips;
  28.     private int        badGuyCount;
  29.     public int        level;
  30.   
  31.     //
  32.     // Should a player be created?
  33.     //
  34.     public boolean    createNewPlayer = true;
  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.     super.init( 500, 400, "./" );
  53.     Frame f = new Frame( "Boinkaroids" );
  54.     f.setSize( 500, 500 );
  55.     myLabel= new Label("Score: 00000000" + "   Ships: " + numShips
  56.               + "   Level: "+ level);
  57.     f.add("South",myLabel);
  58.  
  59.     f.add( "Center",this );
  60.     f.addWindowListener( new WindowDestroyer( f, this ) );
  61.     f.setResizable( false );
  62.   
  63.     //
  64.     // initialize the score manager
  65.     //
  66.     scoreManager= new ScoreManager();
  67.     scoreManager.registerForBonusNotification(this, 20000);  
  68.  
  69.   
  70.     //
  71.     // show the main window
  72.     //
  73.     f.show();  
  74.  
  75.     //
  76.     // cache images
  77.     //
  78.     new ImageManager(this);
  79.   
  80.     myLabel.addKeyListener( this );
  81.  
  82.     this.newGame();  
  83.     //
  84.     // paint background image
  85.     //
  86.     displayManager.setBackgroundTile (getImage(getCodeBase(), "images/background.gif"));
  87.  
  88.     start();
  89.     run();
  90. } /*init*/
  91.  
  92.  
  93. /**
  94.  * Set up the new game.
  95.  */
  96. public void newGame() {
  97.     scoreManager.setScore (0);
  98.     numShips= 3;
  99.     badGuyCount= 0;
  100.     level= 0;
  101.     removePlayer();
  102.     actorManager.removeAllActors();
  103.     this.createActors();
  104. } /*newGame*/
  105.  
  106.  
  107. /**
  108.  * Advance levels.
  109.  */
  110. public void newLevel() {
  111.     level++;
  112.     actorManager.removeAllActors();
  113.     this.createActors();
  114. } /*newLevel*/
  115.  
  116.  
  117. /**
  118.  * Create the actors for this scene.
  119.  */
  120. public void createActors() {
  121.     for (int i= 0; i< 2*level; i++) {
  122.         actorManager.addActor (new Asteroid(this, null, "gumball", 
  123.                 Asteroid.LARGE_SIZE));
  124.         addOneBadGuy();
  125.     } /*nexti*/ 
  126.     for (int i= 0; i< 0.5*level; i++) {
  127.         actorManager.addActor (new Bigoobie(this));
  128.         addOneBadGuy(); 
  129.     } /*next_i*/
  130.     this.createPlayer();
  131. } /*createActors*/
  132.  
  133. /**
  134.  *  Remove one bad guy
  135.  */
  136. public void removeOneBadGuy(){
  137.     badGuyCount--;
  138. }
  139.  
  140. /**
  141.  *  Remove one bad guy
  142.  */
  143. public void addOneBadGuy(){
  144.     badGuyCount++;
  145. }
  146.  
  147. /**
  148.  * Create the player object.
  149.  */
  150. public void createPlayer() {
  151.     removePlayer();
  152.     player = new Ship(this);
  153.     myLabel.addKeyListener(player);
  154.     actorManager.addActor (player);
  155.     createNewPlayer = false;
  156. } /*createPlayer*/
  157.  
  158.  
  159. /**
  160.  * Remove the player object.
  161.  */
  162. public void removePlayer() {
  163.     if (player!=null) {
  164.         actorManager.removeActor(player);
  165.         myLabel.removeKeyListener( player );
  166.         player = null;
  167.     } /*endif*/
  168. } /*createPlayer*/
  169.  
  170.  
  171. /**
  172.  * Override tick to test for specific game events.
  173.  */
  174. public void tick() {
  175.     super.tick();
  176.     myLabel.setText("Score: " + scoreManager.score + "   Ships: " + numShips
  177.               + "   Level: "+ level );
  178.               
  179.     if (badGuyCount <= 0)
  180.         this.newLevel();
  181.         
  182.     if (createNewPlayer) {
  183.         Rectangle myRectangle= new Rectangle((int)this.getSize().width/2 - SAFETY_ZONE_WIDTH/2,
  184.                     (int)this.getSize().height/2 - SAFETY_ZONE_HEIGHT/2,
  185.                 SAFETY_ZONE_WIDTH,
  186.                 SAFETY_ZONE_HEIGHT);
  187.         if (!actorManager.isActorIn(myRectangle)) {
  188.             createNewPlayer = false;
  189.             this.createPlayer();
  190.         } /*endif*/
  191.     } /*endif*/
  192. } /*tick*/
  193.  
  194.  
  195. /**
  196.  * Handle keyboard events that control ship.
  197.  */
  198. public void keyPressed( KeyEvent k ){
  199.     if(k.getKeyCode() == k.VK_F1 )
  200.         this.newGame();
  201. }
  202.  
  203. public void keyReleased( KeyEvent k ){
  204. }
  205.  
  206. public void keyTyped( KeyEvent k ){
  207. }
  208.  
  209.  
  210. /**
  211.  * Give bonus ship for getting a certain score.
  212.  */
  213. public void didAchieveBonus() {
  214.     numShips++;
  215. } /*didAchieveBonus*/
  216.  
  217.  
  218. /**
  219.  * Player must have died, decrement ship count.
  220.  */
  221. public void decrementShipCount() {
  222.     if (numShips-- > 0) {
  223.           createNewPlayer= true;
  224.         removePlayer();
  225.     } /*endif*/
  226. } /*decrementShipCount*/
  227.  
  228.  
  229. /**
  230.  * Static main to begin the application.
  231.  */
  232. public static void main( String[] args ) {
  233.     Boinkaroids boinkaroids = new Boinkaroids();
  234.     boinkaroids.init();
  235. }
  236.  
  237. } /*Boinkaroids*/
  238.