How can I draw ellipses moving horizontally continuously?

edited January 2015 in How To...

I'm trying to make a game and I don't know hot to put asteroids like the ones that are in this video.

I need help to draw the asteroids I and have no idea. I've done everything but the asteroids.

Thanks!

Answers

  • Answer ✓

    here

    //
    
    ArrayList<BouncingBall> ballList;  // initially null, BallList is a name (can be any name)
    int NUM_BALLS = 10;
    
    void setup() {
      size(500, 500);
    
      // create the initially empty ArrayList of BouncingBall objects
      ballList = new ArrayList<BouncingBall>(); //ballList is a name ( can be any name)
    
      // add some BouncingBall objects
      int i = 0;
      while (i < NUM_BALLS) {
        ballList.add(randomBouncingBall());
        i += 1;
      }
      //
    }
    
    void draw() {
      background(255);
    
      // render and update all the balls
      for (BouncingBall b : ballList) {
        b.render();
        b.update();
      }
    }
    
    // ------------------------------------------
    // other functions 
    
    BouncingBall randomBouncingBall() {
    
      // returns a ball object 
    
      return new BouncingBall(random(5, width), random(0.0, height), 
      valueAroundZero(), 0, 
      random(25, 76), 
      color(random(0, 256), random(0, 256), random(0, 256))
        );
    }
    
    float valueAroundZero() {
    
      // gives a random number which is > or < 0 but not 0
    
      float buffer = random(0.9, 2.0);
    
      if (random(100) < 50) 
        buffer = buffer * -1; 
    
      return buffer;
    }
    
    
    //====================================
    
    
    class BouncingBall {
      float x, y;        // (x, y) is the center of the ball
      float dx, dy;      // speed 
      float diameter;    // size 
      color c;           // color 
    
      //constructor shorter code to intialize the variables
      BouncingBall(float init_x, float init_y, 
      float init_dx, float init_dy, 
      float init_diameter, color init_c) {
        x = init_x;
        y = init_y;
        dx = init_dx;
        dy = init_dy;
        c = init_c;
        diameter = init_diameter;
      }
    
      void render() {
        fill(c);
        noStroke();
        ellipse(x, y, diameter, diameter);
      }
    
      void update() { //update the position of the ball
        // move the ball to its next position
        x += dx;
        y += dy;
        //
        // hit the edge?
        // restart 
        if (x > width+11) {
          //y = random(-10, -100);
          //x = random(5, width);
          dx= -1 * abs(dx);
        }
        else if (x < -11) {
          //y = random(-10, -100);
          //x = random(5, width);
          dx= abs(dx);
        }
        //
      }
      //
    } // class
    
    // ===========================================
    
  • edited January 2015

    Thanks Chrisir, it works and I got what I wanted even though I don't understand this part:

     // other functions 
    
    BouncingBall randomBouncingBall() {
    
      // returns a ball object 
    
      return new BouncingBall(random(5, width), random(0.0, height), 
      valueAroundZero(), 0, 
      random(25, 76), 
      color(random(0, 256), random(0, 256), random(0, 256))
        );
    }
    
    float valueAroundZero() {
    
      // gives a random number which is > or < 0 but not 0
    
      float buffer = random(0.9, 2.0);
    
      if (random(100) < 50) 
        buffer = buffer * -1; 
    
      return buffer;
    }
    

    Can you explain it, please? Thanks

  • edited January 2015 Answer ✓

    Hello,

    ok, that's simple.

    When you have a simple function, it doesn't return anything (or void)

    void sayYouHaveStarted() {
          println ("I've started now.");   // do something 
    }
    

    Now we are working with little more complex function that can return something:

    int AddIt (int a, int b) {
    
       // do something and return the result (using return) of type int 
       return a + b;   
    
    } 
    

    It's like a small factory:

    • something goes in (or nothing),

    • you work with it, produce something and

    • something goes out of it (or nothing, void).

    The 2 functions

    The 2 functions you're asking for return an object of type / class BouncingBall (1st function) and a number of type float (2nd function). What goes into these 2 functions as their parameters? Nothing: you see empty brackets () : float valueAroundZero() { .

    The 2nd function

    I start with the simpler one...

    The 2nd function

    • returns a float value to be the speed of each ball.

    • The value is either between 0.9 and 2.0 or between - 0.9 and - 2.0.

    • My goal was to give each ball a speed that is either positive or negative (flying left or right).

    • But also I wanted it to be not 0 or close to zero, the balls are too slow then (as you see often in this forum).

    • My solution: instead of random(-2,2) what is the normal approach, I used this little function, which delivers you -2 to 2 but without the range -0.9 to 0.9 in it (where the balls would be too slow...) ;-)

      float valueAroundZero() {

      // gives a random number which is pos or negative but not 0 or close to 0. // The value is either between 0.9 and 2.0 or between - 0.9 and - 2.0.

      // buffer is the product we are working on and want to return in the end

      // get a positive value in the range: float buffer = random(0.9, 2.0);

      // in 50 % of the cases (if a random value from 0 to 100 is smaller 50) if (random(100) < 50) buffer = buffer * -1; // make it negative (same range, but negative)

      return buffer; // factory return this result } // end of function

    The 1st function

    The 1st function is used in the spot where we fill our ArrayList with balls:

    // add some BouncingBall objects int i = 0; while (i < NUM_BALLS) { ballList.add(randomBouncingBall()); // fill one object to the list i += 1; } //

    this is the central line of code:

            ballList.add(randomBouncingBall());
    

    you could also say

        color myCoolRandomColor = 
            color(random(0, 256), random(0, 256), random(0, 256)); 
    
        BouncingBall tempBall  = 
             new BouncingBall(random(5, width), random(0.0, height),
              valueAroundZero(), 0,
              random(25, 76),
              myCoolRandomColor);
    
        ballList.add(tempBall);
    

    but I felt that looked weird so I decided to make a small function.

    In the function also an object from the class is instantiated but then returned by the function (as a factory product) to the spot where the function is called from. Here the functions result is an object and is used to add this object to our ArrayList of all balls. Nice.

    The function looks a bit complicate but it's only because we hammer the insane long list of parameters into the constructor of the class. So that each ball has another position, speed, color and size...

    Also in the class we make the object and return it in one line... that is not very nice for the untrained eye; instead you could also say

    BouncingBall randomBouncingBall() {
    
       // returns a ball object 
    
        color myCoolRandomColor = 
            color(random(0, 256), random(0, 256), random(0, 256)); 
    
        BouncingBall tempBall  = 
             new BouncingBall(random(5, width), random(0.0, height),
              valueAroundZero(), 0,
              random(25, 76),
              myCoolRandomColor);
    
         return  tempBall;
    
         } 
    

    You might wanna read http://www.processing.org/tutorials/objects for OOP in general which I am not going to explain here (cookies and cookie maker and all that).

    Please ask again when I didn't answer your questions....

    Best, Chrisir ;-)

  • Thank you so much Chrisir!

  • you're welcome!

  • Answer ✓

    it's pretty much as you say:

    here

    for (BouncingBall b : ballList) {
        b.render();
        b.update();
      }
    

    enter a If clause and check for your shipX and shipY:

    if (b.x-3 < shipX && b.x+3 > shipX && 
    b.y-3 < shipY && b.y+3 > shipY) {
     // collision 
    }
    

    ;-)

  • Where should this be?

  • in draw()

    see above

    line 25

Sign In or Register to comment.