仅将背景用于一个功能

Using background for only one function

我正在 processing 制作动画。然后,我对代码有疑问。通常,我的代码比较长。但是,我制作了一个简单的代码,对初学者也很有用。我的示例代码:

int x1 = 0;
int x2 = 0;

void setup() {
  size(500, 100); // create a screen
}

void draw() {
  background(255);
  drawRectangle();
  drawPoint();
}

void drawRectangle() {
  rect(x1, 20, 20, 20); // rect(x, y, width,height);
  x1 +=5;
}

void drawPoint() {
  point(x2, 20); // point(x, y);
  x2++;
}

所以,我在同一方向上有点和矩形。 background 对两者都有影响。但是,背景不应影响要点。因为我想用点划线。背景应该只影响矩形。 解决方案应如下所示:

您将希望对此采取更简单的方法。不要画一条有多个点的线,而是画一条实际的线!而且你也只能使用 1 x

这是一个小示例程序,产生以下输出:

int x = 10, y = 50;

void setup() {
  size(400, 400);
  rectMode(CENTER); // So the values you give rect() are seen as the center of the rectangle
}

void draw() {
  background(255);

  drawLine();
  drawRectangle();

  x += 3;
}

void drawLine() {
  strokeWeight(5); // Make the line more visible
  line(0, y, x, y); // Draw a line from the left of the screen to x
  strokeWeight(1); // Return to standard
}

void drawRectangle() {
  fill(255, 0, 0); // Make the color red
  rect(x, y, 20, 20); // Draw the rectangle
}