这是GameUtil里的
public static void addFishes(ArrayList<Fish> Fishes){
int count=rand.nextInt(6)+5;
for(int i=0;i<count;i++){
Fish f=new Fish();
f.setState(State_Swin);
f.setLevel(rand.nextInt(11)+1);
f.setDrawCount(15);
BufferedImage image=null;
BufferedImage[] img=new BufferedImage[f.getDrawCount()];
for(int j=1;j<f.getDrawCount();j++){
String path="image/"+f.getLevel()+"/"
+f.getState()+"/"+f.getState()+"_cycle."+j+".png";
try {
image=ImageIO.read(new FileInputStream(path));
img[j-1]=image;
} catch (IOException e) {
e.printStackTrace();
}
}
f.setImage(img);
f.setDrawIndex(0);
int x=rand.nextInt(800)-f.getWidth();
int y=rand.nextInt(480)-f.getHeight();
f.setPoint(new Point(x,y));
x=rand.nextInt(800)-f.getWidth();
y=rand.nextInt(480)-f.getHeight();
f.setTarget(new Point(x,y));
f.setDirection(GameUtil.getDirectionByPoint(f.getPoint(), f.getTarget()));
Fishes.add(f);
}
}
这是GameJPanel的DrawThread类
class DrawThread extends Thread{
public void run(){
while(true){
for(Fish f :fishes){
int nowDrawIndex=f.getDrawIndex()+1;
if(nowDrawIndex==f.getDrawCount()){
if(f.getState().equals(GameUtil.State_Eat))
f.setState(GameUtil.State_Swin);
if(f.getState().equals(GameUtil.State_Turn))
f.setState(GameUtil.State_Swin);
nowDrawIndex=0;
}
f.setDrawIndex(nowDrawIndex);
//还需改变每条鱼的point
//f.setPoint(new Point(f.getPoint().x,f.getPoint().y-10));
//游动到目的地,x,y分别需要移动的距离
int dx = f.getTarget().x-f.getPoint().x;
int dy = f.getTarget().y-f.getPoint().y;
//真实距离
double d = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
//x,y 相对d 的比率
double rateX = dx/d;
double rateY = dy/d;
//每0.1秒在x,与y轴移动的距离=比率*速度
int sx = (int)(rateX * GameUtil.FISH_SPEED);
int sy = (int)(rateY * GameUtil.FISH_SPEED);
//获取移动后鱼的坐标
int newX = f.getPoint().x+sx;
int newY = f.getPoint().y+sy;
if(Math.abs(f.getTarget().x-newX)<=8){
//已经到达了目的地
//产生新目的地
int x = rand.nextInt(800+f.getWidth())-f.getWidth();
int y = rand.nextInt(480+f.getHeight())-f.getHeight();
f.setTarget(new Point(x,y));
//产生了新目标,又有可能需要转变方向
f.setDirection(GameUtil.getDirectionByPoint(new Point(newX,newY),f.getTarget()));
}
//移动之后,将鱼设置到新位置
f.setPoint(new Point(newX,newY));
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//重画界面
GameJPanel.this.repaint();
}
}
}
这是Fish类
package eatfish;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
public class Fish {
private String state;
private int level;
private Point point;
private Point target;
public BufferedImage[] img;
private int direction=GameUtil.Direction_Left;
private boolean dead;
private Rectangle area;
private int width;
private int height;
private int drawCount;
private int drawIndex;
public void setPoint(Point point){
this.point=point;
}
public Point getPoint(){
return point;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public Rectangle getArea(){
Rectangle rect=new Rectangle();
Point p=new Point();
p.x=this.getPoint().x;
p.y=this.getPoint().y+this.getHeight()/3;
rect.setLocation(p);
rect.setSize((int)(this.getWidth()/3),(int)(this.getHeight()/3));
this.area=rect;
return area;
}
public boolean isDead(){
return dead;
}
public void setDead(boolean dead){
this.dead=dead;
}
public void setLevel(int level){
this.level=level;
}
public int getLevel(){
return level;
}
public int getDirection(){
return direction;
}
public void setDirection(int direction){
if(this.direction!=direction){
this.setState(GameUtil.State_Turn);
}
this.direction=direction;
}
public Point getTarget(){
return target;
}
public void setTarget(Point target){
this.target=target;
}
public String getState(){
return state;
}
public void setDrawCount(int drawCount){
this.drawCount=drawCount;
}
public int getDrawCount(){
return drawCount;
}
public void setDrawIndex(int drawIndex){
this.drawIndex=drawIndex;
}
public int getDrawIndex(){
return drawIndex;
}
public void setState(String state){
if(this.state!=state){
switch(state){
case GameUtil.State_Swin:{this.setDrawCount(15);this.setDrawIndex(0);}
case GameUtil.State_Turn:{this.setDrawCount(5);this.setDrawIndex(0);}
case GameUtil.State_Eat:{this.setDrawCount(5);this.setDrawIndex(0);}
}
}
this.state=state;
}
public BufferedImage getImage(){
return img[drawIndex];
}
public void setImage(BufferedImage[] img){
this.img=img;
}
}
/*BufferedImage image;
GameJPanel panel;
int DrawIndex=1;
String path;
public Fish(GameJPanel panel){
}
public void swim(BufferedImage img){
for(int i=1;i<15;i++){
path="image/1/swim/swim_cycle."+i+".png";
try {
image=ImageIO.read(new File(path));
this.image=img;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public BufferedImage getImage() {
return this.image;
}*/