Bouncing balls C++

This is a discussion on Bouncing balls C++ within the C++ Programming forums, part of the General Programming Boards category; Hey guys need a little help I've created a console app that when run it creates a ball that bounces ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Smile 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 display() {fillellipse(cX, cY, radius, radius);
      	  }
    
    
      	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;
      }
    Last edited by Salem; 11-15-2010 at 02:17 PM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    18
    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).

    With your method:
    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.
    Last edited by syneii; 11-15-2010 at 03:31 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    added the code but im getting the error "draw was not declared in the scope"

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    18
    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.
    Last edited by syneii; 11-15-2010 at 03:40 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    haha okay, sturggling but ill keep trying

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a bouncing ball?
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2008, 04:41 AM
  2. Bouncing off surfaces
    By valaris in forum Game Programming
    Replies: 4
    Last Post: 09-02-2008, 08:10 PM
  3. looking for advice on selling golf balls
    By lambs4 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-30-2004, 05:03 PM
  4. bouncing bullets
    By dydoko in forum Game Programming
    Replies: 8
    Last Post: 09-18-2003, 09:31 PM
  5. Bouncing ball - help ??
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 04-13-2002, 03:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21