i know what i want to do but don't know how to do it

what i want to do is each time loop of draw comes a new "action" is randomly chosen. I know i would do if random a number then the action. but I'm not sure how to write it out. also how to i write out for draw to pick a new number randomly each time. (also i marked each action with dotted lines)

 //PImage bg;
    float x;
    float r = 0.0;

void setup (){
 size(800,800);
 smooth();
 //bg = loadImage("a.jpg"); 


}

void draw () {
 //background(bg);
 background(0);
 {for (float x=1; x < 2; x=x+.05) { //------------------------------------------------
  shape();
 } 
 }

 {for (float x=1; x < 20; x=x+.5) { //----------------------------------------------------
  shape();
 } 
 }

 {for (float x=1; x < 200; x=x+.5) { //---------------------------------------------------
  shape();
 } 
 }

 {for (float x=1; x < 2000; x=x+.5) { //----------------------------------------------------
  shape();
 } 
 }

 {for (float x=1; x < 1000; x=x+.05) { //------------------------------------------------------
  shape();
 } 
 }
}




void shape (){
  translate(390,393);
 scale(r);
 for(int i = 0; i < 5; i++){
  int c = color(random(0,255), random(0,255), random(0,255));
  stroke(c);
  noFill();
  rectMode(CENTER);
  rect(0,0,10,10);
    r += 0.5;
    if (r > 55) {
    r = 0;
    }
 }
}
``

Answers

  • Answer ✓

    Here is some example code that draws one of three different shapes at random every frame.

    void setup() {
      size(200, 200);
      noStroke();
    }
    
    void draw() {
    
      background(0);
    
      int r = int(random(3));
    
      if ( r == 0 ) {
        fill(255, 0, 0);
        rect(20, 20, 160, 160);
      }
    
      if ( r == 1 ) {
        fill(0, 255, 0);
        ellipse(100, 100, 180, 180);
      }
    
      if ( r == 2 ) {
        fill(0, 0, 255);
        triangle(100, 20, 20, 180, 180, 180);
      }
    
    } 
    
Sign In or Register to comment.