2017-02-23 9 views
0

私は、コマンドを前方、左、右、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); 
} 
} 

答えて

0

あなたのプログラムはいくつかの問題を抱えている私が持っているコードです。それをStdDraw座標にスケーリングします - 下の私の例では、飛行機を100に分割しますsteps; moveForward()関数は移動後に新しい場所にxCoordyCoord変数を設定しなかったので、タートルは決して実際にはどこにも行きませんでした。 Draw()に本当に多くのことをしていないのはなぜそうはっきりしていません。プログラム出口ロジックが壊れていた。回転ロジックが壊れて不完全でした。

私のやり直しでは、いくつかの単純化のために例を挙げました。余分なファイル/クラスを避けるために、ルーチンを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); 
    } 
} 

USAGE

% 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 
% 

OUTPUT

enter image description here

+0

ありがとうそんなに男 –