需要处理帮助,绘制随机的建筑物集


void setup() {
  size(800, 500);
}
float x=20;
int y=480;
int aptSize=20;
float a, e;
float currentFloor=0;
float s;
float offset;
//float aptNum;
void draw() {
  //draw sky
  //  sky();
  float n=0;    
// if(keyPressed){
 // sky();
  //draw a building
  for (int f=0; f<3 && x<600; f++, x = x + ((n*20)+20)) {
    n = building(x, y, 15);
  }
}
//}    
void sky() {
  float r= 50;
  float g= 0;
  float b=200;
    background(random(r), random(g), random(b));
    r=constrain(r, 0, 140);
    g=constrain(g, 0, 65);
    b=constrain(b, 0, 255);
  }    
float building(float x, int y, float floorNum) {
  for (int i=0; i<floorNum; i++, y=y-aptSize) {
    a = random(3, 12);
    int b = 7;
    currentFloor= y*aptSize;
    aptRow(x, y, b);
    if (currentFloor<floorNum/3) {
      b=b-1;
    }
  }
  return a;
}    
void aptRow(float x, int y, float aptNum) { 
  for (int j=0; j<aptNum; j++, x=x+aptSize) {
    if (x<width)
      aptUnitA(x, y, aptSize);
  }
}
void aptUnitA(float x, int y, int aptSize) {
  fill(155);
  stroke(0);
  rect(x, y, aptSize, aptSize);
  noStroke();
  fill(242, 235, 53);
  ellipse(x+5, y+5, aptSize/5, aptSize/5);
  ellipse(x+15, y+5, aptSize/5, aptSize/5);
  ellipse(x+10, y+15, aptSize/5, aptSize/2);
}

这个代码是一些建筑物。我只是想弄清楚如何在每次按下一个键时绘制一组随机的新建筑物并且在建筑物的顶部有一些大小不同的公寓行?

要获得键盘输入,您可以使用keyPressed()函数或keyPressed变量。更多信息可在参考文献或本教程中获得(免责声明:我编写了该教程)。

为了使建筑物的大小不同,你可以使用random()函数获得一个随机数,然后将其作为aptRow()函数的aptNum参数传入。

我建议你先退后一步,做一些更简单的工作。你能在每次用户按下一个键时显示一个随机大小的矩形吗?

最新更新