Java 我如何让这个盒子在向右和向下移动后向左移动?

Java 我如何让这个盒子在向右和向下移动后向左移动?,java,processing,Java,Processing,我必须让一个盒子沿着之字形一直移动到屏幕底部。我想出了如何让盒子从左上角开始,移动到右上角,然后向下移动(一个完整的(x,y,90,90)盒子)。我现在被卡住了,因为我意识到我要写的下一条if语句正在与第一条if语句进行斗争,以便将方框滑动到屏幕的左端。我已经尝试了while和for循环来克服这个问题,但是我真的不知道该做什么或者在哪里做。我需要程序在屏幕上一直这样做,并在静止时停止(810810,90,90),然后停止。有什么建议吗 以下是我的主要代码: WBox v1; int a = 0

我必须让一个盒子沿着之字形一直移动到屏幕底部。我想出了如何让盒子从左上角开始,移动到右上角,然后向下移动(一个完整的(x,y,90,90)盒子)。我现在被卡住了,因为我意识到我要写的下一条if语句正在与第一条if语句进行斗争,以便将方框滑动到屏幕的左端。我已经尝试了while和for循环来克服这个问题,但是我真的不知道该做什么或者在哪里做。我需要程序在屏幕上一直这样做,并在静止时停止(810810,90,90),然后停止。有什么建议吗

以下是我的主要代码:

WBox v1; 
int a = 0;
float x=0;
float y=0;
void setup() {
  size(900, 900);
  frameRate(1000);
  v1 = new WBox();
}
void draw(){
  background(255);
  fill(0);
  rect(x,y,90,90);

  if(x<810){
    x+=1;
  } else if(y < 90){
    y+=1;
  } else if (x<=0){
    x-=1;
  }
}

完整免责声明:以不同的方式使用WBox类来实现这一点是更好的做法,但由于您似乎是初学者,我将坚持使用简单的东西。如果你愿意,你可以问我问题,我会呆一会儿


让我们用一个变量来尝试,而不是用一个硬编码的数字来表示移动速度:

float-boxSpeedX=1

我们只需要找出一个算法,让它下降。最好的方法是使用伪代码

换句话说:写机器代码后面的逻辑。大概是这样的:

  • 矩形必须水平移动,直到到达边框
  • 当它到达边界时,它必须下降90像素
  • 然后它向另一侧水平移动
  • 当它到达(810810)时,它应该停止并休息
  • 看起来很容易。让我们更接近代码:

    if (reached a border)
      box move horizontally toward the other border
    else
      box move downward 90 pixels
    
    if (box reached (810, 810)
      stop
    
    靠近点

    // let's start with the stop condition
    if (box reached 810, 810)
      do nothing
    else
      if (box is on an horizontal line)
        x += speed
        if (box reached right side)
          speed = -1 // so next time it'll go left
          y +=1 // this way the box isn't on an horizontal line anymore
        else if (box reached left side)
          speed = 1
          y += 1 
      else
        y += 1
    
    现在,让我们详细说明一些细节:

    (box is on an horizontal line):
      horizontal lines are multiples of 90 pixels
    
    (box reached right side)
      x == 810
    
    (box reached left side)
      x == 0
    
    我们将使用来了解y数是倍数还是90。花点时间来理解这个操作符,它真的非常有用。简言之,它将给你整数除法的余数(例如,如果你做13/5,整数除法将显示“2”,模将显示“3”,因为13中有2乘以5,而不是更多的“满5”,并且在这个操作后还有3个单位)

    我觉得我们已经足够接近了。剩下的只是用代码翻译。我是为你做的,我在代码中留下了注释。您可以将它复制并粘贴到处理IDE中,它就会运行。你可以在这个例子的基础上让你的盒子做任何你想做的事情

    WBox v1; 
    int a = 0;
    float x=0;
    float y=0;
    float boxSpeedX = 1;
    
    void setup() {
      size(900, 900);
      frameRate(1000);
      v1 = new WBox();
    }
    
    void draw() {
      background(255);
      fill(0);
      rect(x, y, 90, 90);
    
      moveBox();
    }
    
    void moveBox() {
      // first check for the stop condition:
      if (x != 810 || y != 810) {  // the box will move only while the stop condition isn't met
        // if the box is on a horizontal line, let's move it
        if (y % 90 == 0) { // the '%' operator's name is 'modulo', it's awesome and useful
          x += boxSpeedX;
          // once the box has moved, let's check if we need to steer it down:
          if (x == 0) { // if the box reaches the left side
            boxSpeedX = 1; //next time the box moves horizontally, it'll go right
            y += 1; // let's move down
          } else if (x == 810) {
            boxSpeedX = -1;
            y += 1;
          }
        } else {  // if the box isn't on an horizontal line, let's move it downward until it reaches a new line
          y += 1;
        }
      }
    }
    
    class WBox { 
      PVector wb;
    
      void box(int tempX, int tempY) {
    
        wb = new PVector(tempX, tempY); 
        wb.mult(a);//
        rect(wb.x, wb.y, 90, 90);
      }
    }
    

    玩得开心

    非常感谢你!我是一个初学者lol,我在python方面做得更好,所以我正在努力尝试在头脑中从python转换为处理。我一直在努力(x!=0 | | yI没有提到它,但是按照这种编码方式,您根本不需要WBox类。您的矩形是硬编码的,它的位置是全局变量。如果您计划将其制作成更复杂的东西,我强烈建议您将其制作成一个真实的类并将其作为一个类使用。我注意到,当我删除该类时,它仍然是一个全局变量成功了。我最初是将它作为一个布尔值来处理true和false,但我完全搞糊涂了,所以我想用一种更简单的方式来处理它,更接近python。我将返回并使用它作为指南,将它实现到类的布尔值程序中。太好了!OOP是一个很好的范例,你会看到的。希望再次听到它,祝你好运。
    WBox v1; 
    int a = 0;
    float x=0;
    float y=0;
    float boxSpeedX = 1;
    
    void setup() {
      size(900, 900);
      frameRate(1000);
      v1 = new WBox();
    }
    
    void draw() {
      background(255);
      fill(0);
      rect(x, y, 90, 90);
    
      moveBox();
    }
    
    void moveBox() {
      // first check for the stop condition:
      if (x != 810 || y != 810) {  // the box will move only while the stop condition isn't met
        // if the box is on a horizontal line, let's move it
        if (y % 90 == 0) { // the '%' operator's name is 'modulo', it's awesome and useful
          x += boxSpeedX;
          // once the box has moved, let's check if we need to steer it down:
          if (x == 0) { // if the box reaches the left side
            boxSpeedX = 1; //next time the box moves horizontally, it'll go right
            y += 1; // let's move down
          } else if (x == 810) {
            boxSpeedX = -1;
            y += 1;
          }
        } else {  // if the box isn't on an horizontal line, let's move it downward until it reaches a new line
          y += 1;
        }
      }
    }
    
    class WBox { 
      PVector wb;
    
      void box(int tempX, int tempY) {
    
        wb = new PVector(tempX, tempY); 
        wb.mult(a);//
        rect(wb.x, wb.y, 90, 90);
      }
    }