Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Bouncing Balls (plural)
- * @Author: Allen Thoe
- */
- //DECLARE VARIABLES
- var x, y, Vx, Vy;
- var x2, y2, Vx2, Vy2;
- var ballW, ballH; //Height and width of ball
- var r, g, b, r2, g2, b2; //Colors for ball
- function setup() {
- createCanvas(400, 400);
- ballH = 20;
- ballW = 20;
- x = 220;
- y = 50;
- r = 255;
- g = 255;
- b = 255;
- r2 = 255;
- g2 = 255;
- b2 = 255;
- x2 = 50;
- y2 = 10;
- Vx2 = random(5);
- Vy2 = random(2,5);
- Vx = random(5); //(between 0 and 5)
- Vy = random(2,5); //(start, stop)
- }
- function draw() {
- background(80);
- drawBalls();
- checkCollide(); //ABSTRACT FROM Function
- drawPaddle();
- }
- function drawPaddle(){
- strokeWeight(4);
- fill(255);
- rect(mouseX - 50, height - 30, 100, 20, 4);
- strokeWeight(1);
- }
- function checkCollide(){
- //Collides with walls
- if(x > width || x < 0){
- Vx *= -1;
- r = random(255);
- g = random(255);
- b = random(255);
- } // || Means 'OR', && means 'AND'
- if(y > height || y < 0){
- Vy *= -1;
- r = random(255);
- g = random(255);
- b = random(255);
- }
- if(x2 > width || x2 < 0){
- Vx2 *= -1;
- r2 = random(255);
- g2 = random(255);
- b2 = random(255);
- } // || Means 'OR', && means 'AND'
- if(y2 > height || y2 < 0){
- Vy2 *= -1;
- r2 = random(255);
- g2 = random(255);
- b2 = random(255);
- }
- //Collide with each other
- if(abs(x - x2) < ballW && abs(y - y2) < ballH){
- Vx *= -1;
- Vy *= -1;
- Vx2 *= -1;
- Vy2 *= -1;
- }
- }
- function drawBalls(){
- fill(r,g,b);
- ellipse(x, y, ballW, ballH);
- fill(r2, g2, b2);
- ellipse(x2, y2, ballW, ballH);
- //UPDATE POSITION
- x += Vx;
- y += Vy;
- x2 += Vx2;
- y2 += Vy2;
- }
Advertisement
Add Comment
Please, Sign In to add comment