⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beatball.java

📁 大学课程java程序设计常用小游戏程序源代码
💻 JAVA
字号:


import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;


public class BeatBall extends Applet implements Runnable
{
  static int MAX_X=9, MAX_Y=10, MAX_LVL=100, MAX_IMG=11, MAX_HIST=100;
  static int X_0 = 5, Y_0 = 5; //静态变量说明

  Thread game0 = null;

  private Image[] xImg = new Image[MAX_IMG+1];
  private int xPos, yPos, gLevel, mvPos, maxMove;
  private byte[][] currCanvas = new byte[MAX_Y+1][MAX_X+1];
  private byte[] xMove = new byte[MAX_HIST];
  private byte firstTime;

  private Button btn1  = new Button("X");
  private Button btnN  = new Button(">");
  private Button btnP  = new Button("<");
  //定义按钮
  private static String stGameEnd = " [GAME END]";
  private static String stYouLost = " [YOU LOST]";
  //定义游戏状态
  private static String widthe = " #    *.oO@+,123456789abcdefghij";
  private static int xCoord[][] =
    {{  0, 0,20,20}, { 20, 0,20,20}, { 40, 0,20,20}, { 60, 0,20,20}, { 80, 0,20,20}, {100, 0,20,20},
     {100,20,20,20}, {  0,20,20,20}, { 20,20,20,20}, { 40,20,20,20}, { 60,20,20,20}, { 80,20,20,20}};

  private static String biDat[] = {
    ", #7, #o.o.o.#, # 5#, # # # #1, # # 3#, # 4#1, #6,",
    ",, 1#5, 1#.* 1#, 1# 3#, 1#. 2#, 1# 1.#1, 1#5,",
    ", 2#3, 1#1 .#1, 1# 3#, 1#. o #, 1# 1# #, 1# 1. #, 1#1o #1, 2#3,",
    ",, 1#5, 1# 1. #, 1#1 1#1, 1# 2.#, 1#. 1#1, 1#4,",
    ", 1#2, #1 #2, # . 1#1, #* . 1#1, # 1* . #, # # 1#2, #5,",
    ", #7, #o 3o#, # # 1# #, # 1. 2#, #1. . #1, # 1o . #, #7,",
    ", 1#5, #1 . *#1, # 4.#, # 5#, #. 4#, #1 3#1, 1#1* #1, 2#3,",
    ", #6, # 3.#, #. 2#1, # 1* 1#, # 3#1, #2.#1, 2#2,",
    ",,#9,# 1. 2* #,#1 1*. 1#1, # *. * #, #. 3.#, #7,",
    ", 2#4, 1#1 . #1, #1 4#, #. 3.#, # 2#1*#, #7,",
    ", 4#2, #4.#1, #. 4#, #1 2* #, 1#1 # #1, 2# #2, 2# 1.#, 2#4,",
    ", #6, # 4#, #1 .* #, #. 3#, #* 2.#, # 2* #, #6,",
    " #7, # 1. 2#,#1 1*# 1#,# 2.# 1#,#1 #1 2#, # 1* 2#, # 1. 2#, # 5#, # 5#, #7,",
    ", #6, # .# o#1, #1 3o#, 1# 3#1, 1#* 2.#, 1#.o 1#1, 1#5,",
    ", 1#5, #1. 2#, # 3.#, #1 * #1, # 4#, #1 2.#, 1#5,",
    ", 2#2, 1#1 #3, #1 4#, # 5#, # .o#.o#, #1o#2.#, 1#2 #2,",
    ", 3#2, #3O#1, # o. 1#, # # o.#2,#1 4O*#,# 5o#1,#1 O# 1#1, #6,",
    " 4#2, 2#2 #1, 1#1. 2#, 1# 2o#1, #2 . 1#, # 1o .#1, #1 1o#1, 1#4,",
    ", 1#5, #1 1o #1, #o 3.#, #. 2* #, #1 . 2#, 1#3 #1, 4#2,",
    ", #4, #. 1#1, # 3#1, # o o.#, #2. 1#, 2# 2#, 2#4,",
    ", 2#4, 1#1 O #, #1 3#1,#1.O 3#,# 2o 1#1,# 1#2.#,# O o 1#,#7,",
    " #2,#1 #4,#. 2O #,# 2#1.#1,# 1Oo# 1#,# o# 2#1,# 2. o #,#O 1#1 #1,#7,",
    " 1#2, 1# #2, 1# 1o#1, 1#*. o#1, 1# #O o#, #1 2# #, # . 2O#, #1O 2#1, 1#5,",
    "#8,# Oo.oO #,# 2o 2#,# 2O 2#,# 2o 2#,# o . O #,#1 1# 1#1, #6,",
    " 1#4, #1 # #1, # .# 1#1, #1 #. 1#, # 5#, #1 * 1.#, 1#1 2#1, 2#1 #1, 3#2,",
    " 1#5, #1 1# #1, #.*o.*.#1, # 6#, # #1 # 1#,#1 2#1 1#,# o 4#1,#1 4#1, #6,",
    " 1#2, 1# #, 1# #3, #1 3#1, # 1# . #, # . # 1#, #1 #. #1, 1#1 # #, 2#2 #, 4#2,",
    ", #5, # # 1#1, # 1. 1#,#2. * #1,# 3#. #,#1 2#3, #2 #, 2#2,",
    " #3,#1 1#3,# 2. 1#,#1 .#.#1, # 4#, # #o#o#, #1 1o #, 1#1 2#, 2#1 #1, 3#2,",
    ", #5, # 1o.#1, # . #o#1, # * 3#, #o 4#, #1 2.#1, 1#1 1#1, 2#3,",
    ", 1#5, 1# # .#1, 1# 1o 1#, #2. * #, #. 3#1, #1o 2#, 1# 3#, 1#5,",
    ", 4#2, 3#1*#, 2#1. #, 1#1. 1#1, #1 4#, # 4.#, #o 1* o#, #7,",
    "#7,# .oO* #,#1 4#1,#O . 3#,#o# 3#1,# #1 2#1,# 1o 3#,#. #1O. #,#5 1#, 4#3,",
    ", 1#5, 1#. 2#1, #1 # O #, # 1Oo# #, # 2Oo #, #1 o# 1#, 1# 2.#1, 1#5,",
    ", #5, # 1o #1, # O# o#, #o 1O.#1, # 5#, #1. 2O#, 1#6,",
    " #5, # O .#1, #1 2O#1, 1# o#. #, #1 3o#, #O #O 1#, # o 3#, #1 #4, 1#2,",
    " 1#4, #1O 1#, # o 1#1, #1. 2#, # O.o*#, #1 2.#1, 1# 2o #, 1#1 3#, 2#5,",
    " 1#6, 1# 1. o#, #1 o# 1#, # 2# 1#, #1 3#1, 1#. 2#1, 1# 2o #, 1#1 .# #, 2#5,",
    ", 1#4, #1 1.#1, # . # #1, #1 . 1o#, #2o *.#, # . * o#, #7,",
    " #7,#1 o 1O #,# 4o#1,#1 5#,# 1O 1.#1,#1. 2*#, #1o 2#, # . 1O#, #6,",
  };

