//Declare variables (put at the beginning outside of any command) // keep track of the location of rectangles int xpos = 0; // horizontal (x) position left square int ypos = 1; // vertical (y)position left square int xpos2 = 1024; int ypos2 = 8; int w = 20; //square width int h = 8; // square height int speed = 1; //speed of square movement per pixel boolean moveit_right = true; boolean moveit_left = true; // Code that runs forever goes here -set up the applet void setup() { size(1024,17); // applet size framerate(30); // speed of animation } void draw(){ background(150); // bg color // drawing the squares noStroke(); fill(#66ff33); //square on the left rect(xpos, ypos, w, h); smooth (); noFill(); noStroke(); fill(#66ff33); //square on the right rect(xpos2, ypos2, w, h); smooth (); noFill(); xpos = xpos + speed; //left square moving right if (moveit_right == true) { xpos = xpos + 5; } else { // else going left subtract 5 xpos = xpos - 5; } // if on right edge start going left if (xpos > width) { moveit_right = false; } // if on left edge start going right else if (xpos < 0) { moveit_right = true; } xpos2 = xpos2 - speed; // right square moving left if (moveit_left == true) { xpos2 = xpos2 + 5; } else { // else going left subtract 5 xpos2 = xpos2 - 5; } // if on right edge start going left if (xpos2 > width) { moveit_left = false; } // if on left edge start going right else if (xpos2 < 0) { moveit_left = true; } }