捕鱼达人实验报告.docx
《捕鱼达人实验报告.docx》由会员分享,可在线阅读,更多相关《捕鱼达人实验报告.docx(8页珍藏版)》请在冰豆网上搜索。
捕鱼达人实验报告
山西财经大学《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();
}
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;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;iallfish[i].start();
}
MouseAdapterl=newMouseAdapter(){
publicvoidmouseEntered(MouseEvente){
net.show=true;
}
publicvoidmousePressed(MouseEvente){
for(inti=0;iFishfish=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;iFishfish=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;i<9;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;
returndx>0&&dx0&&dy}
}
组内分工(可选)
实验中发现的问题及解决方法、实验运行效果:
心得体会
教师评语
成绩