Java之Eclipse实现——在海绵宝宝的比基尼海滩里投喂鱼食

背景图片可以在自己的电脑任意选取一张大小合适的图片,重命名为backround.jpg,把之前的backround.jpg删除,然后直接粘贴进去
在这里插入图片描述

抓取图片展示:
在这里插入图片描述

AquariumSimulator类

/*
 * File: BlankClass.java
 * -
 *--------------------
 * This class is a blank one that you can change at will. Remember, if you change
 * the class name, you'll need to change the filename so that it matches.
 * Then you can extend GraphicsProgram, ConsoleProgram, or DialogProgram as you like.
 */
import acm.program.*;

public class AquariumSimulator extends Program {
	
	// The animation constant.
	private static final double DELAY = 1000.0 / 50;

	/*引入fish*/
	private Tank fishTank = new Tank();
	
	public void run() {
		/*设置屏幕大小*/
		setSize(Tank.SCREEN_WIDTH, Tank.SCREEN_HEIGHT);
		// adds the fishtank (which is a GCanvas) to the screen
		add(fishTank);
		fishTank.init();
		// animates the fishTank
		while(true) {
			fishTank.heartbeat();
			pause(DELAY);
		}
	}
}

Fish类

import acm.graphics.*;
import acm.util.RandomGenerator;

public class Fish{
	private static final double MOVE_AMT = 3.0;//移动速度
	private GImage img = null;//鱼图也决定鱼
	private GPoint goal = null;//鱼饵
	private boolean isLeftImgShown = false;//是否朝左
	
	public Fish() {
		img = new GImage("blueFishRight.png");
		isLeftImgShown = false;
	}
	public void heartbeat() {//没鱼食了加,把鱼食打印到鱼的目标上
		if(goal == null) {
			setGoal();
		}
		moveTowardsGoal();
	}
	public GImage getImage() {//传出鱼的图
		return img;
	}
	private void moveTowardsGoal() {//决定游泳朝向
		double dy = goal.getY() - img.getY()-60;
		double dx = goal.getX() - img.getX()-100;
		double dist = Math.sqrt(dx * dx + dy * dy);
		if(dist > MOVE_AMT) {//距离大于游泳速度
			// move MOVE_AMT pixels towards the goal
			double moveX = MOVE_AMT / dist * dx;
			double moveY = MOVE_AMT / dist * dy;
			img.move(moveX, moveY);
		} else {//吃掉了鱼饵
			goal = null;
			Tank.fishfood.beichi();
		}
		makeImageMatchDirection(dx);
	}
	private void makeImageMatchDirection(double dx) {//转换方向
		boolean shouldFaceLeft = dx < 0;
		if(shouldFaceLeft != isLeftImgShown) {//是否与原方向相同
			if(shouldFaceLeft) {
				img.setImage("blueFishLeft.png");
			} else {
				img.setImage("blueFishRight.png");
			}
			isLeftImgShown = shouldFaceLeft;
		}
	}
	private void setGoal() {//随机设立鱼饵
		RandomGenerator rg = RandomGenerator.getInstance();
		double maxX = Tank.SCREEN_WIDTH - img.getWidth();
		double maxY = Tank.SCREEN_HEIGHT - img.getHeight();
		double goalX = rg.nextDouble(0, maxX);
		double goalY = rg.nextDouble(0, maxY);
		goal = new GPoint(goalX,goalY);
	}
	public void findfood(double x,double y){
		goal = new GPoint(x,y);
	}
}

Tank类

//Tank
/*
 * File: BlankClass.java
 * ---------------------
 * This class is a blank one that you can change at will. Remember, if you change
 * the class name, you'll need to change the filename so that it matches.
 * Then you can extend GraphicsProgram, ConsoleProgram, or DialogProgram as you like.
 */
import java.util.ArrayList;
import acm.graphics.*;

public class Tank extends GCanvas {//鱼缸
	public static final int SCREEN_WIDTH = 800;
	public static final int SCREEN_HEIGHT = 600;
	private ArrayList<Fish> fishes = new ArrayList<Fish>();
	public static final Fishfood fishfood = new Fishfood();
	public void init() {//设立背景和鱼
		addBackground();
		addFishes();
	}
	private void addBackground() {//设立背景
		GImage img = new GImage("background.jpg");
		img.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
		add(img);
		fishfood.Initfishfood();//							1
	}
	private void addFishes() {//设立鱼
		for(int i = 0; i < 3; i++) {
			Fish fish = new Fish();//加鱼
			add(fish.getImage());
			fishes.add(fish);
		}
	}
	public void heartbeat() {
		for(Fish fish : fishes) {
			fish.heartbeat();
			if(!fishfood.exit){
				if(System.currentTimeMillis()>fishfood.startTime){
					fishfood.exit = true;
					fishfood.Initfishfood();
					fish.findfood(fishfood.x, fishfood.y);
					fishfood.fishfod.setFilled(true);
					add(fishfood.fishfod,fishfood.x,fishfood.y);
				}
			}else {
				fish.findfood(fishfood.x, fishfood.y);
			}
		}
	}
}

Fishfood类

import acm.util.RandomGenerator;
import java.awt.Color;
import acm.graphics.*;

public class Fishfood {
	boolean exit;
	public GOval fishfod;
	public double x,y;
	public long Sleptime;
	public long startTime;
	Fishfood(){
		this.exit = false;
		this.x = 0;
		this.y = 0;
		this.Sleptime = 2000;
		this.startTime = System.currentTimeMillis()+this.startTime;
		fishfod = new GOval(20,20);
		fishfod.setFilled(false);
		fishfod.setFillColor(Color.yellow);
	}
	public void Initfishfood(){
		RandomGenerator rg = RandomGenerator.getInstance();
		this.x = rg.nextDouble(0, Tank.SCREEN_WIDTH);
		this.y = rg.nextDouble(0, Tank.SCREEN_HEIGHT);
	}
	public void beichi(){
		this.exit = false;
		fishfod.setFilled(false);
		this.startTime = System.currentTimeMillis()+this.Sleptime;
	}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米莱虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值