java--拼图游戏源代码

时间:2013-12-12 12:41:38
【文件属性】:

文件名称:java--拼图游戏源代码

文件大小:114KB

文件格式:RAR

更新时间:2013-12-12 12:41:38

游戏-拼图-课程设计

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,13},{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); add(bStart); add(bSee); } void prepareImage(){ imgPuzzle=getImage(getCodeBase(),"images/3.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(){ java.util.Random rnd=new java.util.Random(); int temp,x1,x2,y1,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(){ public void paint(Graphics g){ if(gs==null) gs=getGraphics(); if(running) drawScreen(); else g.drawImage(imgPuzzle,0,0,this); } }; 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; 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(); drawScreen(); running=true; bSee.setLabel("显示正确图像"); } }); //显示正确图像 按钮事件处理 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中 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); //向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 ); } }


【文件预览】:
拼图
----PPuzzle.java(3KB)
----PPuzzle$1.class(784B)
----PPuzzle$4.class(1KB)
----P.jar(548B)
----PPuzzle$3.class(715B)
----PPuzzle$2.class(993B)
----manifest.mf(18B)
----PPuzzle.class(3KB)
----PPuzzle.jar(5KB)
----images()
--------3.jpg(30KB)
--------s.jpg(73KB)
----PPuzzle.htm(122B)

网友评论

  • 没主函数 自己改改看
  • 很好!就是有些地方还不清楚
  • 给大学生做毕业设计非常实用
  • 大学毕业作业。。帮人找的 可用
  • 很好,很强大。。。只是有些地方还是不懂
  • 没用上,谢谢
  • 不错的小程序,谢谢作者
  • 还可以,很好玩
  • 有代码,学起来很方便。
  • 虽然看不懂,但是觉得好强大、
  • 能够运行,过程也相对简单,初学者很实用
  • 运行不起来,看不懂。
  • 我怎么运行不出来。晕晕晕
  • 很好,很强大。。。只是有些地方还是不懂
  • 好好玩。菜鸟喜欢
  • 不错的小程序,谢谢作者
  • 运行显示找不到主方法
  • 很好的例子,不错。可以任意选择图片
  • 有源代码的,可以参照学习呢
  • 我没运行出来,不知道效果什么样。
  • 同运行不起来,东西是挺全的
  • 很好玩得java拼图,有源代码,如果注释再完整点就完美了
  • 我怎么运行不出来。晕晕晕
  • 不错~有源代码~要是有word文档就更不错了~~