Home > Back-end >  How to draw a circle and then when keyPressed, draw another circle etc
How to draw a circle and then when keyPressed, draw another circle etc

Time:08-12

I am learning how to code in Processing 4.

I am trying to make a code where when keyPressed() is clicked, one circle is drawn then when the button is pressed again, the first circle remains and then a second circle is drawn but with extent increased by 10, then the key pressed for a final time (3) and the third circle is drawn with the extent increased by 3 then it stops producing more circles.

This is the code I have so far but I am confused on how to approach it. Right now, the first circle just increases in size and it doesn't draw a second one. How do I draw 3 circles, one once keyPressed()?

int sizeIncrease = 10;
int initialSize = 70;

void setup() {
  size(500, 500);
}

void draw() {
  background(0);

  stroke(255,0,0);
  strokeWeight(25);
  fill(255);
  pushMatrix();
  translate(20, 50);
  circle(width/2, height/2, initialSize);
  popMatrix();
}

void keyPressed() {
  initialSize  = sizeIncrease;
}

CodePudding user response:

Add a variable count. Increment the variable when a key is pressed and draw the circles in a for-loop:

int sizeIncrease = 10;
int initialSize = 70;
int count = 1;

void setup() {
  size(500, 500);
}

void draw() {
  background(0);
  noFill();
  stroke(255);
  pushMatrix();
  translate(20, 50);
  for (int i = 0; i < count; i  ) {
    circle(width/2, height/2, initialSize   i*sizeIncrease);
  }
  popMatrix();
}

void keyPressed() {
  count   ;
}

CodePudding user response:

int sizeIncrease = 10;
int initialSize = 70;
int count = 1;

void setup() {
  size(500, 500);
}

void draw() {
  background(0);


  noFill();
  stroke(255);
  pushMatrix();
  translate(20, 50);
  for (int i = 0; i < count; i  ) 
  {
      circle(width/2, height/2, initialSize   i*sizeIncrease);
      **if(i == 1)
      {
           sizeIncrease = 3;
      }**
  }
  popMatrix();
}

void keyPressed() {
  count   ;
}
  • Related