Skip to content

Instantly share code, notes, and snippets.

@m4rk3r
Last active August 29, 2015 14:06
Show Gist options
  • Save m4rk3r/5b8ac808bd68e432d29a to your computer and use it in GitHub Desktop.
Save m4rk3r/5b8ac808bd68e432d29a to your computer and use it in GitHub Desktop.
Basic animation, variables & conditional
/* variables for pos, width & height of rectangle */
int leftPos;
int speed;
int rectW, rectH;
void setup() {
size(300, 300);
leftPos = 0;
speed = 1;
rectW = 30;
rectH = 30;
}
void draw() {
background(200, 200, 200);
fill(random(0, 255), random(0, 255), random(0, 255));
rect(leftPos, 30, rectW, rectH);
/* move our rect postion by speed every frame */
leftPos += speed;
/* ask whether it its left edge has gone past the right
edge of our screen
*/
if (leftPos > width) {
// if beyond screen, set postion to negative width of our rect
leftPos = -rectW;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment