You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Processing生成式世界开发:固定数量随机对象绘制问题求助

Processing 指定数量随机对象绘制解决方案

问题

我正在用Processing开发生成式世界,需要创建4个具备随机位置、大小、颜色的对象,要求每次点击鼠标时重新生成这4个对象的随机属性。已经实现了对象绘制方法,但当前代码会持续生成大量对象,求解决指定数量对象绘制的方案。

原代码

final int NUM_OBJECTS = 4;     
int numObjectsLeft;
float x;
float y;
float radius;

void renderBackground() {
  //sky
  fill(135, 206, 235);
  rect(0, 0, width, height);
  //grass
  fill(86, 125, 70);
  rect(0, 500, 1400, 750);
  //Pathway
  fill(181, 101, 29);
  rect(0, 600, 1400, 100);
  //Buildings
  fill(128, 128, 128);
  rect(900, 300, 100, 200);
  rect(1000, 350, 80, 150);
  fill(160, 220, 222);
  rect(915, 315, 30, 30);
  rect(955, 315, 30, 30);
  rect(915, 355, 30, 30);
  rect(955, 355, 30, 30);
  rect(915, 395, 30, 30);
  rect(955, 395, 30, 30);
  rect(915, 435, 30, 30);
  rect(955, 435, 30, 30);
  rect(1010, 360, 25, 25);
  rect(1045, 360, 25, 25);
  rect(1010, 395, 25, 25);
  rect(1045, 395, 25, 25);
  rect(1010, 430, 25, 25);
  rect(1045, 430, 25, 25);
  //Trees
  fill(102, 51, 0);
  rect(275, 430, 50, 100);
  fill(97, 138, 61);
  ellipse(300, 400, 100, 100);
  fill(102, 51, 0);
  rect(380, 450, 40, 75);
  fill(97, 138, 61);
  ellipse(400, 425, 80, 80);
  //Hills
  fill(122, 115, 114);
  triangle(100, 500, 200, 500, 150, 300);
  triangle(175, 500, 225, 500, 200, 400);
}

void renderObject(float x, float y, float radius, color objColor) {
  fill(objColor);
  line(x, y+(radius/2), x, y+radius+30);
  line(x, y+radius-5, x+(radius/2), y+radius+25);
  line(x, y+radius-5, x-(radius/2), y+radius+25);
  line(x, y+radius+30, x+(radius/2), y+radius*2+25);
  line(x, y+radius+30, x-(radius/2), y+radius*2+25);
  ellipse(x, y, radius, radius);
}

void renderForeground() {
  numObjectsLeft = NUM_OBJECTS;
  x = random(0, 1400);
  y = random(500, 700);
  radius = 30;
  numObjectsLeft--;
  
  renderObject(x,y,radius,numObjectsLeft--);
  
  if (numObjectsLeft > 0) {
  
    renderObject(x,y,radius,numObjectsLeft);
  
    if(numObjectsLeft <= 0) { 
      exit();
          
    }
  
}
  
}

void setup() {
  size(1400, 750);
  renderBackground();
  numObjectsLeft = NUM_OBJECTS;
}

void draw() {
  x = random(0, 1400);
  y = random(500, 700);
  radius = 30;
  renderForeground();
}

void mousePressed() {
}

解决方案

核心问题是原代码在draw()循环中不断调用生成逻辑,且未用数据结构存储对象属性,导致持续生成新对象。修改思路:

  • 用数组存储每个对象的位置、大小、颜色属性
  • 只在setup()初始化和mousePressed()点击时生成4个对象的随机属性
  • draw()函数仅负责绘制背景和已存储的4个对象,避免重复生成

修改后的代码

final int NUM_OBJECTS = 4;
// 存储每个对象的属性:x坐标、y坐标、半径、颜色
float[] objX = new float[NUM_OBJECTS];
float[] objY = new float[NUM_OBJECTS];
float[] objRadius = new float[NUM_OBJECTS];
color[] objColor = new color[NUM_OBJECTS];

void renderBackground() {
  //sky
  fill(135, 206, 235);
  rect(0, 0, width, height);
  //grass
  fill(86, 125, 70);
  rect(0, 500, 1400, 750);
  //Pathway
  fill(181, 101, 29);
  rect(0, 600, 1400, 100);
  //Buildings
  fill(128, 128, 128);
  rect(900, 300, 100, 200);
  rect(1000, 350, 80, 150);
  fill(160, 220, 222);
  rect(915, 315, 30, 30);
  rect(955, 315, 30, 30);
  rect(915, 355, 30, 30);
  rect(955, 355, 30, 30);
  rect(915, 395, 30, 30);
  rect(955, 395, 30, 30);
  rect(915, 435, 30, 30);
  rect(955, 435, 30, 30);
  rect(1010, 360, 25, 25);
  rect(1045, 360, 25, 25);
  rect(1010, 395, 25, 25);
  rect(1045, 395, 25, 25);
  rect(1010, 430, 25, 25);
  rect(1045, 430, 25, 25);
  //Trees
  fill(102, 51, 0);
  rect(275, 430, 50, 100);
  fill(97, 138, 61);
  ellipse(300, 400, 100, 100);
  fill(102, 51, 0);
  rect(380, 450, 40, 75);
  fill(97, 138, 61);
  ellipse(400, 425, 80, 80);
  //Hills
  fill(122, 115, 114);
  triangle(100, 500, 200, 500, 150, 300);
  triangle(175, 500, 225, 500, 200, 400);
}

void renderObject(float x, float y, float radius, color objColor) {
  fill(objColor);
  line(x, y+(radius/2), x, y+radius+30);
  line(x, y+radius-5, x+(radius/2), y+radius+25);
  line(x, y+radius-5, x-(radius/2), y+radius+25);
  line(x, y+radius+30, x+(radius/2), y+radius*2+25);
  line(x, y+radius+30, x-(radius/2), y+radius*2+25);
  ellipse(x, y, radius, radius);
}

// 生成4个对象的随机属性
void generateObjects() {
  for (int i = 0; i < NUM_OBJECTS; i++) {
    objX[i] = random(0, 1400);
    objY[i] = random(500, 700);
    objRadius[i] = random(20, 40); // 随机大小,范围20-40
    objColor[i] = color(random(255), random(255), random(255)); // 随机颜色
  }
}

void setup() {
  size(1400, 750);
  generateObjects(); // 初始化对象属性
}

void draw() {
  renderBackground(); // 每次绘制都先画背景,覆盖之前的对象
  // 循环绘制4个对象
  for (int i = 0; i < NUM_OBJECTS; i++) {
    renderObject(objX[i], objY[i], objRadius[i], objColor[i]);
  }
}

void mousePressed() {
  generateObjects(); // 点击鼠标时重新生成对象属性
}

关键改动说明

  • 新增数组存储每个对象的x、y、radius、color属性,确保每次只维护4个对象的数据
  • 新增generateObjects()函数,专门负责生成4个对象的随机属性,在setup()初始化和mousePressed()点击时调用
  • 修改draw()函数,每次先绘制背景(覆盖之前的对象),再循环绘制存储好的4个对象,避免叠加生成
  • 给对象添加了随机大小和颜色,满足需求中的随机属性要求

内容的提问来源于stack exchange,提问作者intEOF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 22:25:25