JAVA拼图游戏

package lgame8;

import java.awt.*;  
import java.applet.*;  
import java.awt.event.*;  
public class PPuzzle extends Applet{  
    Image imgPuzzle,buff;  
    Point fifteen=new Point(3,3);  
    int[][] map={
       {0,4,8,12},
       {1,5,9,3},
       {2,6,10,14},
       {3,7,11,15}
       };  
    int sx,sy;  
    Canvas screen;  
    Graphics gs,gb;  
    boolean running=false;  
    Button bStart=new Button("新游戏");  
    Button bSee=new Button("显示正确的图像");  
    public void init(){  
        prepareImage();                    //准备图像  
        sx=imgPuzzle.getWidth(this)/4;  
        sy=imgPuzzle.getHeight(this)/4;  
        setBackground(Color.blue);  
        initScreen();                     //初始化画面  
        initButtons();                    //初始化按钮  
        add(screen);                      //向Applet添加组件  
        add(bStart);  
        add(bSee);  
    }  
    void prepareImage(){                  //准备图像的方法  
        imgPuzzle=getImage(getCodeBase(),"images/87.jpg");  
        MediaTracker mt=new MediaTracker(this);  
        mt.addImage(imgPuzzle, 0);        //等待至图像全部加载完毕  
        try{  
            mt.waitForAll();  
        }catch(Exception e){}  
        //创建buffer并获取其Graphics对象  
        buff=createImage(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this));  
        gb=buff.getGraphics();  
    }  
    void initMap(){                       //初始化map  
        java.util.Random rnd=new java.util.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);  
                    break outer;  
                }  
    }  
    void initScreen(){                   //初始化画面  
        screen=new Canvas(){             //创建screen对象  
            public void paint(Graphics g){  //覆盖paint()方法  
                if(gs==null)gs=getGraphics();//获取screen的Graphics对象  
                if(running)  
                    drawScreen();  
                else 
                //若游戏未开始,则显示整幅图像  
                    g.drawImage(imgPuzzle,0,0,this);  
            }  
        };  
        //设定screen的大小  
        screen.setSize(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this));  
          
        screen.addMouseListener(new MouseAdapter(){  //处理鼠标事件  
            public void mousePressed(MouseEvent me){  
                if(!running) return;  
                int x=me.getX()/sx,y=me.getY()/sy;  
                int fx=(int)fifteen.getX(),fy=(int)fifteen.getY();  
                if(Math.abs(fx-x)+Math.abs(fy-y)>=2)return;  
                if (map[x][y]==15)return;  
                map[fx][fy]=map[x][y];  
                map[x][y]=15;  
                fifteen.setLocation(x,y);  
                drawScreen();  
            }  
        });  
    }  
    void initButtons(){                               //初始化按钮  
        //“新游戏”按钮事件的处理  
        bStart.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent ae){  
                initMap();                            //初始化mpa  
                drawScreen();                         //绘制画面  
                running=true;                        //代表游戏正在进行中  
                bSee.setLabel("显示正确的图像");      //改变bSee按钮的标题  
            }  
        });  
        //“显示正确图像”按钮事件处理  
        bSee.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent ae){  
                //bSee按钮标题为“继续游戏”  
                if(bSee.getLabel().equals("继续游戏")){  
                 drawScreen();                         //绘制画面  
//                 bSee.setLabel("显示正确图像");      //标题更改为“显示正确图像”    
                 }  
                else{  
                    //bSee的标题为“显示正确图像”  
                    gs.drawImage(imgPuzzle,0,0,screen);   //显示整幅图像  
                    bSee.setLabel("继续游戏");  
                }  
            }  
        });  
    }  
    void drawScreen(){                                  //绘制画面的方法  
        gb.clearRect(0, 0, sx*4, sy*4);                 //清空buffer  
        //将制定位置的图像块绘制至buffer中  
        int i;  
        for(int j=0;j<4;j++)  
            for(i=0;i<4;i++)  
                if(map[i][j]!=15)drawSegment(map[i][j],i,j);  
        //向Screen绘制buffer中的图像  
        gs.drawImage(buff, 0, 0, screen);  
    }  
    void drawSegment(int seg, int x, int y){  
          
        int dx=seg%4*sx,dy=seg/4*sy;  
          
        gb.drawImage(imgPuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);  
    }  
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值