Remember a previous location of a player object

97 Views Asked by At

Ok, so I am trying to create a program where, using scanner, if the user types "up", "down", "left", or "right", the shape will move accordingly. I got the shape to move as the words say, but whenever I enter a new command, I cannot get the program to remember where the shape was before the new command was given. The shape starts off at a 0,0 every time I enter a new direction. Here is what I have so far, I believe the problem is with my y-coordinate:

 double location = 0;
 double prev = 0;     
 for (double i = -10; i <= 10; i++) // for the scale that was set 
 {
  String word = in.nextLine(); // new string: word = user typed

  if (word.equals("up")) // if word = up, move player up
  {
    location ++;
    prev -= location;
    StdDraw.clear();
    drawTarget(a);
    StdDraw.setPenColor(StdDraw.BLUE);
    StdDraw.filledCircle(prev, location, 0.5);

  }

 else if (word.equals("down")) // if word = down, move player down
  {
    location--;
    prev += location;
    StdDraw.clear();
    drawTarget(a);
    StdDraw.setPenColor(StdDraw.BLUE);
    StdDraw.filledCircle(prev, location, 0.5);

  }
 }
0

There are 0 best solutions below