1. ## Bouncing balls C++

Hey guys need a little help

I've created a console app that when run it creates a ball that bounces from wall to wall like a screen saver.
Each time it run it randomly generates a different coloured and different sized ball but i now want to add more than one ball to the screen. (8 balls)

How can i do this?

I've placed my current code bellow

Any help will be greatly appreciated

Kind Regards

Darkowl

-------------------------------------------------------------------------

Code:
```#include <graphics.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int random(int min, int max){
return (rand()%max + min);
}

int cX,cY, ySpeed, xSpeed, radius, colour;

}

void move() {
cX += xSpeed;   cY += ySpeed;
if (cX > 400 || cX < 0) {  xSpeed *= -1;  cX += xSpeed;   }
if (cY > 300 || cY < 0) {  ySpeed *= -1;  cY += ySpeed;   }
}

int main( ) {

cout<< "Max int: " << RAND_MAX;
srand(time(0));
for (int k = 0; k < 2; k++){
cout << random(0, 21)<< endl;
}

initwindow(400, 300, "First Sample");

radius =  xSpeed =  ySpeed = random(10,25); cX = 200; cY = 150;
colour = random(1,10);  setfillstyle(1, colour);
display();

while (!kbhit( )) {
delay(75);
cleardevice();
move();
display();
}
return 0;
}```

2. A bit off subject from your question, but I couldn't help but notice that your random function doesn't do what you seem to want it to do (judging by the parameter names).

min = 3
max = 5
rand = (24 % max) = 4;
4 + min = 7 <-- See this? Well just fyi but 7 is greater than max.

Here is a pseudo random function which results in the range of [min, max].
Code:
```int random(int min, int max)
{
return (min + (rand()%(1+max-min)));
}```
Also, you should be using code tags.

The answer to your question is to create an array of numbers, each set of elements tracking it's own 'ball'.
So maybe you want to create a class which defines properties of a 'ball'.

class Ball { public: int x, int y; }

Then create an array of balls; Ball balls[8].

Now you have balls[0].x and balls[0].y, ... up to balls[6].

Just FYI, when I say balls so many times in a row the word losses meaning, now it's just awkward saying or thinking about the word and sound 'ball'. Balls ball balls balls ball ... balls!

... when ever you're ready you can spam your balls all over the screen like so;
Code:
```for (int j = 0; j < numberOfBalls; ++j)
{
// Draw is a made-up method, do what ever you have to do to draw in your app.
Draw(balls[j].x, balls[j].y);
}```
I feel dirty now.

3. added the code but im getting the error "draw was not declared in the scope"

4. Everything I posted is pseudo, it's not intended to be used as I wrote it. My words are a giant arrow giving a direction, you work your way down that path. ;p

Draw(x,y) means ... do what ever you have to do to draw the ball to the screen.

You are drawing things to the screen right? So draw the ball to the screen, the ball at that array index.

As for the Ball class, don't use it as I wrote it, I left out tons of stuff you need. e.g.; keeping track of each ball's acceleration, so add properties in the class for acceleration int's. You should take the concepts I laid out, and make it work for you.

Think about it intuitively, like this. "Hmm, I need each ball to have its own acceleration, oh yea I can create an integer type in the Ball class!"

class Ball { public: int x, y, ax, ay; }

"Hmm I need my balls to hold taco's too."

class Ball { public: int x, y, ax, ay; Taco mytaco; }

It's up to you man! Just do what you gotta do to make your description of your app make sense.

5. haha okay, sturggling but ill keep trying