![Java程序設(shè)計制作拼圖游戲_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/da4adfe5-be6a-4771-aa53-1facfe5942b8/da4adfe5-be6a-4771-aa53-1facfe5942b81.gif)
![Java程序設(shè)計制作拼圖游戲_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/da4adfe5-be6a-4771-aa53-1facfe5942b8/da4adfe5-be6a-4771-aa53-1facfe5942b82.gif)
![Java程序設(shè)計制作拼圖游戲_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/da4adfe5-be6a-4771-aa53-1facfe5942b8/da4adfe5-be6a-4771-aa53-1facfe5942b83.gif)
![Java程序設(shè)計制作拼圖游戲_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/da4adfe5-be6a-4771-aa53-1facfe5942b8/da4adfe5-be6a-4771-aa53-1facfe5942b84.gif)
![Java程序設(shè)計制作拼圖游戲_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/2/da4adfe5-be6a-4771-aa53-1facfe5942b8/da4adfe5-be6a-4771-aa53-1facfe5942b85.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、java程序設(shè)計實例:制作拼圖游戲本例知識點一句話講解新學(xué)知識使用image類導(dǎo)入圖片使用mouseevent類處理鼠標(biāo)事件已學(xué)知識使用point類轉(zhuǎn)換坐標(biāo)使用graphics類管理屏幕顯示一、練習(xí)具體要求本練習(xí)制作拼圖游戲,運行效果如圖99-1所示。執(zhí)行本實例后,用鼠標(biāo)拖動選中的小圖片進(jìn)行拼接,可以拼成一幅完整的圖片。本實例的知識點有:鼠標(biāo)事件的監(jiān)聽,graphics類和image類的應(yīng)用。二、程序及注釋(1)編程思路:本練習(xí)因為要制作拼圖游戲,所以首先要實現(xiàn)圖片的導(dǎo)入。這是通過getimage()函數(shù)來實現(xiàn)的,該函數(shù)有兩個參數(shù),第一個參數(shù)指明圖片的路徑,第二個參數(shù)指明圖片的名稱。然后,因為
2、要實現(xiàn)圖片擺放的隨意性,所以要通過initgame()函數(shù)來實現(xiàn)。initgame()函數(shù)是自寫函數(shù),在函數(shù)體內(nèi),通過調(diào)用math.random()函數(shù)產(chǎn)生隨機數(shù),用來達(dá)到圖片位置擺放的隨意性和隨機性。最后,因為要實現(xiàn)人機交互.,所以首先要通過一系列函數(shù)來實現(xiàn)對鼠標(biāo)事件的監(jiān)聽和響應(yīng),這是通過函數(shù)addmouselistener(this) 和addmousemotionlistener(this)來完成的。這樣程序會區(qū)分用戶對鼠標(biāo)不同的操作,正確執(zhí)行相應(yīng)的功能。 (2) 程序?qū)崿F(xiàn)及注釋:import java.awt.*;import java.applet.*;import java.awt
3、.event.* ;public class pintu extends appletimplements mouselistener,mousemotionlistener private image picture;private graphics buffer;private image pic;private image off_pic;private graphics off_buf;private image off_screen;private graphics off_buffer;private image off_drag;private graphics off_drag
4、_buf;private int map;private int ran;private int width=0;private int height=0;private int lastx;private int lasty;private int last_downx;private int last_downy;private int stepx;private int stepy;private boolean choose;private boolean click;private boolean m_down;private boolean m_drag;private boole
5、an not_redraw;private boolean able;font font1,font2;/程序的初始化public void init()resize(640,480);pic = new image 3;off_pic = new image16;off_buf = new graphics 16;map = new int 44;ran = new int 15;for(int a=0;a<16;a+)mapa/4a%4 = a;for(int a=0;a<15;a+)rana=a;click=new boolean 44;mediatracker tracke
6、r= new mediatracker (this);/要載入的圖片pic0=getimage(getcodebase(),"picture0.jpg");pic1=getimage(getcodebase(),"picture1.jpg");pic2=getimage(getcodebase(),"picture2.gif");tracker.addimage (pic0,0);tracker.addimage (pic1,0);tracker.addimage (pic2,0);trytracker.waitforid (0);c
7、atch(interruptedexception e)/設(shè)置字體font1= new font ("timesroman", font.bold, 48);font2= new font ("timesroman", font.bold, 32);width=640;height=480;/初始化主界面initform();/添加鼠標(biāo)監(jiān)聽事件addmouselistener(this);addmousemotionlistener(this);/面板初始化void initform()this.setbackground (color.orange);
8、if(off_drag=null)off_drag = createimage(width/4,height/4);off_drag_buf = off_drag.getgraphics ();public void paint(graphics g)if(off_screen=null)off_screen = createimage(width,height);off_buffer=off_screen.getgraphics ();if(able)off_buffer.setcolor (color.black );for(int a=0;a<4;a+)for(int b=0;b&
9、lt;4;b+)if(mapab!=15)off_buffer.drawimage (off_picmapab,b*width/4,a*height/4,this);if(mapab=15)off_buffer.fillrect (b*width/4,a*height/4,width/4,height/4);for(int c=0;c<2;c+)off_buffer.drawrect (b*width/4+c,a*height/4+c,width/4-c,height/4-c);if(clickab)off_buffer.setcolor(color.red);for(int d=0;d
10、<2;d+)off_buffer.drawoval (b*width/4-d,a*height/4-d,width/4+d,height/4+d);off_buffer.setcolor (color.black );elseoff_buffer.setcolor (color.orange );off_buffer.fillrect (0,0,640,480);off_buffer.setfont (font1);off_buffer.setcolor(color.red );off_buffer.drawimage (pic2,30,50,250,180,this);off_buff
11、er.drawimage (pic0,370,160,250,180,this);off_buffer.drawimage (pic1,60,270,250,180,this);off_buffer.drawstring ("choose one!",320,100);g.drawimage (off_screen,0,0,this); public void repaint()paint(this.getgraphics ();/單擊鼠標(biāo)時產(chǎn)生的事件public void mouseclicked(mouseevent evt)/鼠標(biāo)進(jìn)入某個區(qū)域時產(chǎn)生的事件public
12、void mouseentered(mouseevent evt)/鼠標(biāo)退出某個區(qū)域時產(chǎn)生的事件public void mouseexited(mouseevent evt)/移動鼠標(biāo)時產(chǎn)生的事件public void mousemoved(mouseevent evt)if(!able)point point;point=evt.getpoint();if(point.x >30 && point.x<280 && point.y>50 && point.y<230)off_buffer.setcolor (color.
13、orange );off_buffer.fillrect (0,0,640,480);off_buffer.setfont (font1);off_buffer.drawimage (pic2,25,45,250,180,this);off_buffer.drawimage (pic0,370,160,250,180,this);off_buffer.drawimage (pic1,60,270,250,180,this);off_buffer.setcolor(color.black );off_buffer.fillrect (30,225,250,5);off_buffer.fillre
14、ct (275,50,5,176);off_buffer.setcolor(color.red );off_buffer.drawstring ("picture 2!",320,100);this.getgraphics ().drawimage (off_screen,0,0,this); else if(point.x >370 && point.x<620 && point.y>160 && point.y<340)off_buffer.setcolor (color.orange );off_
15、buffer.fillrect (0,0,640,480);off_buffer.setfont (font1);off_buffer.drawimage (pic2,30,50,250,180,this);off_buffer.drawimage (pic0,365,155,250,180,this);off_buffer.drawimage (pic1,60,270,250,180,this);off_buffer.setcolor(color.black );off_buffer.fillrect (370,335,250,5);off_buffer.fillrect (615,160,
16、5,175);off_buffer.setcolor(color.red );off_buffer.drawstring ("picture 0!",320,100);this.getgraphics ().drawimage (off_screen,0,0,this); else if(point.x >60 && point.x<310 && point.y>270 && point.y<450)off_buffer.setcolor (color.orange );off_buffer.fillr
17、ect (0,0,640,480);off_buffer.setfont (font1);off_buffer.drawimage (pic2,30,50,250,180,this);off_buffer.drawimage (pic0,370,160,250,180,this);off_buffer.drawimage (pic1,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_bu
18、ffer.setcolor(color.red );off_buffer.drawstring ("picture 1!",320,100);this.getgraphics ().drawimage (off_screen,0,0,this); elserepaint();/拖動鼠標(biāo)時產(chǎn)生的事件public void mousedragged(mouseevent evt)if(!able)return;if(m_down)point point;point temp;point=evt.getpoint();m_drag=true;repaint();graphics
19、david = this.getgraphics ();if(!not_redraw)off_drag_buf.drawimage (off_picmaplast_downylast_downx,0,0,this); david.drawimage (off_drag,point.x+stepx,point.y+stepy,this);not_redraw=true;/按下鼠標(biāo)時產(chǎn)生的事件public void mousepressed(mouseevent evt)if(!able)return;point point;point temp;point=evt.getpoint();if(g
20、etarea(point) = point)return;else temp=getarea(point);if(!m_down)if(maptemp.ytemp.x=15)return;elsem_down=true;last_downx=temp.x;last_downy=temp.y;stepx=temp.x*160-point.x;stepy=temp.y*120-point.y;else if(m_down)m_down=false;/放開鼠標(biāo)時產(chǎn)生的事件public void mousereleased(mouseevent evt)if(able)if(m_drag)m_down
21、=false;m_drag=false;not_redraw=false;point point;point temp;point=evt.getpoint();if(getarea(point) = point)repaint();return;else temp=getarea(point);if(maptemp.ytemp.x!=15)repaint();return;elseif(math.abs (last_downx-temp.x)=1 && last_downy-temp.y=0)int david;david=maplast_downylast_downx;ma
22、plast_downylast_downx =15;maptemp.ytemp.x=david;if(wingame()able=false;repaint();return;else if(last_downx-temp.x=0 && math.abs (last_downy-temp.y)=1)int david;david=maplast_downylast_downx;maplast_downylast_downx =15;maptemp.ytemp.x=david;if(wingame()able=false;repaint();return;elserepaint(
23、); return;elsepoint point;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 &
24、amp;& point.x<310 && point.y>270 && point.y<450)able=true;initmap(1);else return;/轉(zhuǎn)換坐標(biāo)public point getarea(point point)if(point.x>640 | point.y>480)return point;else return point = new point (point.x/160,point.y/120);/圖片初始化void initmap(int stage)picture = createima
25、ge(width,height);buffer = picture.getgraphics ();buffer.drawimage (picstage,0,0,640,480,this);for(int a=0;a<15;a+)off_pica = createimage(width/4,height/4);off_bufa = off_pica.getgraphics ();off_bufa.drawimage (picture,0,0,width/4,height/4,(a%4)*width/4,(a/4)*height/4,(a%4+1)*width/4,(a/4+1)*heigh
26、t/4,this);initgame();repaint();/程序是否結(jié)束boolean wingame()for(int a=0;a<4;a+)for(int b=0;b<4;b+)if(mapab=a*4+b);else return false;return true;/游戲初始化void initgame()for(int a=0;a<4;a+)for(int b=0;b<4;b+)if(!(a=3 && b=3)mapab = (int)(math.random()*14);if(ranmapab=-1)int temp=mapab;while(rantemp=-1)temp+;if(temp>14)temp=0;mapab=rantemp;rantemp=-1;elseranmapab=-1;else map33 = 15;三、練習(xí)效
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年油基型密封膠合作協(xié)議書
- 人教版 八年級英語下冊 Unit 6 單元綜合測試卷(2025年春)
- 部編版小學(xué)六年級語文(上冊)第三單元集體備課發(fā)言稿
- 2025年個人律師見證委托合同范文(2篇)
- 山西省2024七年級道德與法治上冊第四單元追求美好人生第十一課確立人生目標(biāo)情境基礎(chǔ)小練新人教版
- 2025年買狗簽合同格式版(2篇)
- 2025年九年級班主任年終個人教學(xué)工作總結(jié)范例(三篇)
- 2025年二年級班主任個人總結(jié)例文(3篇)
- 2025年二級域名合作協(xié)議簡單版(4篇)
- 房屋美化施工合同范例
- 2024年黑龍江農(nóng)業(yè)職業(yè)技術(shù)學(xué)院高職單招(英語/數(shù)學(xué)/語文)筆試歷年參考題庫含答案解析
- 部編版小學(xué)語文四年級下冊教師教學(xué)用書(教學(xué)參考)完整版
- 基于數(shù)據(jù)驅(qū)動的鋰離子電池剩余使用壽命預(yù)測方法研究
- 《內(nèi)臟疾病康復(fù)》課件
- 串通招投標(biāo)法律問題研究
- 高原鐵路建設(shè)衛(wèi)生保障
- 家具廠各崗位責(zé)任制匯編
- 顳下頜關(guān)節(jié)盤復(fù)位固定術(shù)后護(hù)理查房
- 硝苯地平控釋片
- 部編版語文六年級下冊全套單元基礎(chǔ)??紲y試卷含答案
- 提高檢驗標(biāo)本合格率品管圈PDCA成果匯報
評論
0/150
提交評論