Last active
September 8, 2019 09:02
-
-
Save Hologos/bedb05c2cf07b546e36868adbac70293 to your computer and use it in GitHub Desktop.
Processing gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// size of dvd box | |
int dvdW = 80; | |
int dvdH = 50; | |
// positive value = moving towards right | |
// negative value = moving towards left | |
int xspeed = 1; | |
// coordinates of dvd box | |
// remember the x coordinate is the leftmost side | |
int x; | |
// remember the y coordinate is the topmost side | |
int y; | |
void setup() { | |
size(400, 300); | |
// more about random function at https://processing.org/reference/random_.html | |
// we generate random x coordinate between 0 and the width of the screen minus the width of the dvd box (so it doesn't start outside of the screen) | |
// random() function returns float so we convert it back to integer with int() function | |
x = int(random(0, width - dvdW)); | |
y = int(random(0, height - dvdH)); | |
} | |
void draw() { | |
background(0); | |
fill(255); | |
noStroke(); | |
rect(x, y, dvdW, dvdH); | |
// if x coordinate is equal to or higher than the width of the screen minus the width of the dvd box, | |
// it means the right side of the dvd box is touching the right side of the screen | |
// we change the xspeed to negative value (as specified above, negative value means moving towards left) | |
// | |
// the same goes when x is less or equal to zero, it means the dvd box is touching the left side of the screen | |
// we change the xspeed to positive value (as specified above, positive value means moving towards right) | |
if(x >= width - dvdW) { | |
xspeed = -1; | |
} else if (x <= 0) { | |
xspeed = 1; | |
} | |
x = x + xspeed; | |
} | |
// exercises: | |
// | |
// 1) Implement the same behaviour for y coordinate so the box moves along y axis and bounces of top and bottom sides as well. | |
// | |
// 2) Change color of the box each time it bounces from ANY side (using functions: fill(), random()). | |
// | |
// 3) If the box hits ANY corner, stop rendering (using function: noLoop()). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment