java遊戲--拼圖

package lession.game.game1;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ppuzzle extends Applet{
 Image imgppuzzle,buffer;
 Point fifteen=new Point(3,3);
 Canvas screen;
 Graphics gs,gb;
 Button bstart=new Button("開始遊戲");
 Button bsee=new Button("正確圖象");
 boolean running;
 int sx,sy;
 int[][]map={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}};
 
 public void init(){
  PrepareImage();
  sx=imgppuzzle.getWidth(this)/4;  //?????????????????????????????
  sy=imgppuzzle.getHeight(this)/4;
  this.setBackground(Color.blue);
  InitScreen();
  InitButtons();
  add(screen);
  add(bstart);
  add(bsee);
  this.setSize(sx*6,sy*4+10);
 // this.setVisible(true);
  //System.out.println("ddddddd");
 }

 public void PrepareImage(){
  /*Class cl=this.getClass();
  URL url=cl.getResource("2.gif");
  imgppuzzle=getImage(url);
  */
  imgppuzzle=getImage(getCodeBase(),"image/2.gif");
  MediaTracker mt=new MediaTracker(this);
  mt.addImage(imgppuzzle,0);
  try{
   mt.waitForAll();
  }catch(Exception e){}
  
  buffer=createImage(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
  gb=buffer.getGraphics();
 }
 
 public void InitMap(){
  Random rnd=new Random();
  int temp,x1,y1,x2,y2;
  for(int i=0;i<100;i++){
   x1=rnd.nextInt(4);
   x2=rnd.nextInt(4);
   y1=rnd.nextInt(4);
   y2=rnd.nextInt(4);
   temp=map[x1][y1];
   map[x1][y1]=map[x2][y2];
   map[x2][y2]=temp;   
  }
  outer:
  for(int j=0;j<4;j++)
   for(int i=0;i<4;i++){
    if(map[i][j]==15){
     fifteen.setLocation(i,j);    //設置第15快圖象的顯示位置
     break outer;
    }
   }
 }
 
 public void InitScreen(){
  screen=new Canvas(){
   public void paint(Graphics g){
    if(gs==null) gs=getGraphics();
    if(running)  DrawScreen();
    else            //如果遊戲沒開始顯示整個圖象
     g.drawImage(imgppuzzle,0,0,this);
   }
  };
  
  screen.setSize(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
  screen.addMouseListener(new MouseAdapter (){
    public void mousePressed(MouseEvent me){
    // System.out.println("選擇了塊1");
     if(running==false)   return;
    // System.out.println("選擇了塊2");
     int x=me.getX()/sx;
     int y=me.getY()/sy;
     int fx=(int)fifteen.getX();
     int fy=(int)fifteen.getY();
    // System.out.println(x+","+y+"    "+fx+","+fy);
     
     if((Math.abs(fx-x)+Math.abs(fy-y))>=2)   return;
    // System.out.println("選擇了塊3");
     if(map[x][y]==15)  return ;
    // System.out.println("選擇了塊4");
     map[fx][fy]=map[x][y];    //交換兩快圖象的位置  重新設置
     map[x][y]=15;
     fifteen.setLocation(x,y);
     //System.out.println("選擇了塊");
     DrawScreen();
    }
   });
 }
 
 public void InitButtons(){
  bstart.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
     InitMap();
     DrawScreen();
     running=true;
     bsee.setLabel("顯示圖象");
    }
   });
  
  bsee.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
     if(bsee.getLabel().equals("繼續")){//??????????????????????????????
      DrawScreen();
      bsee.setLabel("顯示圖象");
     }
     else{
      gs.drawImage(imgppuzzle,0,0,screen);
      bsee.setLabel("繼續");
     }
    }
   }); 
 }
 
 public void DrawScreen(){
  gb.clearRect(0,0,sx*4,sy*4);
  
  for(int j=0;j<4;j++)
   for(int i=0;i<4;i++){
    if(map[i][j]!=15)   DrawSegment(map[i][j],i,j);
   }
  gs.drawImage(buffer,0,0,screen); 
 }
 
 public void DrawSegment(int seg,int x,int y){
  int dx=seg%4 *sx;
  int dy=seg/4 *sy;
  gb.drawImage(imgppuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);
 }
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章