Thread: Help with C++ on a simple game

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    19

    Help with C++ on a simple game

    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?

  2. #2
    spaghetticode
    Guest
    Quote Originally Posted by Luminous Lizard View Post
    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?
    No offense here, but if that's your question, quickly forget about your game and start learning the basics.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    19
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    19
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    19
    Im stuck, i just can't get arrays to work!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    19
    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.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    19
    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.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    19
    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:
    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;
    
    }
    So where would i put it in?

    If it went in my main prog i think it would be here:
    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
    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.

    Hope ive asked this question correctly.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Simple Game
    By lukec2 in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2011, 11:34 PM
  2. C++ Simple Game.
    By united07 in forum C++ Programming
    Replies: 14
    Last Post: 05-05-2010, 06:09 AM
  3. help with a simple game..
    By ninne in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2006, 05:48 AM
  4. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 11:05 AM
  5. my new (simple) game
    By dune911 in forum C Programming
    Replies: 14
    Last Post: 03-16-2002, 03:20 PM