Hi im trying to make a Block breaker game, and i want 88 blocks in which to break, how do i make an array to do such a thing?
Hi im trying to make a Block breaker game, and i want 88 blocks in which to break, how do i make an array to do such a thing?
How To Ask Questions The Smart Way
Who knows - your question is a vague demand.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well could someone point me in the direction of an example of an array in a game.
I have done arrays before and i have to code that i think would work, and i have some of the coding for my game complete but its all in the main prog at the moment and i don't know how to implement it. The only way i can do it so far is abandon the array and just make 88 sprites.
Arrays in games work just the same as arrays in any other kind of a program.
If you have
someType thisIsMySprite;
Then there is really nothing to getting as far as
someType thisIsMyArrayOfSprites[88];
You're just going to have to show us what you're doing if you're getting stuck.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Current program, i have a more in depth one that has header files etc but im just trying to get it run first then ill put it in different files.
At the moment all it does is display the background, move the paddle left and right and shoot a ball but it doesn't have collision detection.
Code:#include "Awsprite.h" int main (int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); // initislise SDL SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE|SDL_DOUBLEBUF) ; SDL_Surface* temp = SDL_LoadBMP("bgrd1.bmp"); //load bgrd bitmap SDL_Surface* bg = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); AWSprite *block,*bat, *ball; //declare sprites block = new AWSprite ("block1.bmp", 1); bat = new AWSprite ("bat1.bmp", 1); ball = new AWSprite ("s_ball.bmp", 1); block->set_world_position(20,20); bat->set_world_position (255,455); ball->set_world_position(-10, -10); block->set_transparent_colour (0,0,0); bat->set_transparent_colour(0,0,0); ball->set_transparent_colour(0,0,0); SDL_Event event; // This is the game loop message handler bool gameover = false; while (!gameover) //loop untill game over { SDL_Delay(10); //delay 10MS so CPU not used excessaivily if (SDL_PollEvent(&event)) //an event was found { switch (event.type) { case SDL_QUIT: //close button clicked gameover = true; break; case SDL_KEYDOWN: /* handle the keyboard */ switch (event.key.keysym.sym) { case SDLK_RIGHT: bat->set_velocities(10, 0); bat ->move(); break; case SDLK_LEFT: bat->set_velocities(-10, 0); bat ->move(); break; case SDLK_SPACE: int pw = bat->get_width(); int ph = bat->get_height(); int px = bat->get_x(); int py = bat->get_y(); px += pw/2; py = ph - 3; ball->set_world_position (px, py); ball->set_auto_move(10); ball->set_velocities(0, 10); break; ball->update_everything(); } break; } //end event.type } // end if SDL_BlitSurface(bg, NULL, screen, NULL); //drawb/grd - NULL for whole screen block->update_everything(); bat->update_everything(); ball->update_everything(); SDL_Flip(screen); // flip the buffers } //end while SDL_FreeSurface(bg); //cleanup SDL SDL_Quit(); return 0; }
Code:int main(int argc, char *argv[]) { const int NUM_BLOCKS = 10; int currProjectile = 0; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); // initislise SDL SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE|SDL_DOUBLEBUF) ; SDL_Surface* temp = SDL_LoadBMP("si_back.bmp"); //load bgrd bitmap SDL_Surface* bg = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); Block*b[NUM_BLOCKS]; for(int i = 0; i < NUM_BLOCKS; i++) B[i] = new Block("block1.bmp",300, 200); SDL_Event event; // This is the game loop message handler bool gameover = false; while (!gameover) //loop untill game over { SDL_Delay(10); //delay 10MS so CPU not used excessaivily if (SDL_PollEvent(&event)) //an event was found { switch (event.type) { case SDL_QUIT: //close button clicked gameover = true; break; case SDL_KEYDOWN: /* handle the keyboard */ switch (event.key.keysym.sym) { case SDLK_SPACE: b[currBlock]->setVelocities(10,0); break; b[currBlock]->update_everything(); } break; } //end event.type } // end if
OK, so start with just two blocks at different points on the screen, then try to write some collision detection code to see if you can tell which one is hit.
I think you've got the basic idea sorted out, it's just a matter of applying it to the problem at hand.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You can draw the screen from the array. You can use simple division and modulo arithmetic to determine what row and column on the screen the ball is in and/or has hit or you can use simple AABB collision detection to detect if the ball hits something. If the blocks always fill the grid cell they are in there is no reason to do complex collision detection.
Im stuck, i just can't get arrays to work!
What's that supposed to mean (or what do you expect us to do).
If you have an array of blocks, can you draw them all with a for loop?
Have you tried writing smaller test programs to explore just one aspect of your game?
For one thing, you might just see what it is you're doing wrong.
For another, you'll have something simpler to post when you get stuck.
For sure, if you just say "help, it doesn't work" without posting any code or error messages, you're just going to get ignored.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
OK apologies i don't really post on forums much.
My issue as it stands is not necessarily error messages, its more that i dont know where to start putting the array. I posted the code i was using (Which is a simpler version of my code that im using for my actual game) and code that i think (correct me if im wrong) would work correctly if i knew where to put it. So what im asking is where to start really, maybe a few steps which will get me started. Apologies if this is frowned upon, i was just asking for help.
Well the simple fact is, if you don't post some code and some error messages (or at least some observations along the lines of "I expected 'x', but 'y' happened), there isn't a lot we can do except offer you a virtual cup of tea.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Iv'e done it now, it was simpler than i thought it was and took in me like 5 minutes. Now onto to the collision code. As a question, i emailed my folder to myself in a zip and now i can open it in visual studio and can run it but it doesn't show any of my code and wont let me put individual CPP or header files?
EDIT: doesn't matter ive solved the problem now.
Last edited by Luminous Lizard; 05-23-2011 at 09:53 AM.
Hello, how would you input code to make is so the user has a option to change the angel of which the projectile moves? Would it be in the Projectile CPP or my mainprog?
Here is my Projectile CPP:
So where would i put it in?Code:#include "Projectile.h" #include "AWFont.h" #include "AWSprite.h" Projectile::Projectile(char* bmp, float X, float Y) : AWSprite (bmp, 10 ) { set_velocities(0, 5); set_world_position(X,Y); set_transparent_colour(0,0,0); AWSprite *Projectile1; //declare sprites Projectile1 = new AWSprite ("Projectile.bmp", 1); } void Projectile::setVelocities(float speed, float angleInDeg) { set_velocities(11,-6); float angleInReg = angleInDeg *3.1459/180; speed = 1; velX = speed *cos(angleInReg); velY = -speed *sin(angleInReg); set_auto_move(10); } void Projectile::updatePosition(float timeInSecs) { worldX =initialX + velX *timeInSecs; worldY = initialY + velY *timeInSecs + 0.5*9.8*timeInSecs*timeInSecs; }
If it went in my main prog i think it would be here:
But with an SDLK_UP function? How would you make it so you get the angel they have chosen at the top of the screen, im guessing cout with it but im lost on how to input it.Code:SDL_Event event; // This is the game loop message handler bool gameover = false; while (!gameover) //loop untill game over { SDL_Delay(10); //delay 10MS so CPU not used excessaivily if (SDL_PollEvent(&event)) //an event was found { switch (event.type) { case SDL_QUIT: //close button clicked gameover = true; break; case SDL_KEYDOWN: /* handle the keyboard */ switch (event.key.keysym.sym) { case SDLK_SPACE: if(currProjectile < NUM_PROJECTILES) { p[currProjectile]->set_auto_move(10); p[currProjectile]->update_everything(); p[currProjectile]->setVelocities(11, 43); currProjectile++; for(int i = 0; i < NUM_PROJECTILES; i++) p[i]->move(); } break; } break; } //end event.type } // end if
Hope ive asked this question correctly.
Do you have some kind of text display for the angle, and say an up/down arrow to control it?
Perhaps make this into a small class, say angleControl.cpp with some methods to respond to up/down events, something to draw it, and something to get the current angle.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.