基本Java Turtle图形动画

时间:2016-03-09 03:12:58

标签: java oop

我正在编写一个接近完成的程序,但我遇到了一些小问题。我正在编写一个程序来移动显示输出周围的动画海龟。我的程序运行完美,但我需要有一个测试器类,它将以OOP样式调用我的函数,我不知道如何实现。此外,程序代码必须允许用户影响动画,另一点我需要帮助。

如果有人能够输入一些有助于解决这些问题的代码,那将非常感激。谢谢。

这是一个动画程序,可以在屏幕上移动动画的彩色海龟。

import java.util.Random;
import java.util.Date;
import java.util.List;
import java.awt.Color;

public class Main{
public static void main(String[] args){
new Runner().run();
  }//end main method
}//end class Main
//------------------------------------------------------//

class Runner{

  Random randGen = new Random(new Date().getTime());


  int aquariumWidth = 450;
  int aquariumHeight = 338;
  World aquarium = new World(
                        aquariumWidth,aquariumHeight);


  List turtleList = aquarium.getTurtleList();
  //----------------------------------------------------//

void run(){
aquarium.setPicture(new Picture("aquarium.gif"));

int numberTurtles = 8;


for(int cnt=0;cnt < numberTurtles;cnt++){
  int xCoor =
          Math.abs(randGen.nextInt() % aquariumWidth);
  int yCoor =
         Math.abs(randGen.nextInt() % aquariumHeight);
  new Turtle(xCoor,yCoor,aquarium);
}//end for loop

int angle = 0;
int leaderMove = 0;

Turtle turtle = null; //empty container
Turtle testTurtle = null; //empty container 


Turtle leader = (Turtle)turtleList.get(0);
leader.setShellColor(Color.RED);
int turtleLength = leader.getHeight();


turtle = (Turtle)turtleList.get(3);
turtle.setBodyColor(Color.YELLOW);
turtle.setShellColor(Color.ORANGE);
turtle = (Turtle)turtleList.get(7);
turtle.setBodyColor(Color.ORANGE);
turtle.setShellColor(Color.YELLOW);

while(true){//animation loop will run forever

  leaderMove = (int)(turtleLength/2
               + turtleLength*randGen.nextDouble()/4);

  angle = (int)(45*(randGen.nextDouble() - 0.5));


  for(int cnt = 0;cnt < turtleList.size();cnt++){
    turtle = (Turtle)turtleList.get(cnt);
    turtle.penUp();//no turtle tracks allowed

    //Force the turtles to maintain some distance
    // between them 
    for(int cntr = 1;cntr < turtleList.size();cntr++){
      testTurtle = (Turtle)turtleList.get(cntr);
      //Use size as a part of the equation and increasy by one
      if((testTurtle != turtle) && (cnt != 0)){
        int separation = (int)(turtle.getDistance(
          testTurtle.getXPos(),testTurtle.getYPos()));

          if(separation < 2*turtleLength){

            turtle.turnToFace(testTurtle);
            turtle.turn(180);
            turtle.forward(turtleLength/3);
          }//end if
      }//end if
    }//end for loop on turtle separation

    if(cnt == 0){
      //This is the leader

      int xPos = leader.getXPos();
      int yPos = leader.getYPos();

      if(xPos < turtleLength){
        leader.setHeading(90);
      }else if(xPos > aquariumWidth -turtleLength -2){
        leader.setHeading(-90);
      }//end else

      if(yPos < turtleLength){
        leader.setHeading(180);
      }else if(
             yPos > aquariumHeight -turtleLength - 2){
        leader.setHeading(0);
      }//end else


      leader.turn(angle);
      leader.forward(leaderMove);
    }else{

      turtle.turnToFace(leader);
      int distanceToLeader = (int)(turtle.getDistance(
                  leader.getXPos(),leader.getYPos()));
      turtle.forward(distanceToLeader/10);
    }//end else

  }//end for loop processing all turtles

 //This took some looking over to find the right methods to control the speed. 
  try{
    Thread.currentThread().sleep(100);
  }catch(InterruptedException ex){
  }
}

}
}

0 个答案:

没有答案