  private static int aMove[] =
    {4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,6,6,6,6,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,10,};

  private Image mainImg;
  private Graphics g;
  private TextField txt1;

  public void init()
  {
    String param;
    int i;
    if (g==null) g = getGraphics();
    URL url = getCodeBase();
    String str = "";
    int b = 0;
    MediaTracker tracker = new MediaTracker(this);
    mainImg = getImage(url, "Balls.gif");
    tracker.addImage(mainImg,0);
    for (i=0; i<=MAX_IMG; i++)
      {
        xImg[i] = extractImage(xCoord[i]);
        tracker.addImage(xImg[i], i+1);
      }
    try { tracker.waitForAll(); }
    catch (InterruptedException e) {}
    //设置布局
    setLayout(new BorderLayout());
    txt1 = new TextField(17);
    txt1.setForeground(new Color(255,255,204));
    txt1.setBackground(new Color(102,51,51));
    txt1.setEditable(false);

    Panel p = new Panel();

    p.add(txt1);
    p.add(btnP);
    p.add(btnN);
    p.add(btn1);
    add("South", p);//布局格式
    gLevel = 0;
    dat2Canvas(0);
    firstTime = 1;
  }
  public void start() //启动线程
  {
    if (game0==null)
     {
       game0 = new Thread(this);
       game0.start();
     }
  }

