falling balls

edited March 2018 in Questions about Code

so i need to do a project. i need to make balls fall like rain and every ball needs to have a different color. And when they tuch the ground they need to bounce off and then faid of . Can anybody help me???

Answers

  • the balls have to be big at first and when they fall get smaller

  • Sure

    Make a class Ball and an array of it (before setup ())

    See tutorial objects

    Then fill the array in setup

    Move and display array in draw

    See examples on movement and gravity

    For fade see opacity in command fill

    Then show your entire code and tell us what you have problems with

    Chrisir

  • ArrayList balls; float a;

    void setup(){ size(600,600); balls = new ArrayList(); } void draw() { background(230,230,250); balls.add(new Ball()); for (Ball balls:balls){ balls.fall(); balls.display(); } } class Ball {

     float x=random(width);
    float y=random(-200,100);
     float yspeed=random(3,4);
     float a=random(40);
    
     void fall(){
       if (y<height) {
       y+=yspeed;  
       } 
    

    } void display(){ fill(255); noStroke(); ellipse(x,y,a,a);} }

  • Edit your post

    Format your code better

  • Not bad at all!

    Now add the things I told you

  • Look at the examples...

  • how do you make it become smaller and smaller ?

  • edited March 2018

    You can have a fade or radius variable inside the class

    Now use the radius variable with ellipse() command

    Let’s make it a float variable radius = 7 : float radius = 7;

    After ellipse(...) say radius = radius - 0.3; or even smaller

    When radius <= 0 you could mark the ball as dead using a boolean variable isDead = true; inside the class

  • class Drop { float x = random(width); float y = random(-500, -50); float yspeed = random (1, 6); float radius = 7; boolean bDead; void fall() { y = y + yspeed; //yspeed = yspeed + 0.05;

    if(y > height) { y = random(-200.-100);

    } } void show() { stroke(255); ellipse( x, y, 20, 20); radius = radius - 0.05; if(radius <= 0) { bDead =true; println("dead"); } } }

  • its not making the balls smaller . why?

  • you still don't use radius in ellipse()......

This discussion has been closed.