Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //button to star movement of orange circle
- boolean button = false;
- //variable ellipse values
- int circleX = 0;
- int circleY = 100;
- //color variables
- float c1 = 0;
- float c2 = 255;
- //varibles too incrementally change the color of the window
- float c1Change = 1;
- float c2Change = - 1;
- float x = 100; //x location of square
- float y = 0; //y location of square
- float speed = 0; // speed of square
- float gravity = 0.1; // the acceleration of the ball based on its location
- void setup() {
- size(200,200);
- }
- void draw() {
- //draw rectangle on left
- fill(c2, 0, c1);
- rect(0, 0, 100, 200);
- //draw rectangle on right
- fill(c2, 0, c1);
- rect(100, 0, 100, 200);
- //adjust color values
- c1 = c1 + c1Change;//c1 is changing at an increment of 1
- c2 = c2 + c2Change;// is changeing by a negetive amount of 1
- // so that once the color gets to blue in reverse back to red and then recycles
- if (c1 < 0 || c1 > 255) {
- c1Change *= -1;
- }
- if (c2 < 0 || c2 > 255) {
- c2Change *= -1;
- }
- fill(255, 150, 0);
- ellipse(circleX, circleY, 50, 50);
- // draw the ball
- fill(255,150,0);
- noStroke();
- ellipse(x, y, 10, 10);
- y = y + speed;
- speed = speed + gravity; // add speed to location add gravity to speed, becuase then the speed wont get to fast and is relative to location
- //bounce back up!
- if (y > height) {
- speed = speed * -0.95; // -0.95 becuase it doesnt reverse the speed it "dampens" it or slows it down incrementally
- y = height; // this is neceressary so that when draw loops this is at the end making sure the ball comes back down to "the ground" instead of leaving the window.
- }
- if (button) {
- circleX++;
- }
- if (circleX > width) {
- circleX = 0;
- }
- }
- void mousePressed() {
- button = true;
- }
- // some question: How to make a random X variable so that the ball bounces in all directions?
Add Comment
Please, Sign In to add comment