I'm trying to make my triangles redraw themselves when I click the mouse, but they only just multiply when I mouse click

huangapple go评论66阅读模式
英文:

I'm trying to make my triangles redraw themselves when I click the mouse, but they only just multiply when I mouse click

问题

当我运行这个程序时,点击后,绿色的三角形会出现在各个地方,而我希望它重新定位到一个随机的位置,并移除原始的绿色三角形。

float x;

void setup()
{
size (800, 600);
background(29, 154, 178);
x=random(width);
frameRate(30);
}

void draw()
{
noStroke();

fill(91, 180, 118 );
triangle(x, 20, 0, height, width, height);

fill(82, 45, 80);
triangle(width/2-200, 120, 0, height, width, height);

fill(82, 45, 60);
triangle(width/2+150, 220, 0, height, width, height);

fill(82, 45, 28);
triangle(width/2-100, 320, 0, height, width, height);
fill(243, 245, 158);
rect(0, 525, 800, 100);

if(mousePressed == true)
{
x=random(width);
}
}


<details>
<summary>英文:</summary>

When I run this program the green triangles appear all over the place when I click instead I want it to reposition itself to a random place and remove the original green triangle

float x;

void setup()
{
size (800, 600);
background(29, 154, 178);
x=random(width);
frameRate(30);
}

void draw()
{
noStroke();

fill(91, 180, 118 );
triangle(x, 20, 0, height, width, height);

fill(82, 45, 80);
triangle(width/2-200, 120, 0, height, width, height);

fill(82, 45, 60);
triangle(width/2+150, 220, 0, height, width, height);

fill(82, 45, 28);
triangle(width/2-100, 320, 0, height, width, height);
fill(243, 245, 158);
rect(0, 525, 800, 100);

if(mousePressed == true)
{
x=random(width);
}
}


</details>


# 答案1
**得分**: 1

然后在你的`setup`中添加`noLoop()`,这样你就不会以每秒60次的频率绘制相同的内容,而是使用[鼠标点击处理](https://processing.org/reference/mouseClicked_.html),结合[redraw](https://processing.org/reference/redraw_.html)调用,这样只有在有新内容需要绘制时才进行绘制:

```java
float x, y, triangleSideLength;

void setup() {
  size(300, 300);
  noLoop();
  triangleSideLength = 30; // 或者根据需要设定合适的值
}

void draw() {
  x = random(0, width - triangleSideLength);
  y = random(0, height - triangleSideLength);
  drawTriangleAt(x, y);
}

void drawTriangleAt(float x, float y) { 
  //...三角形绘制代码在这里...
}

void mouseClicked() {
  redraw();
}

另外还要注意通常要约束你的x/y坐标,以便三角形始终“适合屏幕”,而不是获得一个x/y坐标,使得x=width,导致三角形基本上完全看不见。

英文:

Then add noLoop() in your setup so you're not drawing the same thing 60 times a second, and use mouse click handling instead, with a redraw call so you only draw when there is something new to draw:

float x, y, triangleSideLength;

void setup() {
  size(300,300);
  noLoop();
  triangleSideLength = 30; // or whatever value it needs to be of course
}

void draw() {
  x = random(0, width - triangleSideLength);
  y = random(0, height - triangleSideLength);
  drawTriangleAt(x, y);
}

void drawTriangleAt(float x, float y) { 
  //...triangle drawing code here...
}

void mouseClicked() {
  redraw();
}

Also note that you usually want to constrain your x/y coordinates so that a triangle will always "fit on the screen" instead of getting an x/y coordinate that happens to have x=width and now your triangle is basically 100% out of view.

huangapple
  • 本文由 发表于 2020年9月17日 00:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63924036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定