开发工具eclipse,jdk1.8
文档截图:
package com.qd.fish;import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;public class Fish {//定义鱼的图片BufferedImage fishImage;//定义鱼的数组帧BufferedImage[] fishFrame;//定义鱼的帧数int num;//定义鱼的坐标int x,y;//定义鱼的移动速度int v;//定义鱼的宽高int w,h;//定义随机randomRandom r=new Random();//定义当前鱼的显示帧int frameIndex=0;//捕捉的概率int rate;//标识是否被捕捉boolean isCatch=false;//定义分值int point;public Fish(BufferedImage fishImage,int num,int w,int h,int rate,int point){this.fishImage=fishImage;this.num=num;this.w=w;this.h=h;this.rate=rate;this.point=point;//切帧fishFrame=new BufferedImage[num];for(int i=0;i<num;i++){fishFrame[i]=fishImage.getSubimage(0, h*i, w, h);}//初始化坐标和速度x=-r.nextInt(21)-w;//-w~(-w-20)y=r.nextInt(410-h/2);v=r.nextInt(10)+2;//2-11}//移动public void move(){x=x+v;//如果移出渔场if(x>800){x=-r.nextInt(21)-w;y=r.nextInt(410-h/2);v=r.nextInt(10)+2;}}//画鱼的方法public void draw(Graphics g) {if(!isCatch){g.drawImage(fishFrame[frameIndex],x, y, null);frameIndex++;if(frameIndex>num/2-1) {frameIndex=0;}}else{//被捕捉if(frameIndex<num/2) {frameIndex=num/2;}g.drawImage(fishFrame[frameIndex],x, y, null);frameIndex++;}}
}