I made a checker pattern with a while loop, and I want one set of loops to move 6px to the right and then back. I also have a second loop that I want to move 6px to the left and then back. I keep trying different things, but it keeps leaving a gap. The code I have so far is just the checker pattern, any help would be appreciated.
- float g=0;
- int g2=0;
- int g3=0;
- int g4=0;
- float h=0;
- int h2=0;
- int h3=0;
- int h4=0;
- color c1=(#000000);
- color c2=color(170,39,126);
- color c3=color(27,136,132);
- float n=0;
- void setup()
- {
- size(800,800);
- for(int i=0;i<height; i++)
- {
- color c=lerpColor(c1,c2,n);
- stroke(c);
- line(0,i,width,i);
- n+=0.001;
- }
- }
- void draw()
- {
- while(g<375){
- checkers(g,h);
- checkers(g-50,h+50);
- checkers(g-100,h+100);
- checkers(g-150,h+150);
- checkers(g-200,h+200);
- checkers(g-250,h+250);
- checkers(g-300,h+300);
- checkers(g-350,h+350);
- g=g+50;
- }
- while(g2<350){
- checkers(g2-25,h2+25);
- checkers(g2-75,h2+75);
- checkers(g2-125,h2+125);
- checkers(g2-175,h2+175);
- checkers(g2-225,h2+225);
- checkers(g2-275,h2+275);
- checkers(g2-325,h2+325);
- g2=g2+50;
- }
- while(g3<850){
- checkers(g3+400,h3+775);
- checkers(g3+450,h3+725);
- checkers(g3+500,h3+675);
- checkers(g3+550,h3+625);
- checkers(g3+600,h3+575);
- checkers(g3+650,h3+525);
- checkers(g3+700,h3+475);
- checkers(g3+750,h3+425);
- g3=g3+50;
- }
- while(g4<850){
- checkers(g4+475,h4+750);
- checkers(g4+525,h4+700);
- checkers(g4+575,h4+650);
- checkers(g4+625,h4+600);
- checkers(g4+675,h4+550);
- checkers(g4+725,h4+500);
- checkers(g4+775,h4+450);
- g4=g4+50;
- }
- }
- void checkers(float g, float h)
- {
- noStroke();
- fill(0);
- rect(0+g,0+h,25,25);
- fill(255);
- rect(25+g,0+h,25,25);
- }
Answers
I see two chess boards cut in half
You wrote:
Do you want to move the entire chess board? Or just one line / one pair?
And when do you want this to happen, when you press the mouse?
a general chess board is done like this. Your way is much to complicate
Your way is still too complicated 8)
Square colour can be calculated on the sum of x and y...
So essentially I want to set an idle animation that will move g and g3 to the right 6px and then move it back. Also idle, moving g2 and g4 to the left and move back. I pretty much want to do this.
so do you want the change instantaneously or animated from 0 to 6 ?
just add offsetX where appropriate
set the offsetX to 6. You can use function
void mousePressed()
and sayto toggle between two situations
I guess animated, and I would like to have it do this without me having to interact (clicking, keys, ect.).
Would offsetX be a float?
yes
I am also a complete novice at code, and I have no clue where to place this to make it work. I also appreciate you working with me on this.
you need to add offsetX to the lines in the grid you want to move
So if had this from the previous code
Would I add the whole thing, part of it before the while, after the while, or after the whole thing. Sorry, I tried several ways, and it doesn't move.
It can really help conceptually do understand that you are trying to do a single bouncing ball, learn how to do that, then apply it to a bunch of objects so that they all bounce in the same way at the same time.
I agree with jeremy.
also, in this section you want
checkers(g+offsetX,h);