home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / BRICK / BRICK.EXE / BricksGame.java < prev    next >
Encoding:
Java Source  |  1996-04-23  |  5.9 KB  |  271 lines

  1.  
  2. import java.awt.*;
  3. import java.util.Vector;
  4. import java.util.Date;
  5. import java.util.Random;
  6. import java.lang.Math;
  7.  
  8.  
  9. public class BricksGame extends Canvas {
  10.   private BricksApplet parent;
  11.   private Color brickColor;
  12.   Image osI;
  13.   Graphics osG;
  14.   int rows, cols, level=0;
  15.   int radius;
  16.  
  17.   Brick bricks[][];
  18.   Ball ball;
  19.   Paddle paddle;
  20.   Wall wall;
  21.  
  22.   public BricksGame (BricksApplet parent) {
  23.     System.out.println("Bricks:Creating");
  24.  
  25.     this.parent = parent;
  26.  
  27.     level = 0;
  28.     rows = 4; cols = 6;
  29.     brickColor = Color.red;
  30.   }
  31.  
  32.   public void nextLevel() {
  33.  
  34.     level++;
  35.     System.out.println(this + "Next Level (" + level + ").");
  36.  
  37.     switch (level) {
  38.       case 1:
  39.         rows = 4; cols = 8;
  40.         brickColor = Color.blue;
  41.         break;
  42.       case 2:
  43.         rows = 5; cols = 10;
  44.         brickColor = Color.green;
  45.         break;
  46.       case 3:
  47.         rows = 6; cols = 12;
  48.         brickColor = Color.magenta;
  49.         break;
  50.       case 4:
  51.         rows = 7; cols = 14;
  52.         brickColor = Color.white;
  53.         break;
  54.       default:
  55.         rows = 3 + level; cols = 6 + 2 * level;
  56.         brickColor = Color.magenta;
  57.       }
  58.  
  59.       System.out.println(this + "Level " + level + ".");
  60.  
  61.       osI = null;
  62.       osG = null;
  63.  
  64.       repaint();
  65.  
  66.   }
  67.  
  68.   public void pause () {
  69.     System.out.println(this + "Pause");
  70.     if (ball != null) ball.suspend();
  71.   }
  72.  
  73.   public void resume () {
  74.     System.out.println(this + "resume");
  75.     if (ball != null) ball.resume();
  76.   }
  77.  
  78.   public void stop () {
  79.     System.out.println(this + "Stop");
  80.     if (ball != null) ball.stop();
  81.   }
  82.  
  83.   public void destroy () {
  84.     System.out.println(this + "Destroy");
  85.     if (ball != null) ball.stop();
  86.   }
  87.  
  88.   public void start () {
  89.     System.out.println(this + "Start");
  90.     if (ball != null) ball.start();
  91.   }
  92.  
  93.   public void reset () {
  94.     System.out.println(this + "Reset");
  95.  
  96.     rows = 4; cols = 6;
  97.     brickColor = Color.red;
  98.  
  99.     if (ball != null) ball.stop();
  100.  
  101.     resize();
  102.     repaint();
  103.   }
  104.  
  105.   public void play(int sound) {
  106.     parent.getEfx()[sound].play();
  107.   }
  108.  
  109.   public void resize () {
  110.     System.out.println("Bricks:Setup.");
  111.  
  112.     osI = this.createImage(this.size().width, this.size().height);
  113.     osG = osI.getGraphics();
  114.     this.setBackground(Color.black);
  115.     this.setForeground(Color.white);
  116.  
  117.     System.out.println("Bricks:Setup Objects (" +
  118.         this.size().width + "," + this.size().height + ").");
  119.  
  120.     wall = new Wall(1, 1, this.size().width - 1, this.size().height - 1, Color.green, osG);
  121.  
  122.     bricks = new Brick[rows][cols];
  123.     int dw, dh, top;
  124.     dw = (int) (wall.width / cols);
  125.     dh = (int) (wall.height * 0.4f / rows);
  126.     top = dh;
  127.  
  128.     System.out.println("Bricks: dw=" + dw + ", dh=" + dh + ", top=" + top + ".");
  129.     
  130.     int x, y;
  131.  
  132.     for (y=0; y < rows; ++y) {
  133.       if (y > 0) brickColor = brickColor.darker();
  134.  
  135.       for (x=0; x < cols; ++x) 
  136.         bricks[y][x] = new Brick(
  137.         wall.left + (int) (dw * x + 0.025f*dw), 
  138.         wall.top + (int) (top + y * dh + 0.05f*dh),
  139.         wall.left + (int) (dw * (x+1) - 0.05f*dw), 
  140.         wall.top + (int) (top + dh * (y+1) - 0.05f*dh),
  141.                 brickColor,
  142.                 rows - y + 1);
  143.     }
  144.  
  145.     bricks[0][0].setGraphics(osG);
  146.  
  147.     Random rand = new Random();
  148.  
  149.     paddle = new Paddle(
  150.         (int) (wall.width * 0.8f * rand.nextFloat() + wall.width * 0.1f),
  151.     (int) (wall.height * 0.95f),
  152.     (int) (wall.width * 0.1f),
  153.     (int) (wall.height * 0.02f),
  154.     Color.red, osG);
  155.  
  156.     // clear the offscreen
  157.     osG.setColor(this.getBackground());
  158.     osG.fillRect(0, 0, this.size().width, this.size().height);
  159.     osG.setColor(this.getForeground());
  160.  
  161.     // this is actually the first time here
  162.     redraw();
  163.  
  164.     ball = new Ball(
  165.     wall.left + (int) (wall.width * 0.8f * rand.nextFloat() + wall.width * 0.05f),
  166.         wall.top + (int) (wall.height * 0.2f * rand.nextFloat() + top + rows * dh + radius + 2),
  167.         (int) (wall.width * 0.03f), 
  168.     level, 
  169.         ball != null ? ball.getScore() : 0,
  170.     Color.white);
  171.     ball.init(this, osG);
  172.  
  173.     // start the ball in motion
  174.     ball.start();
  175.  
  176.   }
  177.  
  178.   public void redraw () {
  179.   // what to do after an impact
  180.  
  181.     // draw the bricks
  182.     int i, j;
  183.     for (i=0; i < cols; ++i) 
  184.       for (j=0; j < rows; ++j) 
  185.         bricks[j][i].draw();
  186.  
  187.     // draw the wall
  188.     wall.draw();
  189.   }
  190.     
  191.  
  192.   public void update (Graphics g) {
  193.     paint(g);
  194.   }
  195.  
  196.   public void paint (Graphics g) {
  197.  
  198.     if (osI == null) 
  199.       this.resize();
  200.  
  201.     // draw the paddle
  202.     paddle.draw();
  203.  
  204.     // copy the offscreen to the real screen
  205.     g.drawImage(osI, 0, 0, this);
  206.   }
  207.  
  208.   public boolean mouseDown(Event e, int x, int y) {
  209.     System.out.println(this + "Coordinate (" + x + "," + y + ").");
  210.     return true;
  211.   }
  212.  
  213.   public boolean mouseDrag (Event e, int x, int y) {
  214.  
  215.     paddle.undraw();
  216.  
  217.     if (x > this.size().width) {
  218.       x = this.size().width - paddle.width - 2;
  219.       wall.draw();
  220.     } else if (x < -1 * paddle.width) {
  221.       x = 2;
  222.       wall.draw();
  223.     }
  224.  
  225.     paddle.moveX(x);
  226. //    ball.undraw();
  227. //    ball.move(x, y);
  228.  
  229.     repaint();
  230.     return true;
  231.   }
  232.  
  233.   public boolean mouseUp (Event e, int x, int y) {
  234. /*    System.out.println("Lines:New Line.");
  235.  
  236.     if (e.shiftDown()) {
  237.       if (java.lang.Math.abs(end.x - start.x) > java.lang.Math.abs(end.y - start.y)) 
  238.         y=start.y;
  239.       else
  240.         x=start.x;
  241.     }
  242.     end.move(x, y);
  243.  
  244.     tmp = new Rect(start, end);
  245.     lines.addElement(tmp);
  246.  
  247.     parent.points.addItem("(" + tmp.x1 + "," + tmp.y1 + ") :  (" + tmp.x2 + "," + tmp.y2 + ")");
  248.     parent.points.makeVisible(parent.points.countItems()-1);
  249.  
  250.     start = null; end = null;
  251. */
  252.     repaint();
  253.  
  254.     return true;
  255.   }
  256.  
  257.   public final Ball getBall() {
  258.     return ball;
  259.   }
  260.  
  261.   public final Wall getWall() {
  262.     return wall;
  263.   }
  264.  
  265.   public String toString () {
  266.     return "BricksGame::";
  267.   }
  268.  
  269. }
  270.  
  271.