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

📄 gamefrm.java

📁 一个JAVA小游戏
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class GameFrm extends Frame implements MouseListener,ActionListener{
    protected boolean m_bIsRun,m_bIsPlay;
    protected int m_aX[],m_aY[];
    protected int m_nTop,m_nLastX,m_nLastY,m_nClickCount;
    protected boolean m_abStatus[][];
    protected int m_nLeftPosition,m_nScore;
    protected MenuBar m_barMainMenu;
    protected Menu m_menuGame,m_menuHelp,m_menuSet;
    protected MenuItem m_itemStart,m_itemEnd,m_itemExit,m_itemHelp,m_itemHero,m_itemPlay,m_itemStop;
    protected Chessboard m_cbBoard;
    protected PLabel m_plLabel;
    protected Color m_acrManColor[]=new Color[7];
    protected MidiPlayer m_player;
    public GameFrm(){
       super("游戏");
       setLayout(new BorderLayout(3,3));
       m_bIsRun=false;
       m_nScore=0;
       m_aX=new int[81];
       m_aY=new int[81];
       m_nClickCount=0;
       m_nLeftPosition=81;
       m_abStatus=new boolean[9][9];
       //setBackground(Color.black);
       int i,j;
       for(i=0;i<9;i++)
          for(j=0;j<9;j++)m_abStatus[i][j]=false;

       m_acrManColor[0]=Color.black;
       m_acrManColor[1]=Color.blue;
       m_acrManColor[2]=Color.green;
       m_acrManColor[3]=Color.yellow;
       m_acrManColor[4]=Color.red;
       m_acrManColor[5]=Color.pink;
       m_acrManColor[6]=Color.cyan;

       //添加菜单
       m_barMainMenu=new MenuBar();
       m_menuGame=new Menu("游戏");
       m_menuSet=new Menu("背景音乐");
       m_menuHelp=new Menu("帮助");

       m_itemStart=new MenuItem("开始");
       m_itemEnd=new MenuItem("结束");
       m_itemExit=new MenuItem("退出");
       m_itemHelp=new MenuItem("帮助");
       m_itemHero=new MenuItem("英雄榜");
       m_itemPlay=new MenuItem("播放");
       m_itemStop=new MenuItem("停止");

       m_menuGame.add(m_itemStart);
       m_menuGame.add(m_itemEnd);
       m_menuGame.add(m_itemHero);
       m_menuGame.addSeparator();
       m_menuGame.add(m_itemExit);
       m_menuHelp.add(m_itemHelp);
       m_menuSet.add(m_itemPlay);
       m_menuSet.add(m_itemStop);

       m_barMainMenu.add(m_menuGame);
       m_barMainMenu.add(m_menuSet);
       m_barMainMenu.add(m_menuHelp);
       setMenuBar(m_barMainMenu);
      
       //添加界面
       m_cbBoard=new Chessboard();
       m_plLabel=new PLabel();
       add("North",m_plLabel);
       add(m_cbBoard,"Center");

       //添加消息监听
       addWindowListener(new WindowAdapter(){
                             public void windowClosing(WindowEvent e){
                                   m_cbBoard.reset();
                                   setVisible(false);
                                   System.exit(0);
                             }
                         });
       for(i=0;i<9;i++)
          for(j=0;j<9;j++)m_cbBoard.m_cmMan[i][j].addMouseListener(this);
       m_itemStart.addActionListener(this);
       m_itemEnd.addActionListener(this);
       m_itemExit.addActionListener(this);
       m_itemHelp.addActionListener(this);
       m_itemHero.addActionListener(this);
       m_itemPlay.addActionListener(this);
       m_itemStop.addActionListener(this);
       m_bIsPlay=false;
       m_player=new MidiPlayer("sound.mid");
       if(m_player.m_bOk==false){
          m_itemPlay.setEnabled(false);
          m_itemStop.setEnabled(false);
       }
       setSize(460,550);
       setResizable(false);
       m_itemEnd.setEnabled(false);
   }
   //定义鼠标消息
   public void mousePressed(MouseEvent e){
   }
   public void mouseReleased(MouseEvent e){
   }
   public void mouseEntered(MouseEvent e){
   }
   public void mouseExited(MouseEvent e){
   }
   
   //菜单消息
   public void actionPerformed(ActionEvent e){
        if(e.getSource()==m_itemExit){
             m_cbBoard.reset();
             setVisible(false);
             System.exit(0);
        }
        else if(e.getSource()==m_itemEnd){
             m_cbBoard.reset();
             m_plLabel.reset();
             m_bIsRun=false;
             m_nScore=0;
             m_nClickCount=0;
             m_nLeftPosition=81;
             int i,j;
             for(i=0;i<9;i++)
                 for(j=0;j<9;j++)m_abStatus[i][j]=false;
             m_nTop=0;
             m_itemEnd.setEnabled(false);
             m_itemStart.setEnabled(true);
        }
        else if(e.getSource()==m_itemStart){
             int i,nIndex,nPosition;
             for(i=0;i<3;i++){
                 nIndex=(int)Math.round(Math.random()*1000)%7;
                 nPosition=(int)Math.round(Math.random()*90)%m_nLeftPosition;
                 setPosition(nPosition,m_acrManColor[nIndex]);
                 
             }
             for(i=0;i<3;i++){
                 nIndex=(int)Math.round(Math.random()*1000)%7;
                 m_plLabel.m_cmColorBox[i].select(m_acrManColor[nIndex]);
             }
             m_bIsRun=true;
             m_itemStart.setEnabled(false);
             m_itemEnd.setEnabled(true);
        }
        else if(e.getSource()==m_itemHelp){
             HelpDlg help=new HelpDlg(this ,"帮助",false);
             help.setVisible(true);
        }
        else if(e.getSource()==m_itemHero){
             FileOperator file=new FileOperator("score.gm",this);
             file.showScore();
             file.close();
             
       }
       else if(e.getSource()==m_itemPlay){
            if(m_bIsPlay==false){
                 m_bIsPlay=true;
                 m_player.loop();
            }
       }
       else if(e.getSource()==m_itemStop){
            if(m_bIsPlay==true){
                 m_bIsPlay=false;
                 m_player.stop();
            }
       }
   }
   protected void setPosition(int nPosition,Color cr){
        int count=0,i,j=0;
        out:
        while(true){
              i=j=0;
              for(i=0;i<9;i++)
                 for(j=0;j<9;j++){
                    if(m_abStatus[i][j]==false){
                        if(count==nPosition)break out;
                        else count++;
                    }
               }
        }
        m_abStatus[i][j]=true;
        m_cbBoard.m_cmMan[i][j].select(cr);
        check(i,j);m_nLeftPosition--;
        if(m_nLeftPosition==0){
           GameOver();
           return;
        }
        
   }
   protected boolean check(int x,int y){
        int aX[],aY[],len,i;
        aX=new int[9];
        aY=new int[9];
        len=0;
        Color cr=m_cbBoard.m_cmMan[x][y].getColor();
        int a[]=new int[4];int b[]=new int[4];
        a[0]=1;a[1]=0;a[2]=1;a[3]=1;
        b[0]=0;b[1]=1;b[2]=1;b[3]=-1;
        int x0,y0;
        aX[0]=x;aY[0]=y;
        for(i=0;i<4;i++){
            x0=x+a[i];y0=y+b[i];len=1;
            while(x0<9&&x0>=0&&y0<9&&y0>=0&&m_abStatus[x0][y0]&&
                  cr==m_cbBoard.m_cmMan[x0][y0].getColor()){
                  aX[len]=x0;aY[len]=y0;
                  len++;
                  x0+=a[i];y0+=b[i];
            }
            x0=x-a[i];y0=y-b[i];
            while(x0<9&&x0>=0&&y0<9&&y0>=0&&m_abStatus[x0][y0]&&
                  cr==m_cbBoard.m_cmMan[x0][y0].getColor()){
                  aX[len]=x0;aY[len]=y0;
                  len++;
                  x0-=a[i];y0-=b[i];
            }
            if(len>=5)break;
        }
        if(len>=5){
            m_nScore+=(len-5)*2+5;
            m_nLeftPosition+=len;
            for(i=0;i<len;i++){
               m_abStatus[aX[i]][aY[i]]=false;
               m_cbBoard.m_cmMan[aX[i]][aY[i]].cancelSelect();
            }
            m_plLabel.setScore(m_nScore);
       }
       if(len>=5)return true;
       else return false;
   }
   public void mouseClicked(MouseEvent e){
        if(m_bIsRun==false)return;
        
        int i,j=0;
        bk:
        for(i=0;i<9;i++)
           for(j=0;j<9;j++)if(e.getSource()==m_cbBoard.m_cmMan[i][j])break bk;
        if(i>=9)return;
        if(m_nClickCount==0&&m_abStatus[i][j]){
           m_cbBoard.m_cmMan[i][j].setFirst();
           m_cbBoard.m_cmMan[i][j].flicker();
           m_nLastX=i;
           m_nLastY=j;
           m_nClickCount++;
       }
       else if(m_abStatus[i][j]==false&&m_nClickCount==1){
           boolean bPath=findway(m_nLastX,m_nLastY,i,j);
           if(bPath){
               m_cbBoard.move(m_aX,m_aY,m_nTop);
               m_abStatus[m_nLastX][m_nLastY]=false;
               m_abStatus[i][j]=true;
               if(!check(i,j))
                    prepareNext();
               m_nClickCount=0;
           }
       }
       else if(m_nLastX==i&&m_nLastY==j&&m_nClickCount==1){
           m_cbBoard.m_cmMan[i][j].cancelFlicker();
           m_nClickCount=0;
       }
   }
   public void prepareNext(){
       int nIndex,nPosition;
       int i;Color cr;
       for(i=0;i<3;i++){
          if(m_nLeftPosition==0){
               GameOver();
               return;
          }
          cr=m_plLabel.m_cmColorBox[i].getColor();
          nIndex=(int)Math.round(Math.random()*1000)%7;
          nPosition=(int)Math.round(Math.random()*90)%m_nLeftPosition;
          setPosition(nPosition,cr);
          m_plLabel.m_cmColorBox[i].setColor(m_acrManColor[nIndex]);
      }
   }
   protected void GameOver(){
       if(m_bIsRun==false)return;
       m_bIsRun=false;
       EndDlg end=new EndDlg(this,"得分",true,m_nScore);
       end.setVisible(true);
       
       FileOperator file=new FileOperator("score.gm",this);
       if(file.compare(m_nScore)){
          file.insert(m_nScore);
          file.close();
       }
   }
   boolean findway(int sx,int sy,int dx,int dy){
      int a[][]=new int [9][9];
      int i,j,i1,j1;
      for(i=0;i<9;i++)
         for(j=0;j<9;j++)a[i][j]=0;
      i=sx;j=sy;m_nTop=0;
      
      while(i!=dx||j!=dy){
         a[i][j]++;
         if(a[i][j]>4){
            m_nTop--;
            if(m_nTop<0)break;
            i=m_aX[m_nTop];
            j=m_aY[m_nTop];
         }
         else{
            switch(a[i][j]){
            case 1:i1=i-1;j1=j;
                   if(i1<0)break;
                   if(m_abStatus[i1][j1]||a[i1][j1]>0)break;
                   m_aX[m_nTop]=i;
                   m_aY[m_nTop]=j;
                   m_nTop++;
                   i=i1;j=j1;
                   break;
            case 2:i1=i;j1=j+1;
                   if(j1>=9)break;
                   if(m_abStatus[i1][j1]||a[i1][j1]>0)break;
                   m_aX[m_nTop]=i;
                   m_aY[m_nTop]=j;
                   m_nTop++;
                   i=i1;j=j1;
                   break;
            case 3:i1=i+1;j1=j;
                   if(i1>=9)break;
                   if(m_abStatus[i1][j1]||a[i1][j1]>0)break;
                   m_aX[m_nTop]=i;
                   m_aY[m_nTop]=j;
                   m_nTop++;
                   i=i1;j=j1;
                   break;
            case 4:i1=i;j1=j-1;
                   if(j1<0)break;
                   if(m_abStatus[i1][j1]||a[i1][j1]>0)break;
                   m_aX[m_nTop]=i;
                   m_aY[m_nTop]=j;
                   m_nTop++;
                   i=i1;j=j1;
                   break;
            default:break;
            }
         }
      }
      if(m_nTop<0)return false;
      else {
           m_aX[m_nTop]=i;
           m_aY[m_nTop]=j;
           m_nTop++;
           return true;
      }
   }
}   
   

⌨️ 快捷键说明

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