stacking issue in a game

Hey guys,

sorry about the second question in the space of 30 minutes,I'm pretty bored so I decided to mess around and try create a kind of connect four kind of game with SDL,anyway I seem to have a bug that I can't get to the bottom off,

basically after the 5th ball I would like the balls to keep and stacking up but after 5 the next one comes down and disappears through the top one

here is what I'm talking about - https://youtu.be/-GvFJ5xHuAg

would be cool if someone could point me in the right direction :)

thanks

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219


#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <vector>

using namespace std;

const int WIDTH = 500;
const int HEIGHT = 500;
bool p1,p2;
SDL_Event event;
SDL_Renderer* renderer;
SDL_Window* window;
class Ball;
class BallTracker;
vector<Ball*> redBall;
vector<Ball*> blueBall;
//BallTracker tracker[6];

class BallTracker{

   public:

       int colNumber;
       int amountOfBalls;

       BallTracker(){

           amountOfBalls = 0;

       }
};

class Ball{

   public:
     SDL_Surface* surface;
     SDL_Texture*  texture;
     SDL_Rect destRct;
     int number;
     int colNumber;
     string color;

     Ball(){

        surface = IMG_Load("red.png");
        texture = SDL_CreateTextureFromSurface(renderer,surface);
        destRct.y = 20;
        destRct.h = 80;
        destRct.w = 80;
     }

     ~Ball(){

       SDL_DestroyTexture(texture);

     }

};




bool initGame(){

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0){

        cout << "failed to init" << endl;
        return false;
    }
    if(IMG_Init(IMG_INIT_PNG) < 0){

        cout << "failed to init image" << endl;
        return false;
    }

    window = SDL_CreateWindow("C0nnect f0ur",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_RESIZABLE);
    renderer = SDL_CreateRenderer(window,-1,0);
    SDL_SetRenderDrawColor(renderer,255,255,255,255);
    p1 = false;
    p2 = false;

}

void renderGame(){


   SDL_RenderClear(renderer);

   for(int i = 0; i < redBall.size(); i++){

      SDL_RenderCopy(renderer,redBall.at(i)->texture,NULL,&(redBall.at(i)->destRct));

   }

   SDL_RenderPresent(renderer);

}

void cleanUp(){

   SDL_DestroyRenderer(renderer);
   SDL_DestroyWindow(window);

}

Ball& createBall(){

     Ball* ball = new Ball;
     ball->color = "red";
     ball->number = 1;

     redBall.push_back(ball);

     return *ball;

}

bool collision(Ball& ball){

     if(ball.destRct.y + ball.destRct.h == HEIGHT){

        return true;
     }
     for(int i = 0; i < redBall.size(); i++){

         if((ball.destRct.y + ball.destRct.h ==  redBall.at(i)->destRct.y) && (ball.colNumber == redBall.at(i)->colNumber)){

            return true;
         }

     }

    return false;
}





void choosenCol(int col){

   Ball& ball = createBall();
   ball.destRct.x = 0;
   cout << "rb size" << redBall.size() << endl;

   switch(col){

   case 1:
       while(!collision(ball)){

          ball.colNumber = 1;
          ball.destRct.x = 0;
          ball.destRct.y++;
          // renderBallMoving(ball);
          renderGame();
          SDL_Delay(2);

       }
       break;
   case 2:
        while(!collision(ball)){

          ball.colNumber = 2;
          ball.destRct.x = 80;
          ball.destRct.y++;
          // renderBallMoving(ball);
          renderGame();
          SDL_Delay(2);

       }
        break;
   case 3:
        break;
   case 4:
        break;
   case 5:
        break;
   case 6:
        break;

   }
}


int SDL_main(int argc,char* argv[])
{

    bool quit = false;

    initGame();

    while(!quit){


        SDL_PollEvent(&event);
        if(event.type == SDL_KEYDOWN){

            switch(event.key.keysym.sym){

        case SDLK_1:
            cout << "ball being created in col 1" << endl;
            choosenCol(1);
            break;
        case SDLK_2:
            cout << "ball being created in col 1" << endl;
            choosenCol(2);
            break;

            }
        }

        renderGame();
    }

    cleanUp();
}
...
Last edited on
hey tpb,

that was the plan but finding it hard to implement it with a GUI
...
Last edited on
thanks Tpb :)

that code looks a lot more elegant and compact than mine,

just a question though which part of the code places the discs into the array? I noticed an int array but how does this correlate with the discs?

thanks
...
Last edited on
that looks pretty good,only less than 150 lines of code,that is pretty compact compared to mine,

I took your advice and made a two dimensional board array,but instead of ints I used chars,if red was chosen the position/offset of the array would be an 'r' for player one or 'y' for player two,my game still looks bad lol but just doing this for fun only took a couple of hours altogether.Also not finished it yet either

https://youtu.be/FzDQvKYGnNw

also tpb,please do not delete posts on here it is a selfish thing to do,your answers and code may help someone in the future.(to be honest not that I used it apart from the board idea)

thanks
Topic archived. No new replies allowed.