Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PVector location;
- PVector velocity;
- float a = 0;
- float b = 0;
- float c = 0;
- int size = 16;
- boolean down = true;
- boolean gravity = true;
- boolean symmetrical = false;
- void setup() {
- size(600,600);
- smooth();
- background(255);
- location = new PVector(350,300);
- velocity = new PVector(2.5,0);
- }
- void draw() {
- noStroke();
- fill(255,10);
- rect(0,0,width,height);
- location.add(velocity);//moves the ball
- if(gravity)//if gravity is true or "on"...
- {
- if(down == true)//...and down is true...
- {
- velocity.add(0,.2);//...gravity pulls ball down
- }
- if(down == false)//...and down is false
- {
- velocity.add(0,-.2);//...gravity pulls ball up
- }
- }
- if (((location.y > height) || (location.y < 0)) && (down == true)) {
- velocity.y = (velocity.y - .4) * -1;//makes ball lose some momentum when it bounces off of the floor or ceiling
- a = random(0,255);//sets a b and c variables to random floats between 0 and 255
- b = random(0,255);
- c = random(0,255);
- }
- if(((location.y > height) || (location.y < 0)) && (down == false))
- {
- velocity.y = (velocity.y + .4) * -1;//makes ball lose some momentum when it bounces off of the floor or ceiling
- a = random(0,255);//sets a b and c variables to random floats between 0 and 255
- b = random(0,255);
- c = random(0,255);
- }
- if(symmetrical == false)//if symmetrical is false...
- {
- if ((location.x > width) || (location.x < 0)) {//...then the ball can move across the entire width of the rectangle
- velocity.x = velocity.x * -1;//makes ball bounce off of walls
- a = random(0,255);
- b = random(0,255);
- c = random(0,255);
- }
- stroke(0);
- fill(a,b,c);
- ellipse(location.x,location.y,size,size);//shows the path of the object
- }
- if(symmetrical == true)//if symmetrical is true...
- {
- if ((location.x < (width/2 + size/2)) || (location.x > width)) {//...then makes ball bounce off of the middle line
- velocity.x = velocity.x * -1;
- a = random(0,255);
- b = random(0,255);
- c = random(0,255);
- }
- stroke(0);
- fill(a,b,c); // makes ball the color of random floats
- ellipse(location.x,location.y,size,size);//shows the path of the object
- scale(-1,1);
- ellipse(location.x-600,location.y,size,size);//shows a second object (that isn't there) on the other side of the line of symmetry, because i wanted to do it with only one PVector
- }
- }
- void keyPressed()
- {
- if(key == 's' || key == 'S')//s for size
- {
- size = (int)random(2,30);//randomly changes the size of the ball
- }
- if(key == 'd' || key == 'D')//d for down
- {
- down = !down;//reverses gravity(goes up or down)
- }
- if(key == 'a' || key == 'A')//a for acceleration (sometimes, the ball goes out of bounds)
- {
- velocity.y = 0;//stops the ball from falling momentarily
- }
- if(key == 'g'|| key == 'G')//g for gravity
- {
- gravity = !gravity;//turns off gravity
- }
- }
- void mouseClicked()
- {
- setup();
- symmetrical = !symmetrical;//makes the symmetrical version run
- }
Add Comment
Please, Sign In to add comment