SFML 2.x BreakOut Help!

Hi guys, i've been trying to make the classic breakout game using SFML. I have the following problems:

1) I cant figure out an easy way to make the bricks, stacked side by side!
2) I cant figure out how to make the ball movement after collision look uhm "Natural!"

Please check my code for what i've got and suggest improvements, 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>

int main()
{
    /*
        Setting up the game window
    */
    sf::RenderWindow window(sf::VideoMode (640,480), "Breakout");

    /*
        Declaring game objects i.e sprites (Paddles, blocks and ball)
    */
    sf::RectangleShape bricks[500];
    sf::CircleShape ball(8.f);
    sf::RectangleShape paddle(sf::Vector2f(60, 10));

    paddle.setFillColor(sf::Color::Blue);
    ball.setFillColor(sf::Color::Red);

    paddle.setPosition((window.getSize().x / 2) - (paddle.getSize().x / 2), window.getSize().y - (30 + paddle.getSize().y));
    ball.setPosition(paddle.getPosition().x, paddle.getPosition().y - 30);

    ball.setRotation(-90);
    sf::Vector2f ballSpeed(-0.5f,-0.5f); //Meh!, Ballspeed constant!..,


    /*
        Set up game window bounds
    */

    //define sides
    sf::RectangleShape top;
    sf::RectangleShape bottom;
    sf::RectangleShape left;
    sf::RectangleShape right;

    //define side properties
    left.setSize(sf::Vector2f(0.1, 640));
    left.setFillColor(sf::Color::White);
    left.setPosition(sf::Vector2f(0,0));

    right.setSize(sf::Vector2f(0.1, 640));
    right.setFillColor(sf::Color::White);
    right.setPosition(sf::Vector2f(640,0));

    bottom.setSize(sf::Vector2f(640, 0.1));
    bottom.setFillColor(sf::Color::White);
    bottom.setPosition(sf::Vector2f(0,479));

    top.setSize(sf::Vector2f(640, 0.1));
    top.setFillColor(sf::Color::White);
    top.setPosition(sf::Vector2f(0,0));

    /*
         Declaring brick properties
     */
    for (unsigned i = 0; i < 500; i++)
    {
        bricks[i].setFillColor(sf::Color::Blue);
        bricks[i].setSize(sf::Vector2f(32, 12));
        bricks[i].setOutlineColor(sf::Color::Black);
        bricks[i].setOutlineThickness(1.f);


        if (i == 0)
        {
            bricks[i].setPosition(0, 0);
        }
        else if (i < 50)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, 0);
        }
        else if (i==50)
        {
            bricks[i].setPosition(0, 0+bricks[i].getSize().y);
        }
        else if (i > 50 && i < 100)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==100)
        {
            bricks[i].setPosition(0, 12+bricks[i].getSize().y);
        }
        else if (i > 100 && i < 150)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==150)
        {
            bricks[i].setPosition(0, 24+bricks[i].getSize().y);
        }
        else if (i > 150 && i < 200)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }

        else if (i==200)
        {
            bricks[i].setPosition(0, 36+bricks[i].getSize().y);
        }
        else if (i > 200 && i < 250)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==250)
        {
            bricks[i].setPosition(0, 48+bricks[i].getSize().y);
        }
        else if (i > 250 && i < 300)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==300)
        {
            bricks[i].setPosition(0, 60+bricks[i].getSize().y);
        }
        else if (i > 300 && i < 350)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==350)
        {
            bricks[i].setPosition(0, 72+bricks[i].getSize().y);
        }
        else if (i > 350 && i < 400)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==400)
        {
            bricks[i].setPosition(0, 84+bricks[i].getSize().y);
        }
        else if (i > 400 && i < 450)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }
        else if (i==450)
        {
            bricks[i].setPosition(0, 96+bricks[i].getSize().y);
        }
        else if (i > 450 && i < 500)
        {
            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, bricks[i-1].getPosition().y);
        }

    }  /*
        There should be easier way to declare bricks without that many if statement: nested for loops maybe?
        */ //End brick properties declaration!


    ball.setPosition(paddle.getPosition()); //ball starts on paddle!

    while(window.isOpen()) //Begin Game Loop!
    {
        sf::Event Event;
        while(window.pollEvent(Event))
        {
            switch (Event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            }
        }

        /*
            Config mouse movement!
        */
        if (sf::Mouse::getPosition(window).x > 0 && sf::Mouse::getPosition(window).x < (window.getSize().x - paddle.getSize().x))
            paddle.setPosition(sf::Mouse::getPosition(window).x, paddle.getPosition().y);

        /*
            Sort out ball and brick collision!
        */
        for (int i = 0; i < 500; i++)
        {
            if(bricks[i].getGlobalBounds().intersects(ball.getGlobalBounds()))
            {
                ballSpeed.y = -ballSpeed.y; //change direction of movement

                bricks[i].setSize(sf::Vector2f(0,0)); //make collided brick disappear!
                i = 499;    //terminate loop!, one collision at a time!
            }
        }

        /*
        Restrict ball within game bounds!
        */
        if(top.getGlobalBounds().intersects(ball.getGlobalBounds()))
        {
            ballSpeed.y = - ballSpeed.y;
        }

        if(bottom.getGlobalBounds().intersects(ball.getGlobalBounds()))
        {
            ballSpeed.y = - ballSpeed.y;
            ball.setPosition(paddle.getPosition());
        }

        if(left.getGlobalBounds().intersects(ball.getGlobalBounds()))
        {
            ballSpeed.x = - ballSpeed.x;
        }

        if(right.getGlobalBounds().intersects(ball.getGlobalBounds()))
        {
            ballSpeed.x = - ballSpeed.x;
        }
        /*
            Handle paddle and ball Collision!
        */
        if(paddle.getGlobalBounds().intersects(ball.getGlobalBounds()))
        {
            ballSpeed.y = -ballSpeed.y; //change direction!
        }

        ball.move(ballSpeed.x, ballSpeed.y);

        window.clear(sf::Color::White);


        for (unsigned i = 0; i < 500; i++)
        {
            window.draw(bricks[i]);
        }

        window.draw(top);
        window.draw(bottom);
        window.draw(left);
        window.draw(right);

        window.draw(paddle);
        window.draw(ball);
        window.display();
    }
}


