Java程序设计制作拼图游戏_第1页
Java程序设计制作拼图游戏_第2页
Java程序设计制作拼图游戏_第3页
Java程序设计制作拼图游戏_第4页
Java程序设计制作拼图游戏_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java程序设计实例:制作拼图游戏本例知识点一句话讲解新学知识使用Image类导入图片使用MouseEvent类处理鼠标事件已学知识使用Point类转换坐标使用Graphics类管理屏幕显示一、练习具体要求本练习制作拼图游戏,运行效果如图99-1所示。执行本实例后,用鼠标拖动选中的小图片进行拼接,可以拼成一幅完整的图片。本实例的知识点有:鼠标事件的监听,Graphics类和Image类的应用。二、程序及注释(1)编程思路:本练习因为要制作拼图游戏,所以首先要实现图片的导入。这是通过getImage()函数来实现的,该函数有两个参数,第一个参数指明图片的路径,第二个参数指明图片的名称。然后,因为要实现图片摆放的随意性,所以要通过initgame()函数来实现。Initgame()函数是自写函数,在函数体内,通过调用Math.random()函数产生随机数,用来达到图片位置摆放的随意性和随机性。最后,因为要实现人机交互.,所以首先要通过一系列函数来实现对鼠标事件的监听和响应,这是通过函数addMouseListener(this)和addMouseMotionListener(this)来完成的。这样程序会区分用户对鼠标不同的操作,正确执行相应的功能。(2)程序实现及注释:importjava.awt.*;importjava.applet.*;importjava.awt.event.*;publicclasspintuextendsApplet implementsMouseListener,MouseMotionListener{ privateImagepicture; privateGraphicsbuffer; privateImagepic[]; privateImageoff_pic[]; privateGraphicsoff_buf[]; privateImageoff_screen; privateGraphicsoff_buffer; privateImageoff_drag; privateGraphicsoff_drag_buf; privateintmap[][]; privateintran[]; privateintwidth=0; privateintheight=0; privateintlastx; privateintlasty; privateintlast_downx; privateintlast_downy; privateintstepx; privateintstepy; privatebooleanchoose; privatebooleanclick[][]; privatebooleanm_down; privatebooleanm_drag; privatebooleannot_redraw; privatebooleanable; Fontfont1,font2; //程序的初始化 publicvoidinit() { resize(640,480); pic=newImage[3]; off_pic=newImage[16]; off_buf=newGraphics[16]; map=newint[4][4]; ran=newint[15]; for(inta=0;a<16;a++) map[a/4][a%4]=a; for(inta=0;a<15;a++) ran[a]=a; click=newboolean[4][4]; MediaTrackertracker=newMediaTracker(this); //要载入的图片 pic[0]=getImage(getCodeBase(),"PICTURE0.JPG"); pic[1]=getImage(getCodeBase(),"PICTURE1.JPG"); pic[2]=getImage(getCodeBase(),"PICTURE2.GIF"); tracker.addImage(pic[0],0); tracker.addImage(pic[1],0); tracker.addImage(pic[2],0); try{ tracker.waitForID(0); }catch(InterruptedExceptione){} //设置字体 font1=newFont("TimesRoman",Font.BOLD,48); font2=newFont("TimesRoman",Font.BOLD,32); width=640; height=480; //初始化主界面 initForm(); //添加鼠标监听事件 addMouseListener(this); addMouseMotionListener(this); } //面板初始化 voidinitForm() { this.setBackground(Color.orange); if(off_drag==null){ off_drag=createImage(width/4,height/4); off_drag_buf=off_drag.getGraphics(); } } publicvoidpaint(Graphicsg){ if(off_screen==null) { off_screen=createImage(width,height); off_buffer=off_screen.getGraphics(); } if(able){ off_buffer.setColor(Color.black); for(inta=0;a<4;a++) for(intb=0;b<4;b++) { if(map[a][b]!=15) off_buffer.drawImage(off_pic[map[a][b]],b*width/4,a*height/4,this); if(map[a][b]==15) off_buffer.fillRect(b*width/4,a*height/4,width/4,height/4); for(intc=0;c<2;c++) off_buffer.drawRect(b*width/4+c,a*height/4+c,width/4-c,height/4-c); if(click[a][b]) { off_buffer.setColor(Color.red); for(intd=0;d<2;d++) off_buffer.drawOval(b*width/4-d,a*height/4-d,width/4+d,height/4+d); off_buffer.setColor(Color.black); } } } else{ off_buffer.setColor(Color.orange); off_buffer.fillRect(0,0,640,480); off_buffer.setFont(font1); off_buffer.setColor(Color.red); off_buffer.drawImage(pic[2],30,50,250,180,this); off_buffer.drawImage(pic[0],370,160,250,180,this); off_buffer.drawImage(pic[1],60,270,250,180,this); off_buffer.drawString("ChooseOne!",320,100); } g.drawImage(off_screen,0,0,this); } publicvoidrepaint(){ paint(this.getGraphics()); } //单击鼠标时产生的事件 publicvoidmouseClicked(MouseEventevt){} //鼠标进入某个区域时产生的事件 publicvoidmouseEntered(MouseEventevt){} //鼠标退出某个区域时产生的事件 publicvoidmouseExited(MouseEventevt){} //移动鼠标时产生的事件 publicvoidmouseMoved(MouseEventevt){ if(!able){ Pointpoint; point=evt.getPoint(); if(point.x>30&&point.x<280&&point.y>50&&point.y<230) { off_buffer.setColor(Color.orange); off_buffer.fillRect(0,0,640,480); off_buffer.setFont(font1); off_buffer.drawImage(pic[2],25,45,250,180,this); off_buffer.drawImage(pic[0],370,160,250,180,this); off_buffer.drawImage(pic[1],60,270,250,180,this); off_buffer.setColor(Color.black); off_buffer.fillRect(30,225,250,5); off_buffer.fillRect(275,50,5,176); off_buffer.setColor(Color.red); off_buffer.drawString("picture2!",320,100); this.getGraphics().drawImage(off_screen,0,0,this); } elseif(point.x>370&&point.x<620&&point.y>160&&point.y<340) { off_buffer.setColor(Color.orange); off_buffer.fillRect(0,0,640,480); off_buffer.setFont(font1); off_buffer.drawImage(pic[2],30,50,250,180,this); off_buffer.drawImage(pic[0],365,155,250,180,this); off_buffer.drawImage(pic[1],60,270,250,180,this); off_buffer.setColor(Color.black); off_buffer.fillRect(370,335,250,5); off_buffer.fillRect(615,160,5,175); off_buffer.setColor(Color.red); off_buffer.drawString("picture0!",320,100); this.getGraphics().drawImage(off_screen,0,0,this); } elseif(point.x>60&&point.x<310&&point.y>270&&point.y<450) { off_buffer.setColor(Color.orange); off_buffer.fillRect(0,0,640,480); off_buffer.setFont(font1); off_buffer.drawImage(pic[2],30,50,250,180,this); off_buffer.drawImage(pic[0],370,160,250,180,this); off_buffer.drawImage(pic[1],55,265,250,180,this); off_buffer.setColor(Color.black); off_buffer.fillRect(60,445,250,5); off_buffer.fillRect(305,270,5,175); off_buffer.setColor(Color.red); off_buffer.drawString("picture1!",320,100); this.getGraphics().drawImage(off_screen,0,0,this); } else{ repaint(); } } } //拖动鼠标时产生的事件 publicvoidmouseDragged(MouseEventevt){ if(!able) return; if(m_down){ Pointpoint; Pointtemp; point=evt.getPoint(); m_drag=true; repaint(); Graphicsdavid=this.getGraphics(); if(!not_redraw) off_drag_buf.drawImage(off_pic[map[last_downy][last_downx]],0,0,this); david.drawImage(off_drag,point.x+stepx,point.y+stepy,this); not_redraw=true; } } //按下鼠标时产生的事件 publicvoidmousePressed(MouseEventevt){ if(!able) return; Pointpoint; Pointtemp; point=evt.getPoint(); if(getarea(point)==point) return; else{ temp=getarea(point); if(!m_down){ if(map[temp.y][temp.x]==15) return; else{ m_down=true; last_downx=temp.x; last_downy=temp.y; stepx=temp.x*160-point.x; stepy=temp.y*120-point.y; } } elseif(m_down){ m_down=false; } } } //放开鼠标时产生的事件 publicvoidmouseReleased(MouseEventevt){ if(able){ if(m_drag){ m_down=false;m_drag=false;not_redraw=false; Pointpoint; Pointtemp; point=evt.getPoint(); if(getarea(point)==point) { repaint(); return;} else{ temp=getarea(point); if(map[temp.y][temp.x]!=15){ repaint();return;} else{ if(Math.abs(last_downx-temp.x)==1&&last_downy-temp.y==0) { intdavid; david=map[last_downy][last_downx]; map[last_downy][last_downx]=15; map[temp.y][temp.x]=david; if(wingame()) able=false; repaint(); return; } elseif(last_downx-temp.x==0&&Math.abs(last_downy-temp.y)==1) { intdavid; david=map[last_downy][last_downx]; map[last_downy][last_downx]=15; map[temp.y][temp.x]=david; if(wingame()) able=false; repaint(); return; } else{repaint();return;} } } } } else{ Pointpoint; point=evt.getPoint(); if(point.x>30&&point.x<280&&point.y>50&&point.y<230) {able=true; initmap(2);} if(point.x>370&&point.x<620&&point.y>160&&point.y<340) {able=true;initmap(0);} if(point.x>60&&point.x<310&&point.y>270&&point.y<450) {able=true;initmap(1);} elsereturn; } } //转换坐标 publicPointgetarea(Pointpoint){ if(point.x>640||point.y>480) returnpoint; elsereturnpoint=newPoint(point.x/160,point.y/120); } //图片初始化 voidinitmap(intstage){ picture=createImage(width,height); buffer=picture.getGraphics(); buffer.drawImage(pic[stage],0,0,640,480,this); for(inta=0;a<15;a++) { off_pic[a]=createImage(width/4,height/4); off_buf[a]=off_pic[a].getGraphics(); off_buf[a].drawImage(picture,0,0,width/4,height/4, (a%4)*width/4,(a/4)*height/4,(a%4+1)*width/4,(a/4+1)*height/4,this); } initgame(); repaint(); } //程序是否结束 booleanwingame(){ for(inta=0;a<4;a++) for(intb=0;b<4;b++) { if(map[a][b]==a*4+b) ; elsereturnfalse; } returntrue; } //游戏初始化 voidinitgame(){ for(inta=0;a<4;a++) for(intb=0;b<4;b++) { if(!(a==3&&b==3)){ map[a][b]=(int)(Ma

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论