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

📄 boxpanel.java

📁 java编写的一个推箱子游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
}

void downback(int n){
       if(n==20){
	    if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
		   nowmap[mapY][mapX]=4;
	     else  nowmap[mapY][mapX]=2;
        }   
       else if(n==21){
	    if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
		  nowmap[mapY][mapX]=6;
	    else nowmap[mapY][mapX]=3;
	    if(bemap[mapY+1][mapX]==4||bemap[mapY+1][mapX]==6)
		  nowmap[mapY+1][mapX]=4;
	    else nowmap[mapY+1][mapX]=2;
            movements--;
       } 
	    nowmap[mapY-1][mapX]=5;
	    repaint(); mapY--;   
 }
void moveleft(){
		if(nowmap[mapY][mapX-1]==2||nowmap[mapY][mapX-1]==4){
		
			if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
				nowmap[mapY][mapX]=4;
			else nowmap[mapY][mapX]=2;
			nowmap[mapY][mapX-1]=5;			
			repaint();mapX--;   
                        mystack.push(len);
		}
		else if(nowmap[mapY][mapX-1]==3){
			if(nowmap[mapY][mapX-2]==4){
                            
                               if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX-1]=5;
				nowmap[mapY][mapX-2]=6;
				repaint();  
                                mapX--;  
                                movements++;
                                mystack.push(31);
			}
			else if(nowmap[mapY][mapX-2]==2){
			
				if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX-1]=5;
				nowmap[mapY][mapX-2]=3;
				repaint();mapX--;  
                                movements++;
                                mystack.push(31);
			}
			else {
                            nowmap[mapY][mapX]=5;repaint();
                        }
		}
		else if(nowmap[mapY][mapX-1]==6){
		
			if(nowmap[mapY][mapX-2]==4){
			
				if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX-1]=5;
				nowmap[mapY][mapX-2]=6;
				repaint();mapX--;  
                                movements++;
                                mystack.push(31);
			}
			else if(nowmap[mapY][mapX-2]==2){
			
				if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX-1]=5;
				nowmap[mapY][mapX-2]=3;
				repaint();mapX--; 
                                movements++;
                                mystack.push(31);
			}
			else {
                            nowmap[mapY][mapX]=5;
                            repaint();
                        }
		}
		else if(nowmap[mapY][mapX-1]==1){
		        nowmap[mapY][mapX]=5;
                        repaint();
		    }
	}

void leftback(int n){
          if(n==30){
	       if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6){
				nowmap[mapY][mapX]=4;
			   }
	       else      nowmap[mapY][mapX]=2;
                 }
          
	  else if(n==31){
		        if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
			        nowmap[mapY][mapX]=6;
			else 
                               nowmap[mapY][mapX]=3;
		        if(bemap[mapY][mapX-1]==4||bemap[mapY][mapX-1]==6)
		               nowmap[mapY][mapX-1]=4;
		        else
                              nowmap[mapY][mapX-1]=2;
                        movements--;
		  }
		nowmap[mapY][mapX+1]=5;
		repaint();
                mapX++;  
	}

void moveright(){
	   if(nowmap[mapY][mapX+1]==2||nowmap[mapY][mapX+1]==4){
					
		       if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
				nowmap[mapY][mapX]=4;
			else nowmap[mapY][mapX]=2;
			nowmap[mapY][mapX+1]=5;			
			repaint();
                        mapX++;
                        mystack.push(40);
		}
		else if(nowmap[mapY][mapX+1]==3){
			if(nowmap[mapY][mapX+2]==4){
			
				if(bemap[mapY][mapX]==4)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX+1]=5;
				nowmap[mapY][mapX+2]=6;
				repaint();
                                mapX++;
                                movements++;
                                mystack.push(41);
			}
			else if(nowmap[mapY][mapX+2]==2){
			
				if(bemap[mapY][mapX]==4)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX+1]=5;
				nowmap[mapY][mapX+2]=3;
				repaint();
                                mapX++;
                                movements++;
                                mystack.push(41);
			}
		     else {
                            nowmap[mapY][mapX]=5;
                            repaint();
                     }
		}
		else if(nowmap[mapY][mapX+1]==6){
		
			if(nowmap[mapY][mapX+2]==4){
			       if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX+1]=5;
				nowmap[mapY][mapX+2]=6;
				repaint();
                                mapX++; 
                                movements++;
                                mystack.push(41);
			}
			else if(nowmap[mapY][mapX+2]==2){
			
				if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
					nowmap[mapY][mapX]=4;
				else nowmap[mapY][mapX]=2;
				nowmap[mapY][mapX+1]=5;
				nowmap[mapY][mapX+2]=3;
				repaint();
                                mapX++;
                                movements++;
                                mystack.push(41);
			}
			else {
                            nowmap[mapY][mapX]=5;
                            repaint();
                        }
		}
		else if(nowmap[mapY][mapX+1]==1){
		        nowmap[mapY][mapX]=5;
                        repaint();
		}
	}

void rightback(int n){
	    if(n==40){
	           if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
			 nowmap[mapY][mapX]=4;
	          else 
                         nowmap[mapY][mapX]=2;
              }
	    else if(n==41){
		   if(bemap[mapY][mapX]==4||bemap[mapY][mapX]==6)
			   nowmap[mapY][mapX]=6;
		   else 
                           nowmap[mapY][mapX]=3;
		   if(bemap[mapY][mapX+1]==4||bemap[mapY][mapX+1]==6)
			  nowmap[mapY][mapX+1]=4;
		   else 
                          nowmap[mapY][mapX+1]=2;
                   movements--;
		}
		nowmap[mapY][mapX-1]=5;
		repaint();
                mapX--; 
	}
   boolean pass()//判断胜利
	{
		boolean win=false;
		out:for(int i=0; i<16; i++)
		     for(int j=0; j<16; j++){
			if(bemap[i][j]==4||bemap[i][j]==6)
			    if(nowmap[i][j]==6)  win=true;
			    else{
                                  win=false;
                                  break out;
                            }
		}
		return win;
	}

