捕鱼达人(四)

    在上篇文章中,鱼是作为面板的一部分被处理着,一条鱼只有一个,当处理多条鱼的时候,鱼变量首先的改变就是

变成鱼数组,现在看下关于鱼的图片名的规律。


在第一篇中用的是index变量来控制同种鱼的动画,产生游动的效果,当处理多条鱼的时候,另外还需要加一个变量prefix来控制鱼的种类,在上一篇控制鱼游动的for循环外还需外套一个for循环,使得多条鱼能够同时游动。为了便于控制鱼的速度,在这里,用prefix控制的鱼种类来影响鱼的速度,另外还有鱼y坐标的位置,多条鱼不可能从同一个出发点出发,所以这里鱼出发的y轴坐标调用radom函数来随机产生,其主要的Fish.java代码如下:

package my.ui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class Fish extends Thread{
	
	//当前帧的鱼图片
	BufferedImage fishImage;
	//鱼游动10帧的动画
	BufferedImage[] animation=new BufferedImage[10];
	//鱼翻转8帧的动画
	BufferedImage[] animation_catch;
	
	int location_x;
	int location_y;
	int fish_width;
	int fish_height;
	int fishIndex;
	int n;
	int speed;
	
	public Fish(int fishIndex){
		this.fishIndex=fishIndex;
		this.n=fishIndex<=7?2:4;
		this.speed=fishIndex;
		this.location_x=800;
		this.location_y=new Random().nextInt(400);//[0,400)
		this.animation_catch=
			new BufferedImage[this.n*2];
		init();
		this.fish_width=fishImage.getWidth();
		this.fish_height=fishImage.getHeight();
	}
	public void init(){
		//将硬盘上的图片加载到内存中,用background存储到app
		//描述内存中的文件系统
		try {
			String fishImagePath="";//"images\\fish13_01.png"
			//初始化鱼游动的动画数组
			String prefix= 
				(fishIndex<10?"0":"")+fishIndex; 
			for(int i=1;i<=10;i++){
				String index= (i<10?"0":"")+i;
				fishImagePath=
					"images\\fish"+prefix+"_"+index+".png";
				File fishFile=
					new File(fishImagePath);
				animation[i-1]=ImageIO.read(fishFile);
			}
			//初始化鱼翻转的动画数组
			for(int i=1;i<=2;i++){
				for(int j=1;j<=this.n;j++){
					fishImagePath=
						"images\\fish"+prefix+"_catch_0"+j+".png";
					System.out.println(fishImagePath);
					File fishFile=
						new File(fishImagePath);
					System.out.println("****"+fishFile.getName());
					animation_catch[j+(i-1)*this.n-1]=
						ImageIO.read(fishFile);
					System.out.println();
				}
			}
			
			fishImage=animation[0];
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//执行鱼游动的动画
	public void swimming(){
		int step=1;
		while(true){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			step=step%10;
			System.out.println("鱼游动到了"+(step+1)+"帧...");
			fishImage=animation[step++];
			location_x -= this.speed;
//			panel.repaint();//重新执行paint(),完成界面的重绘
			//判断鱼是否撞墙 并完成翻转动画
			if(location_x <= -fish_width){
				this.location_x=800;
				this.location_y=new Random().nextInt(400);//[0,400)
			}
		}
	}
	//执行翻转的动画
	public void turnover(){
		for(int i=1;i<=this.n*2;i++){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			fishImage=animation_catch[i-1];
//			panel.repaint();
		}
	}
	//鱼清理出界面
	public void getout(){
		fishImage=null;
//		panel.repaint();
	}
	
	//一条鱼线程执行的内容
	public void run() {
		swimming();
		turnover();
		getout();
	}	
}
在这个类的处理中主要是细节方面的处理,其最后的界面如下:



多条鱼就这样快乐的游着,但是都会游到最左边的时候撞到界面,死亡,消失。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值