私は、コマンドを前方、左、右、penUp、penDown、penColor、quitのいずれかにすることができるカメを飼う予定のプロジェクトに取り組んでいます。ペンが下がっているときは、ポイント間に線を引くことになっています。カメの動きに伴って足跡が残るはずです。左と右は度で方向を変えています。これまでのところ、私はそれ以上のポイントを得ることはできません。 StdDrawは、あなたがない限り意味がありません、あなたの整数でsteps
ので、0.0から1.0に行くと座標平面を考える:これは、これまでの1クラスのメインと1匹の亀Turtle Project Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Turtle t = new Turtle();
Scanner keyboard = new Scanner(System.in);
String command;
StdDraw.setPenRadius(0.05);
t.Draw();
do {
System.out.println("Enter A Command: forward, right, left, penup, pendown, pencolor, or quit");
command = keyboard.next();
if (command.equalsIgnoreCase("quit")){
break;
}
else if (command.equalsIgnoreCase("pencolor")) {
System.out.println("Enter A Color: Red, Green, Black, Yellow, Blue");
String color = keyboard.next();
t.setColor(color);
t.Draw();
}
else if (command.equalsIgnoreCase("forward")) {
System.out.println("Enter The Number of Steps You Would Like To Move");
int steps = keyboard.nextInt();
t.moveForward(steps);
t.Draw();
} else if (command.equalsIgnoreCase("right")) {
System.out.println("Enter The Number Of Degrees You Would Like To Move");
String radAngle = keyboard.next();
t.Draw();
} else if (command.equalsIgnoreCase("left")) {
System.out.println("Enter The Number Of Degrees You Would Like To Move");
String radAngle = keyboard.next();
t.Draw();
} else if (command.equalsIgnoreCase("penup")) {
t.putPenUp();
t.Draw();
} else if (command.equalsIgnoreCase("pendown")) {
t.putPenDown();
t.Draw();
}} while (!(command.equalsIgnoreCase("quit")));
}
}
public class Turtle {
private double location;
public double xCoord;
public double yCoord;
double direction;
private boolean penPosition;
public static boolean penDown = false;
public static boolean penUp = true;
private int red;
private int green;
private int blue;
private int steps;
public Turtle() {
xCoord = .5;
yCoord = .5;
penPosition = penUp;
location = 90;
}
public void Draw(){
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.point(xCoord, yCoord);
}
public void setColor(String color) {
if (color.equalsIgnoreCase("red")) {
red = 255;
green = 0;
blue = 0;
} else if (color.equalsIgnoreCase("green")) {
red = 0;
green = 255;
blue = 0;
} else if(color.equalsIgnoreCase("blue")) {
red = 0;
green = 0;
blue = 255;
} else if(color.equalsIgnoreCase("yellow")) {
red = 255;
green = 255;
blue = 0;
} else if(color.equalsIgnoreCase("black")) {
red = 0;
green = 0;
blue = 0;
} else {
red = 0;
green = 0;
blue = 0;
}
}
public void moveForward(int steps) {
double radAngle = Math.toRadians(location);
double newx = xCoord + (Math.cos(radAngle) * steps);
double newy = yCoord + (Math.sin(radAngle) * steps);
StdDraw.point(newx, newy);
StdDraw.line(xCoord, yCoord, newx, newy);
StdDraw.show();
}
public void putPenDown() {
penPosition = penDown;
if (true) {
// StdDraw.line(x, y, xCoord, yCoord);
}
} public void putPenUp() {
penPosition = penUp;
// StdDraw.line(xCoord, yCoord, newx, newy);
}
}
ありがとうそんなに男 –