public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }

public void mouseclickback(){   //鼠标点击回退
        Point start1,end1;
        end1= (Point) mouseclick.pop();
        start1=(Point) mouseclick.pop();
        nowmap[start1.y][start1.x]=5;
        if(bemap[end1.y/len][end1.x/len]==4)
              nowmap[end1.y/len][end1.x/len]=4;
        else  nowmap[end1.y/len][end1.x/len]=2;
        repaint();
        mapY=start1.y; 
        mapX=start1.x;
  } 
 public void mousePressed(MouseEvent e) {
         
          Point end=e.getPoint();
          Point start =new  Point();
          if(nowmap[end.y/len][end.x/len]==2||nowmap[end.y/len][end.x/len]==4){
                    start.x=mapX;   start.y=mapY;   
                    if(click(end.x/len ,end.y/len)){
                          nowmap[ end.y/len][end.x/len]= 5;
                          if(bemap[mapY][mapX]==4)     nowmap[mapY][mapX]=4;
                          else     nowmap[mapY][mapX]=2;
                          repaint();
                          mapY=end.y/len;    mapX=end.x/len;
                          Smaze.removeAllElements();
                          mouseclick.push(start);
                          mouseclick.push(end);
                          mystack.push(1);
                        }
                 }
 
       if(nowmap[end.y/len][end.x/len]==3||nowmap[end.y/len][end.x/len]==6) {
            if(nowmap[end.y/len+1][end.x/len]== 5)
                  moveup();
            if(nowmap[end.y/len-1][end.x/len]==5)
                  movedown();
            if(nowmap[end.y/len][end.x/len-1]==5)
                  moveright();
            if(nowmap[end.y/len][end.x/len+1]==5)
                 moveleft();
          }  
       if(pass()){
                        if(level==lastmap){   
                            JOptionPane.showMessageDialog(this, "恭喜您通过最后一关!!!"); 
                        }
			else{               
                                score=score+1000*level/(movements*(backmovements+1));
                                movements=0;
                                backmovements=0;
                                level++;
			        play(level);
                                
		            }
			mystack.removeAllElements();
                        mouseclick.removeAllElements();
		}
 }
public boolean click ( int endx ,int endy  )  {               
     
      int [][]Foot=new int[16][16];  
      int  starty=mapY, startx=mapX;
      for(int i=0; i<16; i++){
	       for(int j=0; j<16; j++){
                   if(nowmap[i][j]==2||nowmap[i][j]==4||nowmap[i][j]==5||nowmap[i][j]==5||nowmap[i][j]==5||nowmap[i][j]==5)
                            Foot[i][j]=1;     
                   else     Foot[i][j]=0;  
               } 
            }  
           do{
                if(Foot[starty][startx]==1){
                     signType pe =new signType();
                     pe.sx=startx;   
                     pe.sy=starty;    
                     pe.di=1;
                     nowmap[starty][startx]=5;
                     Foot[starty][startx]=0;
                     Smaze.push(pe);
                     if(startx==endx && starty==endy)   return true;
                     else   startx++; 
                         
                 }
               else{
                      if(!Smaze.empty()){  
                          signType pek; 
                          pek=(signType)Smaze.pop();
                          while( pek.di==4 && !Smaze.empty() ){
                           //Foot[pek.sx][pek.sy]=0;  
                             pek=(signType)Smaze.pop();
                           }
                           if(pek.di<4){
                                pek.di++;  Smaze.push(pek);
                                startx=pek.sx;  starty=pek.sy;
                                switch(pek.di){
                                     case 2: starty++;  break;
                                     case 3: startx--;  break;     
                                     case 4: starty--;  break;
                                } 
                        }//if
                      }//if
               }//else
             }while(!Smaze.empty());
            return false;
}  
 public void mouseDragged(MouseEvent e) {
         Point dragpoint =e.getPoint();
       
        if(nowmap[dragpoint.y/len+1][dragpoint.x/len]== 5)
               moveup();   
        if(nowmap[dragpoint.y/len-1][dragpoint.x/len]==5)
               movedown(); 
        if(nowmap[dragpoint.y/len][dragpoint.x/len+1]==5)
               moveleft();  
        if(nowmap[dragpoint.y/len][dragpoint.x/len-1]==5)
               moveright();  
        if(pass()){
                   if(level==lastmap){  JOptionPane.showMessageDialog(this, "恭喜您通过最后一关!!!");  }
		   else{               
                             score=score+1000*level/(movements*(backmovements+1));
                              movements=0;
                              backmovements=0;  
                              level++;
                              play(level);
                              
                   }
		  mystack.removeAllElements();
                  mouseclick.removeAllElements();
		}
      }
 public void mouseMoved(MouseEvent e) {  }
 
int[][]getnowmap(){   return nowmap;  }
 int getmovements(){  return movements; }
 int getlevel()  {   return level; } 
 int getscore()  {   return  score;    } 
 int getbackmovements(){   return backmovements;   }

}

    

       

⌨️ 快捷键说明

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