This code is a modification from:
http://www.cplusplus.com/forum/general/116059/

i think i need something like this to make the bricks
1
2
3
4
bricks[0].setPosition(0,0);
for ( int j = 0; j < 5; j++)
	for(int i = 1; i < 100; i++)
		            bricks[i].setPosition(bricks[i-1].getPosition().x + bricks[i].getSize().x, j);
but just couldnt get it to work :(
Thanks for u help guys!
This is how I do it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #define BLOCK_W 60  //block width
 #define BLK_BUF 100 //border spacing
 #define BLOCK_H 20  //block height
...
for (int i = 0; i < 6; ++i)//rows
{
        float y = BLK_BUF + BLOCK_H / 2.f + (BLOCK_H + 1) * i;//calc y based on row index
        for (int j = 0; j < 10; ++j)//cols
        {
               float x = BLK_BUF + BLOCK_W / 2.f + (BLOCK_W + 1) * j;//calc x based on col index
               std::uint32_t index = static_cast<std::uint32_t>(fmod(i + j, 6));//pick a color for the block
               blocks.emplace_back(
                        Block({ x, y }, colors[index], (index + 1) * 10)//construct block inplace
               );
        }
}
Thanks for the reply naraku9333 though i coldnt understand the logic in your code!, m still a beginner!..

I did however manage to get my for loop working , and look like this:

1
2
3
4
5
6
7
8
9
    for ( int i = 0; i < 10; i++)
    {
        bricks[i+200].setPosition(0,i*13);

        for(int j = i*20+1; j < 20+i*20; j++)
        {
            bricks[j].setPosition(bricks[j-1].getPosition().x + bricks[j].getSize().x, i*13);
        }
    }


Thanks again!
Topic archived. No new replies allowed.