捕鱼达人实验报告.docx
- 文档编号:11155441
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:8
- 大小:21.75KB
捕鱼达人实验报告.docx
《捕鱼达人实验报告.docx》由会员分享,可在线阅读,更多相关《捕鱼达人实验报告.docx(8页珍藏版)》请在冰豆网上搜索。
捕鱼达人实验报告
山西财经大学《JAVA实用教程》实验报告
年月日
实验题目
捕鱼达人
选课课号
学院
班级
姓名
学号
理论课教师
上机指导教
师
实验目的及要求:
捕鱼达人游戏的要求:
1.在鱼池有很多鱼且鱼各自游动
2.有一张渔网,随鼠标移动点击鼠标即可抓住渔网中的与
3.抓到鱼进行积分
实验原理、详细操作步骤及程序代码:
1.理清对象之间的数据关系。
对象:
窗口鱼池,多条鱼,渔
网;
2.数据模型:
鱼和渔网的width,height,x,y;
3.类的设计
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.Bufferedlmage;
importjava.io.File;
importjavax.imageio」magelO;
importjavax.swing.*;
importjava.util.Random;
publicclassFishDemoextendsThread{
publicstaticvoidmain(String[]args)throwsException{
FishDemofish=newFishDemo();
fish.start();
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{
Bufferedlmagebg;
Fish[]allfish;
Netnet;
intscore;
publicPool()throwsException{
bg=lmagelO.read(newFile("bg.jpg"));
allfish=newFish[9+2+9];
for(inti=0;i<9;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;i allfish[i].start(); } MouseAdapterl=newMouseAdapter(){ publicvoidmouseEntered(MouseEvente){net.show=true; } publicvoidmousePressed(MouseEvente){ for(inti=0;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.drawlmage(bg,0,0,null); for(inti=0;i Fishfish=allfish[i]; g.drawlmage(fish.image,fish.x,fish.y,null); } if(net.show){ g.drawlmage(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=lmagelO.read(newFile("net09.png")); width=image.getWidth(); height=image.getHeight(); x=0; y=0; show=false; } } classFishextendsThread{ intx; inty; intheight; intwidth; BufferedImageimage; Bufferedlmage[]images; intindex; intstep; publicFish(Stringpre)throwsException{ images=newBufferedlmage[10]; for(inti=0;i<9;i++){ Stringfilename=pre+"_0"+(i+1)+".png";images[i]=lmagelO.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.nextlnt(800-width); y=random.nextlnt(480-height); step=random.nextlnt(3)+1; } publicvoidrun(){ while(true){ x-=step; if(x<-width){ getout(); } index++; image=images[index%images」ength]; if(index==100){index=0;} try{ Thread.sleep(1000/24); }catch(InterruptedExceptione){ e.printStackTrace(); } } } publicvoidgetout(){ x=800; Randomr=newRandom。 ; y=r.nextlnt(480-height); step=r.nextlnt(3)+1; } publicvoidcatchFish(){ getout(); publicbooleancatain(intx2,inty2){ intdx=x2-x; intdy=y2-y; returndx>0&&dxvwidth&&dy>0&&dyvheight; } } 组内分工(可选) 实验中发现的问题及解决方法、实验运行效果: 心得体会 教师评语 成绩
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 捕鱼 实验 报告