您好,欢迎访问三七文档
山西财经大学《JAVA实用教程》实验报告年月日实验题目捕鱼达人选课课号学院班级姓名学号理论课教师上机指导教师实验目的及要求:捕鱼达人游戏的要求:1.在鱼池有很多鱼且鱼各自游动2.有一张渔网,随鼠标移动点击鼠标即可抓住渔网中的与3.抓到鱼进行积分实验原理、详细操作步骤及程序代码:1.理清对象之间的数据关系。对象:窗口鱼池,多条鱼,渔网;2.数据模型:鱼和渔网的width,height,x,y;3.类的设计4.程序代码:importjava.awt.*;importjava.awt.event.*;importjava.awt.image.BufferedImage;importjava.io.File;importjavax.imageio.ImageIO;importjavax.swing.*;importjava.util.Random;publicclassFishDemoextendsThread{publicstaticvoidmain(String[]args)throwsException{FishDemofish=newFishDemo();fish.start();ThreadPoolFishNet}publicvoidrun(){JFrameframe=newJFrame(捕鱼达人);Poolpool;try{pool=newPool();frame.add(pool);frame.setSize(800,510);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);frame.setVisible(true);pool.action();}catch(Exceptione){e.printStackTrace();}}}classPoolextendsJPanel{BufferedImagebg;Fish[]allfish;Netnet;intscore;publicPool()throwsException{bg=ImageIO.read(newFile(bg.jpg));allfish=newFish[9+2+9];for(inti=0;i9;i++){allfish[i]=newFish(fish0+(i+1));allfish[i+9]=newFish(fish0+(i+1));}allfish[allfish.length-2]=newFish(fish13);allfish[allfish.length-1]=newFish(fish14);net=newNet();score=0;}publicvoidaction()throwsInterruptedException{for(inti=0;iallfish.length;i++){allfish[i].start();}MouseAdapterl=newMouseAdapter(){publicvoidmouseEntered(MouseEvente){net.show=true;}publicvoidmousePressed(MouseEvente){for(inti=0;iallfish.length;i++){Fishfish=allfish[i];if(fish.catain(net.x,net.y)){fish.catchFish();score+=fish.width/10;}}}publicvoidmouseMoved(MouseEvente){intx=e.getX();inty=e.getY();net.x=x;net.y=y;}publicvoidmouseExited(MouseEvente){net.show=false;}};this.addMouseListener(l);this.addMouseMotionListener(l);while(true){repaint();Thread.sleep(1000/24);}}publicvoidpaint(Graphicsg){g.drawImage(bg,0,0,null);for(inti=0;iallfish.length;i++){Fishfish=allfish[i];g.drawImage(fish.image,fish.x,fish.y,null);}if(net.show){g.drawImage(net.image,net.x-net.width/2,net.y-net.height/2,null);}g.setColor(Color.white);g.setFont(newFont(黑体,Font.BOLD,15));g.drawString(得分:+score,10,20);}}classNet{intx;inty;intwidth;intheight;booleanshow;BufferedImageimage;publicNet()throwsException{image=ImageIO.read(newFile(net09.png));width=image.getWidth();height=image.getHeight();x=0;y=0;show=false;}}classFishextendsThread{intx;inty;intheight;intwidth;BufferedImageimage;BufferedImage[]images;intindex;intstep;publicFish(Stringpre)throwsException{images=newBufferedImage[10];for(inti=0;i9;i++){Stringfilename=pre+_0+(i+1)+.png;images[i]=ImageIO.read(newFile(filename));System.out.println(filename);}images[9]=ImageIO.read(newFile(pre+_10.png));image=images[0];width=image.getWidth();height=image.getHeight();Randomrandom=newRandom();x=random.nextInt(800-width);y=random.nextInt(480-height);step=random.nextInt(3)+1;}publicvoidrun(){while(true){x-=step;if(x-width){getout();}index++;image=images[index%images.length];if(index==100){index=0;}try{Thread.sleep(1000/24);}catch(InterruptedExceptione){e.printStackTrace();}}}publicvoidgetout(){x=800;Randomr=newRandom();y=r.nextInt(480-height);step=r.nextInt(3)+1;}publicvoidcatchFish(){getout();}publicbooleancatain(intx2,inty2){intdx=x2-x;intdy=y2-y;returndx0&&dxwidth&&dy0&&dyheight;}}组内分工(可选)实验中发现的问题及解决方法、实验运行效果:心得体会教师评语成绩
本文标题:捕鱼达人实验报告
链接地址:https://www.777doc.com/doc-5005683 .html