Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Processing 如何使用“绘制一个动画矩形以实现某些动画”;处理「;_Processing - Fatal编程技术网

Processing 如何使用“绘制一个动画矩形以实现某些动画”;处理「;

Processing 如何使用“绘制一个动画矩形以实现某些动画”;处理「;,processing,Processing,一种处理程序,它在窗口的左上角绘制一个矩形,并将其向右移动,直到它位于屏幕的右边缘。然后向下移动,直到它位于底部。然后把它移到左边。最后,将其移回左上角并重新开始 这是我未完成的代码,我不知道当它到达底线时如何让它向左移动: void setup(){ 尺寸(500500); } int x=0; int y=0; int-dy=2; int dx=2; 作废提款(){ x+=dx; 如果(x>=(宽度-50)){ dx=0; y+=dy; } 如果(y>=(高度-50)){ dy=0; x+

一种处理程序,它在窗口的左上角绘制一个矩形,并将其向右移动,直到它位于屏幕的右边缘。然后向下移动,直到它位于底部。然后把它移到左边。最后,将其移回左上角并重新开始

这是我未完成的代码,我不知道当它到达底线时如何让它向左移动:

void setup(){
尺寸(500500);
}
int x=0;
int y=0;
int-dy=2;
int dx=2;
作废提款(){
x+=dx;
如果(x>=(宽度-50)){
dx=0;
y+=dy;
} 
如果(y>=(高度-50)){
dy=0;
x+=(-dx);
}
rect(x,y,50,50,7);
}
这是一个解决方案:

void setup(){
size(500,500);
}

int direction = 0;
int x = 0;
int y = 0;

void draw(){

  switch (direction)
  {
     case 0: //Right
        x += 1;        
        if (x+50 >= width) direction = 1;
     break;

     case 1: //Down
        y += 1;        
        if (y+50 >= height) direction = 2;
     break;

     case 2: //Left
        x -= 1;        
        if (x <= 0) direction = 3;
     break;

     case 3: //Up
        y -= 1;        
        if (y <= 0) direction = 0;
     break;
   }

   rect(x, y, 50,50, 7);
}
void setup(){
尺寸(500500);
}
int方向=0;
int x=0;
int y=0;
作废提款(){
开关(方向)
{
案例0://对
x+=1;
如果(x+50>=宽度)方向=1;
打破
案例1://向下
y+=1;
如果(y+50>=高度)方向=2;
打破
案例2://左
x-=1;

如果(x除了@Titulum的答案之外,我还建议你使用对象,因为它比你现在处理点的方式更容易

Rectangle rect;
int direction;
int speed;

void checkDirections(){
switch (direction)
  {
     case 0: //Right
        rect.x += speed;        
        if (rect.x+rect.x1 >= width) direction = 1;
     break;

     case 1: //Down
        rect.y += speed;        
        if (rect.y+rect.y1 >= height) direction = 2;
     break;

     case 2: //Left
        rect.x -= speed;        
        if (rect.x <= 0) direction = 3;
     break;

     case 3: //Up
        rect.y -= speed;        
        if (rect.y <= 0) direction = 0;
     break;
   }
}


void setup(){
  size(500,500);
  direction = 0;
  speed = 5;
  rect = new Rectangle(0, 0, 50, 50);
}

void draw(){
  background(255);
  checkDirections();
  rect.drawRect();
}

class Rectangle{
  float x,y,x1,y1;
  Rectangle(float x, float y, float x1, float y1){
    this.x = x;
    this.y = y;
    this.x1 = x1;
    this.y1 = y1;
  }
  void drawRect(){
    fill(0);
    rect(x,y,x1,y1);
  }
}
rect矩形;
int方向;
整数速度;
void checkDirections(){
开关(方向)
{
案例0://对
矩形x+=速度;
如果(矩形x+矩形x1>=宽度)方向=1;
打破
案例1://向下
矩形y+=速度;
如果(y向+y向>=高度)方向=2;
打破
案例2://左
矩形x-=速度;

如果(rect.x)两个变量不够。您还需要左和右两个变量。请同时查看:不客气!不要忘记标记大多数回答您问题的答案,以便其他用户可以看到问题已经解决。小错误:x=0应该是“int x=0”;y=0也是如此。确实如此,我更改了它。