Sorry, that was actually me, I accidentally pressed the wrong button browsing on mobile. I've upvoted a different post to balance it out. :)why did get penalized 3 points?
collision
- Jason Astle-Adams
#include <iostream>
using namespace std;
int main()
{
bool brick[3][8] = { 0 };
int count = 2;
cout << "Draw Ball" << endl;
while (count >= 0)
{
cout << "Move Ball Up" << endl;
brick[count][0] = { 1 };
if (brick[2][0] == 1)
{
cout << "Ball Collides with Lower Brick" << endl;
cout << "Turn Off Brick" << endl;
cout << "Reset Ball"<< endl;
brick[2][0] = 0;
}
if (brick[1][0] == 1)
{
cout << "Ball Collides with Mid Brick" << endl;
cout << "Turn Off Brick" << endl;
cout << "Reset Ball" << endl;
brick[1][0] = 0;
}
if (brick[0][0] == 1)
{
cout << "Ball Collides with Upper Brick" << endl;
cout << "Turn Off Brick" << endl;
cout << "Reset Ball" << endl;
brick[0][0] = 0;
}
count--;
}
system("pause");
return 0;
}
well here is my stubbed out code, all I have to do is integrate this code into my game program. It seems to do exactly what I want it to do. I would appreciate any suggestions or comments.
well I am going back to paper and pencil, I have already done some flowcharts
well here is my stubbed out code, all I have to do is integrate this code into my game program. It seems to do exactly what I want it to do. I would appreciate any suggestions or comments.
You're not going to have an if statement for each brick in your game code are you? That's very inflexible. What happens if you increase the bricks to 6 lines of 20, then you'd need 120 if statements, are you going to write them all?
I would personally find it simpler to make a brick class that stores the position and which also has a collision detection method which can be passed the position of the bullet. I'd then have a list or vector of bricks which I'd iterate through to perform the collision checking.
I don't have time to write any code at the moment, as I'm about to go to work, but i can whip something up when I get home to show you what I mean.
I ended up going straight to bed when I got home from work, but I did this this morning (only one coffee so far, but it works ;) )
I used a list rather than a vector, as when I did my breakout clone, it was more like Arkanoid where the bricks could be laid out in patterns, not just rectangles. The code is fairly straight forward, but if you have any questions, ask away.[spoiler]
#include <iostream>
#include <list>
#include <cstdlib>
#include <ctime>
const int NUMROWS = 3;
const int NUMCOLUMNS = 8;
const int BRICKWIDTH = 3;
const int BRICKHEIGHT = 2;
const int BULLETWIDTH = 1;
const int BULLETHEIGHT = 1;
struct brick {
int x1;
int y1;
int x2;
int y2;
bool active;
bool collision(int _x1, int _y1, int _x2, int _y2);
};
bool brick::collision(int _x1, int _y1, int _x2, int _y2) {
return !(_x1 > x2 || _x2 < x1 || _y1 > y2 || _y2 < y1);
}
struct bullet {
int x;
int y;
};
bool is_inactive(const brick& b) {return (b.active == false);}
int main() {
std::list<brick> bricks;
bullet bul;
srand(time(0));
bul.x = rand() % NUMCOLUMNS * BRICKWIDTH;
bul.y = 10;
for (int rows = 0; rows < NUMROWS; rows++) {
for (int columns = 0; columns < NUMCOLUMNS; columns++) {
brick temp;
temp.x1 = columns * BRICKWIDTH;
temp.x2 = columns * BRICKWIDTH + BRICKWIDTH - 1;
temp.y1 = rows * BRICKHEIGHT;
temp.y2 = rows * BRICKHEIGHT + BRICKHEIGHT - 1;
temp.active = true;
bricks.push_back(temp);
}
}
// print the initial state of all variables.
int count = 1;
for (std::list<brick>::iterator it = bricks.begin(); it != bricks.end(); it++, count++ ) {
std::cout << "Brick #" << count << ": " << it->x1 << ", " << it->y1 << " - " << it->x2 << ", " << it->y2;
if (it->active)
std::cout << " active" << std::endl;
else
std::cout << " inactive" << std::endl;
}
std::cout << "bullet position: " << bul.x << ", " << bul.y << std::endl;
// All 24 bricks have been activated in a 8 x 3 grid, and the bullet has been set to a random x-value with a
// y-value of 10 (at the bottom).
//
// Each game loop, the bullet will be moved upwards, and each brick will check to see if it collides with the
// bullet. If it does, it will be flagged as inactive. At the end of each loop, the inactive bricks will be
// removed from the list of bricks.
bool done = false;
while (!done) {
bul.y--;
std::cout << "bullet position: " << bul.x << ", " << bul.y << std::endl;
for (std::list<brick>::iterator it = bricks.begin(); it != bricks.end(); it++) {
if (it->collision(bul.x, bul.y, bul.x + BULLETWIDTH, bul.y + BULLETHEIGHT)) {
std::cout << "Collision at " << bul.x << ", " << bul.y << std::endl;
it->active = false;
}
}
bricks.remove_if(is_inactive);
if (bul.y == 0) done = true;
}
// print the final state of the bricks
count = 1;
for (std::list<brick>::iterator it = bricks.begin(); it != bricks.end(); it++, count++ ) {
std::cout << "Brick #" << count << ": " << it->x1 << ", " << it->y1 << " - " << it->x2 << ", " << it->y2;
if (it->active)
std::cout << " active" << std::endl;
else
std::cout << " inactive" << std::endl;
}
return 0;
}
[/spoiler]
well I did not want any code I am trying to do this on my own
is there any way I can delete the above post that contains some code, I am really trying to learn on my own.
is there any way I can delete the above post that contains some code, I am really trying to learn on my own.
Close your eyes.
You are not forced to read it, so ignore it if you want to learn on your own.
Hello to all my stalkers.
is there any way I can delete the above post that contains some code, I am really trying to learn on my own.
If you're worried that I've done all the work for you, fear not. That was just the most basic of programs to explain an idea. The implementation I used probably isn't suitable for your needs, but was sufficient for a demonstration.
Reading other people's code is probably one of the best ways you can learn. Not necessarily to copy what they've done, but to be exposed to other ways of doing things. There's usually more than one solution to any problem, and what's best one time might not be another, so the more different ways you learn to do things, the better equipped you'll be to decide how to deal with each new problem you come across.
I would urge you to go read it, just to ask yourself why I wrote it the way I did. When you understand the why, you can redo it yourself in your own style.