背景图片可以在自己的电脑任意选取一张大小合适的图片,重命名为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;
}
}