开发工具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;
//定义随机random
Random 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++;
}
}
}