  public void stop()
  {
    if (game0 != null)
     {
       game0.stop();
       game0 = null;
     }
  }
  public void run()
  {
  }
  public boolean dat2Canvas(int dlvl)
  {
    int i,x,y,tmp0;
    byte z = 0, oz = 0;
    String param;
    try
     {
       gLevel += dlvl;
       if (gLevel<0) gLevel = 0;
       try { i = Integer.parseInt(getParameter("maxmove"+gLevel)); }
       catch (NumberFormatException e) { i = 0; }
       param = getParameter("data"+gLevel);
       if (i>0) maxMove = i;
       if (param==null || i<=0)
          { param = biDat[gLevel]; maxMove = aMove[gLevel]; }
     }
    catch (ArrayIndexOutOfBoundsException exc)
     {
       gLevel -= dlvl;
       return false;
     }
    for (y=0; y<=MAX_Y; y++)
      { for (x=0; x<=MAX_X; x++)
          { currCanvas[y][x] = 0; }
      }
    x = y = 0;
    for (i=0; i<param.length(); i++)
      {
        z = (byte)(widthe.indexOf(param.charAt(i)));
        if (z>MAX_IMG)
         {
           if ((tmp0 = (z-MAX_IMG-1))>0)
            {
              for (int j=0; j<tmp0; j++)
                {
                  try { currCanvas[y][x++] = oz; }
                  catch (ArrayIndexOutOfBoundsException exc) { j = tmp0; }
                }
            }
           else
            {
              x = 0; y++;
              if (y>MAX_Y) break;
            }
         }
        else if (x<=MAX_X)
         {
           if (z<0) z = 0;
           currCanvas[y][x++] = z;
           oz = z;
         }
      }
    for (i=0; i<MAX_HIST; i++) { xMove[i] = 0; }
    mvPos = 0;

    txt1.setText(" Level "+gLevel+" ("+maxMove+" step max.)");
    if (firstTime==1) firstTime = 0;

    return true;
  }

  public boolean action(Event e, Object arg)
  {
    int h = -1;

    if      (e.target==btnP)  h = 44;
    else if (e.target==btnN)  h = 46;
    else if (e.target==btn1)  h = 27;

    if (h>0) keyDown(e, h);

    return true;
  }

  public boolean keyDown(Event e, int c)//键盘动作处理
  {
    int dx = 0;
    int dy = 0;
    int ch = 0;
    int h  = 0;

    switch (c)
    {
      case Event.UP:    dy = -1; h = 1; break;
      case Event.DOWN:  dy =  1; h = 3; break;
      case Event.LEFT:  dx = -1; h = 5; break;
      case Event.RIGHT: dx =  1; h = 7; break;
      case 27:
        if (mvPos==0 && firstTime==0) { firstTime = 1; paint(null); return true; }
        h = -100; break;
      case 44: h = -101; break;
      case 46: h =  -99; break;
      case 14: h =  -90; break;
      case 16: h = -110; break;

      default:
        txt1.setText(" Use ^P,<,>,^N,^R, or BS key.");
        break;
    }

    if (dx==0 && dy==0)
     {
       if (h<0) { if (dat2Canvas(h+100)) paint(null); }
       return true;
     }

    if (mvPos>=maxMove)
       { txt1.setText(stYouLost); return true; }

    h = 1; ch = 0;
    while(h>0)
     {
     ch=0;
       if ((h = moveBall(dx,dy))>0)
          { ch = 1; h = checkBoard(); }
     }

    if (ch==1) { mvPos++; txt1.setText(" Have "+(maxMove-mvPos)+" more step."); }
       else return true;

    h = GameEnd();
    if (h==1) { txt1.setText(stGameEnd); h = -98; }
    else if (h==-1 || mvPos>=maxMove)
            { txt1.setText(stYouLost); h = -99; }

    if (h<=-98)
     {
      try { game0.sleep(1500); }
       catch (InterruptedException exc) {}
       if (dat2Canvas(h+99)) paint(null);
     }

    return true;
  }

