Access Between Classes

Hello! I'm building a game using SDL and I'm having some issues with my class design. I've got two classes which need to talk to one another, and I'm unsure the best way to go about this.

To start with, I need the ability to increase the balls velocity on the event that 5 bricks have been destroyed. The problem is, I can't modify the balls properties in the Brick class because the Ball class isn't defined yet in the code. And I can't move the Ball class before the Brick class because it depends on the Brick class...

So in short.. OOP nightmare. Someone show me the light!

Brick Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Brick
{
 int width, height;
 int x, y; 
 int type;
 SDL_Rect box;
 bool visible; 
 public:
     Brick(int xPos, int yPos, int brickType) 
     { width = brickWidth; height = brickHeight; 
       x = xPos; y = yPos; 
       type = brickType; visible = true; 
       box.x = x; box.y = y;
       box.w = width; box.h = height; }
   int getX(){return box.x;}
   int getY(){return box.y;}
   int getWidth(){return box.w;}
   int getHeight(){return box.h;}
   void show() { 
    if(visible) {
       switch(type) {
        case 1: 
          applySurface(x,y,brick1,screen); break;
        case 2:
          applySurface(x,y,brick2,screen); break;
        case 3:
          applySurface(x,y,brick3,screen); break;
        case 4:
          applySurface(x,y,brick4,screen); break;
        }
    }
   }
   void destroyBrick() { 
     visible = false; 
     //This should move the SDL_Rect for collision outside of the normal
     // bricks region. Which should let the ball pass through the bricks
     // after one disappears. Though.. this is messy. 
     box.x = 0; box.y = 0;
     box.w = 0; box.h = 0;
     //Adjusts the score following a collision.
     switch(type) {
       case 1: myScore.addScore(500); break;
       case 2: myScore.addScore(300); break;
       case 3: myScore.addScore(200); break;
       case 4: myScore.addScore(100); break;
     }
     //Refreshes the score string where we display Score: 500, etc. 
     myScore.updateScore(); 
   }
   SDL_Rect *getBox(){return &box;}
};
vector<Brick> brickLayout;
vector<Brick>::iterator it;


Ball Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Ball
{
  Circle ballCircle; 
  int xVel; int yVel; 
  bool newBall;
 public:     
   Ball(){ ballCircle.x = 200; 
           ballCircle.y = 200; 
           //Changed from 10 to 15 to check the paddle/ball collision issue.
           ballCircle.r = 15;   
           ballCircle.d = 20; 
           xVel = -1; yVel = -2; 
           newBall = true; }
   vector<Brick>::iterator itBrick;
   void changeXVelocity(){ xVel*=(-1); }
   void changeYVelocity(){ yVel*=(-1); }
   void increaseVelocity(){ xVel+=2; yVel+=2; }
   void move()
   { 
     itBrick = brickLayout.begin();
     while( itBrick != brickLayout.end() )
     {
       if( checkCollision( ballCircle, *itBrick->getBox() ) )
        { 
          //Begin with bottom side collision.
          if( ( itBrick->getY()+itBrick->getHeight() ) < ballCircle.y )
          { changeYVelocity(); }
          //Top side collision.
          if(itBrick->getY()>ballCircle.y)
          { changeYVelocity(); }
          //Left side collision.
          if(itBrick->getX()>ballCircle.x)
          { changeXVelocity(); }
          if((itBrick->getX()+itBrick->getWidth())>ballCircle.x)
          { changeXVelocity(); }
          itBrick->destroyBrick();    
        }
       itBrick++;
     }
     //Inverts y velocity when ball collides with paddle. 
     if(checkCollision( ballCircle, *myPad.getBox() ))
     { changeYVelocity(); }
     //Handles ball to screen collisions.
     if( (ballCircle.x<0) || ( ballCircle.x>(scrWidth-ballCircle.d) ) ) 
      { changeXVelocity(); }
     if( ballCircle.y<0 ) 
      { changeYVelocity(); }
     // "Creates" a new ball when the ball reaches the top of the screen.
     if( ballCircle.y>(scrHeight-ballCircle.d) )
      { 
        ballCircle.x = 200; ballCircle.y = 200;
        newBall = true; 
        // Adjust the ball count in the score. 
        myScore.reduceBallCount();
        myScore.updateScore();
      }
      else
        { newBall = false; }
     //Moves the ball after collisions were detected.  
     ballCircle.x+=xVel; ballCircle.y+=yVel; 
   }
   void show()
   { applySurface(ballCircle.x,ballCircle.y,ball,screen); }
};
Ball myBall; 

Last edited on
The bricks shouldn't be caring about how many of themselves were destroyed. That is more of a game feature, and should probably be checked outside of both the ball/game classes.
Topic archived. No new replies allowed.