Stop in a for loop
in
Programming Questions
•
9 months ago
Short explanation:
every time when you move the slider a bit up one block will be added to the pyramid. So it starts really small and gets bigger and bigger. What is important though is that the pyramid will be created from the middle. So first the top, then three beneath, starting with the block in the middle of three, then 5 beneath, starting with the one in the middle, then left, then right, then the most left one and then the most right one, and on and on.
I came this far but can't stop the loop. I was trying to stop the loop when it got a certain amount but it won't work unfortunately :(. Can someone help me out? I can't seem to find the solution..
- import controlP5.*;
- int xPos = 400;
- int yPos = 400;
- int rectSize= 10;
- int count = 1;
- int countY = 1;
- int countP = 1;
- ControlP5 controlP5;
- int jaar = 30;
- void setup() {
- background(100);
- size(800, 800);
- noStroke();
- fill(200, 50);
- controlP5 = new ControlP5(this);
- // add horizontal sliders
- controlP5.addSlider("jaar", 0, 10, 0, 10, 380, 360, 10);
- Slider s2 = (Slider)controlP5.controller("jaar");
- s2.setNumberOfTickMarks(12);
- s2.setSliderMode(Slider.FLEXIBLE);
- }
- void draw() {
- background(255);
- color[] colorOptions = new color[5];
- colorOptions[0] = #FF9164;
- colorOptions[1] = #A84820;
- colorOptions[2] = #F47A47;
- colorOptions[3] = #0FA891;
- colorOptions[4] = #47F4DB;
- int xPos = 400;
- int yPos = 400;
- int rectSize= 10;
- int count = 1;
- int countY = 1;
- int countP = 1;
- int j= 0;
- //rows van de piramide
- for (j =0; j < jaar; j++) {
- rect(xPos, yPos, rectSize, rectSize);
- j++;
- count = 1;
- countY = 1;
- for (int i = 1; i < countP; i++) {
- int kleur = int(random(5));
- fill(colorOptions[kleur]);
- xPos = xPos - rectSize*count;
- rect(xPos, yPos, rectSize, rectSize);
- j++;
- count++;
- xPos = xPos + rectSize*count;
- rect(xPos, yPos, rectSize, rectSize);
- j++;
- count++;
- countY++;
- println(j);
- }
- countP++;
- yPos = yPos + 10;
- xPos = xPos - rectSize*countY + 10;
- }
- }
- void slider(float theColor) {
- fill(100);
- rect(100, 100, jaar, jaar);
- }
1