  public int moveBall(int dx, int dy)
  {
    int x,y,lx,ly,x2,y2;
    byte z0,z1;
    int h = 0;

    if (dx != 0)
     {
       ly = Y_0;
       for (y=0; y<=MAX_Y; y++)
       {
         if (dx==1) x = MAX_X-1; else x = 1;
         while (x>0 && x<MAX_X)
         {
           if ((z0 = currCanvas[y][x])>=6)
            {
              x2 = x; lx = X_0+20*x;
              while (x2>0 && x2<MAX_X && (z1=currCanvas[y][x2+dx])==0)
              {
                try { game0.sleep(10); }
                catch (InterruptedException e) {}
                h++;
                currCanvas[y][x2] = 0;
                g.drawImage(xImg[0],lx,ly,this);
                x2 += dx; lx += 20*dx;
                currCanvas[y][x2] = z0;
                g.drawImage(xImg[z0],lx,ly,this);
              }
            }
           x -= dx;
         }
         ly += 20;
       }
     }

    if (dy != 0)
     {
       lx = X_0;
       for (x=0; x<=MAX_X; x++)
       {
         if (dy==1) y = MAX_Y-1; else y = 1;
         while (y>0 && y<MAX_Y)
         {
           if ((z0 = currCanvas[y][x])>=6)
            {
              y2 = y; ly = Y_0+20*y;
              while (y2>0 && y2<MAX_Y && (z1=currCanvas[y2+dy][x])==0)
              {
                try { game0.sleep(10); }
                catch (InterruptedException e) {}
                h++;
                currCanvas[y2][x] = 0;
                g.drawImage(xImg[0],lx,ly,this);
                y2 += dy; ly += 20*dy;
                currCanvas[y2][x] = z0;
                g.drawImage(xImg[z0],lx,ly,this);
              }
            }
           y -= dy;
         }
         lx += 20;
       }
     }

    return h;
  }
  public int checkBoard()
  {
    byte z0,z1,z2;
    int  h  = 0;
    int  h2 = 0;

    z1 = 100;
    for (int y=0; y<MAX_Y; y++)
    {
      for (int x=0; x<MAX_X; x++)
      {
        z0 = (byte)(currCanvas[y][x] % z1);
        if (z0>=7)
         {
           z2 = (byte)(z0+z1);
           if (z0==(byte)(currCanvas[y][x+1] % z1))
            {
              currCanvas[y][x] =
              currCanvas[y][x+1] = z2;
              h++;
            }
           if (z0==(byte)(currCanvas[y+1][x] % z1))
            {
              currCanvas[y][x] =
              currCanvas[y+1][x] = z2;
              h++;
            }
         }
      }
    }
    if (h==0) return 0;
    for (int z=2; z<=6; z++)
    {
      try { game0.sleep(100); }
      catch (InterruptedException e) {}

      int ly = Y_0;
      for (int y=0; y<MAX_Y; y++)
      {
        int lx = X_0;
        for (int x=0; x<MAX_X; x++)
        {
          if ((z0 = currCanvas[y][x])>MAX_IMG)
           {
             if (z<6) g.drawImage(xImg[z],lx,ly,this);
             else
              {
                currCanvas[y][x] = 0;
                g.drawImage(xImg[0],lx,ly,this);
              }
           }
          lx += 20;
        }
        ly += 20;
      }
    }
    return h;
  }

  public int GameEnd()
  {
    int x,y;
    int h = 1;
    int h0[] = new int[MAX_IMG+1];
    for (y=0; y<=MAX_IMG; y++) h0[y] = 0;
    for (y=0; y<=MAX_Y; y++)
    {
      for (x=0; x<=MAX_X; x++)
      {
        h0[currCanvas[y][x]]++;
      }
    }
    y = 7;
    while (y<=MAX_IMG)
    {
      x = h0[y];
      if (x==1) { h = -1; y = MAX_IMG+1; }
      else if (h==1 && x>1) h = 0;
      y++;
    }

    return h;
  }
  private Image extractImage(int[] xyCoord)
  {
    Image newImage;
    ImageFilter filter;
    ImageProducer producer;

    filter = new CropImageFilter(xyCoord[0],xyCoord[1],xyCoord[2],xyCoord[3]);
    producer = new FilteredImageSource(mainImg.getSource(), filter);
    newImage = createImage(producer);
    return newImage;
   }

  public void paint(Graphics g)
  {
    if (g==null) g = getGraphics();
    int y2 = Y_0;
    for (int y=0; y<=MAX_Y; y++)
      {
        int x2 = X_0;
        for (int x=0; x<=MAX_X; x++)
          {
            byte z = currCanvas[y][x];
            if (firstTime==1 || z>MAX_IMG) z = 0;
            g.drawImage(xImg[z],x2,y2,this);
            x2 += 20;
          }
        y2 += 20;
      }
    if (firstTime==1) doFirstTime();
  }
  public void doFirstTime()//进入游戏时设置,说明规则
  {
    int y = 20, x = 15;
    g.setColor(new Color(255,255,204));
    String str = "* 打球游戏 *";
    g.drawString(str,15,y);
    txt1.setText(str);
    y += 20; g.drawString("",15,y);
    y += 20; g.drawString("游戏目标:打碎所有球",15,y);
    y += 15; g.drawString("",15,y);
    y += 15; g.drawString("移动球:方向键",15,y);
    y += 15; g.drawString("",15,y);
    y += 15; g.drawString("打碎规则:水平或竖直两个以上的球",15,y);
    y += 15; g.drawString("",15,y);
    y += 15; g.drawString("",15,y);
    y += 15; g.drawString("WIN:所有球击碎",15,y);
    y += 30; g.drawString("",15,y);
    y += 15; g.drawString("LOST:球有剩余",15,y);
    y += 15; g.drawString("",15,y);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -