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

📄 main.java

📁 对对碰游戏源代码 有课设报告 功能较多 积分 计时 棋盘变化等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }
        
        for(int i=0;i<xyz;i++)
            for(int j=0;j<xyz;j++)
                ButtonImage[i][j].addActionListener(this);
        checkClear();
        createfile();
        
    }
    
    void checkClear() {
        this.canClear=false;
        this.changed=false;
        for(int i=0;i<xyz;i++)
            for(int j=0;j<xyz;j++) {
            ButtonImage[i][j].sameToNextx=false;
            ButtonImage[i][j].samex=1;
            ButtonImage[i][j].sameToNexty=false;
            ButtonImage[i][j].samey=1;
            System.out.print(ButtonImage[i][j].flag);
            if(j==xyz-1)
                System.out.println();
            
            }
        
        for(int j=xyz-1;j>=0;j--)
            for(int i=xyz-2;i>=0;i--) {
            if(ButtonImage[i][j].flag==ButtonImage[i+1][j].flag) {
                ButtonImage[i][j].sameToNexty=true;
                ButtonImage[i][j].samey=ButtonImage[i+1][j].samey+1;
            }
            }
        for(int i=xyz-1;i>=0;i--)
            for(int j=xyz-2;j>=0;j--) {
            if(ButtonImage[i][j].flag==ButtonImage[i][j+1].flag) {
                ButtonImage[i][j].sameToNextx=true;
                ButtonImage[i][j].samex=ButtonImage[i][j+1].samex+1;
                //System.out.print("checky"+i+""+j+"x:"+ButtonImage[i][j].samex+"\t");
                // System.out.println("y:"+ButtonImage[i][j].samey);
            }
            }
        for(int i=0;i<xyz;i++)
            for(int j=0;j<xyz;j++)
                if(ButtonImage[i][j].samex>=3||ButtonImage[i][j].samey>=3) {//vectorBtns.addElement(ButtonImage[i][j]);
            if(ButtonImage[i][j].samex>=3) {
                ButtonImage[i][j].heng=true;
                for(int a=1;a<ButtonImage[i][j].samex;a++)
                    ButtonImage[i][j+a].samex=1;
            }
            if(ButtonImage[i][j].samey>=3) {
                ButtonImage[i][j].shu=true;
                for(int a=1;a<ButtonImage[i][j].samey;a++)
                    ButtonImage[i+a][j].samey=1;
            }
            
                }
        for(int i=0;i<xyz;i++)
            for(int j=0;j<xyz;j++)
                if(ButtonImage[i][j].samex>=3||ButtonImage[i][j].samey>=3) {//thread[i][j].start();
            clear(i,j);
            //canClear=true;
            System.out.println("sss");
            //System.out.println("bnts"+ButtonImage[i][j].x+""+ButtonImage[i][j].y);
                }
        
        
    }
    void changeImg() {//this.canClear=true;
        for(int j=0;j<xyz;j++)
            for(int i=0;i<xyz;i++) {
            if(ButtonImage[i][j].cleared) {
                this.changed=true;
                this.canClear=true;
//                URL musicUrl = getClass().getResource("2.wav");
//            AudioClip backGroundMusic = java.applet.Applet.newAudioClip(musicUrl);
//            backGroundMusic.play();
                
                 if(score<=30) {
                for(int n=i;n>=0;n--) {
                    if(n==0) {//for(int m=0;m<getHowManyToChange(ButtonImage[n][j]);m++)
                        // {
                        int a=getRandom();
                        ButtonImage[n][j].setIcon(new ImageIcon("ima"+a+".gif"));
                        ButtonImage[n][j].flag=a;
                        ButtonImage[n][j].cleared=false;
                        ButtonImage[n][j].heng=false;
                        ButtonImage[n][j].shu=false;
                        //  }
                    } else {
                        ButtonImage[n][j].setIcon(ButtonImage[n-1][j].getIcon());
                        ButtonImage[n][j].flag=ButtonImage[n-1][j].flag;
                        ButtonImage[n][j].cleared=false;
                        ButtonImage[n][j].heng=false;
                        ButtonImage[n][j].shu=false;
                    }}
                }//end for(int n=0;n<=i;n++)
                
            }//end if
            
            }//end for
        for(int i=0;i<xyz;i++)
            for(int j=0;j<xyz;j++) {
            System.out.print(ButtonImage[i][j].flag);
            if(j==xyz-1)
                System.out.println();
            }System.out.println("next");
    }
    
    public void clear(int i,int j) {
        String q="";
        //int x=i;
        // int y=j;
        if(ButtonImage[i][j].heng) {
            int samex=ButtonImage[i][j].samex;
         
            q=getScore(samex);//改成分数的图像,即同时消去三个改成4分的图像等等,不过好像没有用,因为速度太快了,所以这部分的图片可以不要
            for(int k=0;k<samex;k++) {
                ButtonImage[i][j+k].cleared=true;
                
                ButtonImage[i][j+k].setIcon(new ImageIcon(q));
            }
            
        }//end if(btnimage.heng)
        if(ButtonImage[i][j].shu) {
            int samey=ButtonImage[i][j].samey;
            this.changed=true;
            q=getScore(samey);

            for(int a=0;a<samey;a++) {
                ButtonImage[a+i][j].cleared=true;
                ButtonImage[a+i][j].setIcon(new ImageIcon(q));
            }
            
        }//end if(btnimage.shu)
        changeImg();
        if(changed)
            checkClear();
    }
    int getRandom() {
        int a=(int)(Math.random()*7);
        return a;
    }
    
    String getScore(int xy) {
        String s="";
        switch(xy) {
            case 3:score+=4;s="s4.gif";break;
            case 4:score+=6;s="s6.gif";break;
            case 5:score+=9;s="s9.gif";break;
            case 6:score+=13;s="s13.gif";break;
            case 7:score+=18;s="s18.gif";break;
            case 8:score+=24;s="s24.gif";break;
            
        }//switch
        if(score>=500) {
              score=0;
             URL musicUrl = getClass().getResource("gamewin.wav");
             AudioClip backGroundMusic = java.applet.Applet.newAudioClip(musicUrl);
             backGroundMusic.play();
            JOptionPane msg1 = new JOptionPane();
            JOptionPane.showMessageDialog(null,"恭喜你胜利,已经达到顶峰\n你好棒哦,过关","胜利",2);
            timeListener.t.stop();
//            start.setText("开始游戏");
            for(int i=0;i<xyz;i++){
                for(int j=0;j<xyz;j++) {
                ButtonImage[i][j].setEnabled(false);
               
               

                }
            }
            
                 
           
            if(xyz==6){
                Record record1=new Record(file6,timeListener.显示时间);
                record1.show();
            }
            if(xyz==8){
                Record record1=new Record(file8,timeListener.显示时间);
                record1.show();
            }
            if(xyz==12){
                Record record1=new Record(file12,timeListener.显示时间);
                record1.show();
            }
           
        }
        showScore.setText("分数"+score);
       
        
        return s;
    }
    public void actionPerformed(ActionEvent e) {
        int x,y;
        x=((BtnImage)e.getSource()).x;
        y=((BtnImage)e.getSource()).y;
           
        System.out.println("click:"+x+" "+y);
        if(!ButtonImage[x][y].clicked) {
            clickedBtn.add(ButtonImage[x][y]);
            ButtonImage[x][y].clicked=true;
        }
        if(clickedBtn.size()==2) {
            int x1,y1,x2,y2;
            x1=((BtnImage)clickedBtn.firstElement()).x;
            y1=((BtnImage)clickedBtn.firstElement()).y;
            x2=((BtnImage)clickedBtn.lastElement()).x;
            y2=((BtnImage)clickedBtn.lastElement()).y;
            clickedBtn.removeAllElements();
            ButtonImage[x1][y1].clicked=false;
            ButtonImage[x2][y2].clicked=false;
            if((Math.abs(x1-x2)==1&&(y1==y2))||(Math.abs(y1-y2)==1&&(x1==x2))) {
                changeBtn(x1,y1,x2,y2);
            }
            
        }// if(clickedBtn.size()==2)
    }
    boolean btncanClear(int x1,int y1) {
        if(x1>=0&&x1<xyz-2&&y1>=0&&y1<xyz&&(ButtonImage[x1][y1].flag==ButtonImage[x1+1][y1].flag)
        &&(ButtonImage[x1][y1].flag==ButtonImage[x1+2][y1].flag)) {
            System.out.println(x1+y1+"true");
            return true;}
        
        if(x1<xyz&&x1>2&&y1>=0&&y1<xyz&&(ButtonImage[x1][y1].flag==ButtonImage[x1-1][y1].flag)
        &&(ButtonImage[x1][y1].flag==ButtonImage[x1-2][y1].flag)) {
            System.out.println(x1+y1+"true");
            return true;}
        
        if(y1>=0&&y1<xyz-2&&x1>=0&&x1<xyz&&(ButtonImage[x1][y1].flag==ButtonImage[x1][y1+1].flag)
        &&(ButtonImage[x1][y1].flag==ButtonImage[x1][y1+2].flag)) {
            System.out.println(x1+y1+"true");
            return true;}
        if(y1<xyz&&y1>2&&x1>=0&&x1<xyz&&(ButtonImage[x1][y1].flag==ButtonImage[x1][y1-1].flag)
        &&(ButtonImage[x1][y1].flag==ButtonImage[x1][y1-2].flag)) {
            System.out.println(x1+y1+"true");
            return true;} else return false;
    }
    boolean allBtnCanClear(int x1,int y1) {
        if(btncanClear(x1,y1)) return true;
        if(btncanClear(x1+1,y1)) return true;
        if(btncanClear(x1+2,y1)) return true;
        if(btncanClear(x1-1,y1)) return true;
        if(btncanClear(x1-2,y1)) return true;
        if(btncanClear(x1,y1+1)) return true;
        if(btncanClear(x1,y1+2)) return true;
        if(btncanClear(x1,y1-2)) return true;
        if(btncanClear(x1,y1-1)) return true;
        else return false;
    }   
    void changeBtn(int x1,int y1,int x2,int y2) {
        int flagtemp;
        ImageIcon icon=new ImageIcon(ButtonImage[x1][y1].getIcon().toString());
        flagtemp=ButtonImage[x1][y1].flag;
        System.out.println("11"+ButtonImage[x1][y1].getIcon());
        System.out.println("22"+ButtonImage[x2][y2].getIcon());
        ButtonImage[x1][y1].setIcon(ButtonImage[x2][y2].getIcon());
        
        ButtonImage[x1][y1].flag=ButtonImage[x2][y2].flag;
        ButtonImage[x2][y2].setIcon(icon);
        ButtonImage[x2][y2].flag=flagtemp;
        System.out.println("11"+ButtonImage[x1][y1].getIcon());
        System.out.println("22"+ButtonImage[x2][y2].getIcon());
        
        if(!allBtnCanClear(x1,y1)&&!allBtnCanClear(x2,y2))
            //if(!canClear)
        { icon=new ImageIcon(ButtonImage[x1][y1].getIcon().toString());
          flagtemp=ButtonImage[x1][y1].flag;
          ButtonImage[x1][y1].setIcon(ButtonImage[x2][y2].getIcon());
          ButtonImage[x1][y1].flag=ButtonImage[x2][y2].flag;
          ButtonImage[x2][y2].setIcon(icon);
          ButtonImage[x2][y2].flag=flagtemp;
          
        }
        checkClear();
        changeImg();
    }    
    private void replay(JMenuItem c) {
        throw new UnsupportedOperationException("Not yet implemented");
    }    
    private void createfile() {
        LinkedList achivement=new LinkedList();
        if(!file6.exists()){
            try{
                FileOutputStream out=new FileOutputStream("file6.txt");
                ObjectOutputStream object_out=new ObjectOutputStream(out);
                object_out.writeObject(achivement);
                object_out.close();
                out.close();
            } catch(IOException e){}
        }
        
        if(!file8.exists()){
            try{
                FileOutputStream out=new FileOutputStream("file8.txt");
                ObjectOutputStream object_out=new ObjectOutputStream(out);
                object_out.writeObject(achivement);
                object_out.close();
                out.close();
            } catch(IOException e){}
        }
        
        
        if(!file12.exists()){
            try{
                FileOutputStream out=new FileOutputStream("file12.txt");
                ObjectOutputStream object_out=new ObjectOutputStream(out);
                object_out.writeObject(achivement);
                object_out.close();
                out.close();
            } catch(IOException e){}
        }
    }
}

public class Main{
    public static void main(String args[]) {
        MainFrame ddp=new MainFrame();
        
    }
}



⌨️ 快捷键说明

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