0

我正在做一个项目,我们应该有一只乌龟,它可以将命令向前、向左、向右、penUp、penDown、penColor 和退出。当笔放下时,它应该在点之间画一条线。当乌龟移动时,它应该留下一个脚印。左右应该以度为单位改变方向。到目前为止,我无法获得更多积分。这是我到目前为止的代码一类主要和一只乌龟

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);
 }
}
4

1 回答 1

0

你的程序有几个问题: StdDraw 认为坐标平面从 0.0 到 1.0 所以你的整数steps没有意义,除非你将它缩放到 StdDraw 坐标中——在下面的示例中,我将平面划分为 100 steps;移动后,您的moveForward()函数没有将xCoordandyCoord变量设置为新位置,因此海龟从未真正走到任何地方;目前尚不清楚为什么你如此依赖你Draw()的,因为它并没有真正做太多;程序退出逻辑被破坏;转弯逻辑被破坏且不完整。

在下面的返工中,出于示例目的,我进行了一些简化:我已将main()例程移入Turtle以避免额外的文件/类;我已经实现了更简单的颜色,因为你没有工作——你可以添加你的 RGB 模型;我简化了笔向上/向下逻辑,使其更实用。

import java.awt.Color;
import java.util.Scanner;

public class Turtle {
    private double degAngle;
    public double xCoord;
    public double yCoord;
    private boolean penDown;
    private Color penColor;
    private int steps;

    public Turtle() {
        xCoord = 0.5;
        yCoord = 0.5;
        penDown = true;
        penColor = StdDraw.BLACK;
        degAngle = 90;
        StdDraw.setPenRadius(0.01);
    }

    public void Draw() {
        if (penDown) {
            StdDraw.setPenColor(penColor);
            StdDraw.point(xCoord, yCoord);
        }
    }

    public void setColor(String color) {
        if (color.equalsIgnoreCase("red")) {
            penColor = StdDraw.RED;
        } else if (color.equalsIgnoreCase("green")) {
            penColor = StdDraw.GREEN;
        } else if (color.equalsIgnoreCase("blue")) {
            penColor = StdDraw.BLUE;
        } else if (color.equalsIgnoreCase("yellow")) {
            penColor = StdDraw.YELLOW;
        } else {
            penColor = StdDraw.BLACK;
        }

        this.Draw(); // show new color
    }

    public void moveForward(int steps) {

        double radAngle = Math.toRadians(degAngle);
        double newx = xCoord + (Math.cos(radAngle) * steps / 100);
        double newy = yCoord + (Math.sin(radAngle) * steps / 100);

        if (penDown) {
            StdDraw.setPenColor(penColor);
            StdDraw.line(xCoord, yCoord, newx, newy);
        }

        xCoord = newx;
        yCoord = newy;
    }

    public void turnRight(double angle) {
        degAngle += -angle;
    }

    public void turnLeft(double angle) {
        degAngle += angle;
    }

    public void putPenDown() {
        penDown = true;
    }

    public void putPenUp() {
        penDown = false;
    }

    public static void main(String[] args) {

        Turtle t = new Turtle();

        Scanner keyboard = new Scanner(System.in);
        String command;

        t.Draw(); // show turtle

        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");
                t.setColor(keyboard.next());

            } else if (command.equalsIgnoreCase("forward")) {
                System.out.println("Enter the number of steps you would like to move");
                t.moveForward(keyboard.nextInt());

            } else if (command.equalsIgnoreCase("right")) {
                System.out.println("Enter the number of degrees you would like to turn");
                t.turnRight(keyboard.nextDouble());

            } else if (command.equalsIgnoreCase("left")) {
                System.out.println("Enter the number of degrees you would like to turn");
                t.turnLeft(keyboard.nextDouble());

            } else if (command.equalsIgnoreCase("penup")) {
                t.putPenUp();

            } else if (command.equalsIgnoreCase("pendown")) {
                t.putPenDown();
            }

        } while (true);

    System.exit(0);
    }
}

用法

% java Turtle
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
pencolor 
Enter a color: red, green, black, yellow, blue
green
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
forward
Enter the number of steps you would like to move
20
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
left
Enter the number of degrees you would like to turn
120
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
forward
Enter the number of steps you would like to move
20
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
left
Enter the number of degrees you would like to turn
120
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
forward
Enter the number of steps you would like to move
20
Enter a command: forward, right, left, penup, pendown, pencolor, or quit
quit
%

输出

在此处输入图像描述

于 2017-02-24T18:34:43.220 回答