LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Tank Game Collision Detection and Bullets SDL/C++ (https://www.linuxquestions.org/questions/programming-9/tank-game-collision-detection-and-bullets-sdl-c-187548/)

r3dhatter 05-30-2004 01:46 AM

Tank Game Collision Detection and Bullets SDL/C++
 
How should I go about making the collision detection for the tanks, rocks, and tree's? Arrays?

Also, what should I use to make the bullets appear, and then move across the screen?

This is my first graphical game, so I am not too sure how do go about this, but I am willing to learn.

Any suggestions on fixing the code overall would be great too.

Thanks, and here's the code:
Code:

#include <SDL/SDL.h>

#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define TANK_SIZE 32


//trying different arrays to work on collision detection
int main(int argc, char *argv[])    //*argv[3]
{
//argv[3] = "*rock, rcRock";
//argv[1] = "*tank, rcTank";
//argv[2] = "*tank2, rcTank2";

////////////////////
// VARIABLES    //
///////////////////

    SDL_Surface *screen, *temp, *sprite, *tank, *tank2, *grass, *rock, *tree, *bullet;
    SDL_Rect rcTank, rcTank2, rcGrass, rcRock, rcTree, rcBullet;
    SDL_Event event;
    Uint8 *keystate;

    int colorkey, gameover, position;

///////////////////
// END VARIABLES //
///////////////////

///////////////////
// LOAD GRAPHICS //
///////////////////

    // initialize sdl
    SDL_Init(SDL_INIT_VIDEO);

    // window title
    SDL_WM_SetCaption("Tank", "Tank");
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);

    // load tank 1
    temp = SDL_LoadBMP("tank.bmp");
    tank = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    // tank 1 color key
    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    SDL_SetColorKey(tank, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

    // load tank 2
    temp = SDL_LoadBMP("tank2.bmp");
    tank2 = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    // tank 2 color key
    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    SDL_SetColorKey(tank2, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

    // load bullet
    temp = SDL_LoadBMP("bullet.bmp");
    bullet = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    // bullet colorkey
    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    SDL_SetColorKey(bullet, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

    // load grass
    temp = SDL_LoadBMP("grass.bmp");
    grass = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    // load rock
    temp = SDL_LoadBMP("rock.bmp");
    rock = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    // rock color key
    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    SDL_SetColorKey(rock, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

    // load tree
    temp = SDL_LoadBMP("tree.bmp");
    tree = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

    // tree color key
    colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    SDL_SetColorKey(tree, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

///////////////////////
// END LOAD GRAPHICS //
///////////////////////


/////////////////
// LOCATIONS  ///
/////////////////
    rcTank.x = 80;
    rcTank.y = 80;
    rcTank2.x = 900;
    rcTank2.y = 600;

    // NUETRAL UNITS //
    rcRock.x = 200;
    rcRock.y = 300;

    rcTree.x = 250;
    rcTree.y = 250;

    gameover = 0;

    while(!gameover)
    {
      // look for an event
      if (SDL_PollEvent(&event)){
        // event was found
        switch (event.type){
          // close button (X) clicked
          case SDL_QUIT:
            gameover = 1;
            break;

          // handle keyboard
          case SDL_KEYDOWN:
            switch (event.key.keysym.sym){
              case SDLK_ESCAPE:
              case SDLK_q:
                gameover = 1;
                break;
            }
            break;
        }
      }

////////////////////////////
//  tank 1 movement      //
////////////////////////////
      keystate = SDL_GetKeyState(NULL);

      if (keystate[SDLK_LEFT] ){
      rcTank.x -= 2;
      }
      if (keystate[SDLK_RIGHT] ){
      rcTank.x += 2;
      }
      if (keystate[SDLK_UP] ){
      rcTank.y -= 2;
      }
      if (keystate[SDLK_DOWN] ){
      rcTank.y += 2;
      }
      if (keystate[SDLK_SPACE] ){
      SDL_BlitSurface(bullet, NULL, screen, &rcBullet);
      SDL_UpdateRect(screen, 0, 0, 0, 0);
      }

/////////////////////////
// COLLISION DETECTION //
/////////////////////////
    if (rcTank.x < 0){
      rcTank.x = 0;
    }
    else if (rcTank.x > SCREEN_WIDTH-TANK_SIZE){
      rcTank.x = SCREEN_WIDTH-TANK_SIZE;
    }
    if (rcTank.y < 0){
      rcTank.y = 0;
    }
    else if (rcTank.y > SCREEN_HEIGHT-TANK_SIZE){
      rcTank.y = SCREEN_HEIGHT-TANK_SIZE;
    }


// still trying to figure how I am going to input collision
// detection
    ///if (rcTank.x > 3)return(0);// || (rcTank.x  3)){
      //if (rcTank.x*y == rcRock.x*y){
    //  rcTank.x -= rcRock.x;
    //  }

    //if (rcTank.y == rcRock.y){
    // rcTank.y -= rcRock.y;
    // }
    //

/// END ///
/*if (rcRock.x = 3){
  rcRock.x = 3;
}
if (rcRock.y = 3){
  rcRock.y = 3;
} */

/////////////////////////////////
//  tank 2 movement            //
/////////////////////////////////
      keystate = SDL_GetKeyState(NULL);

      if (keystate[SDLK_d] ){
      rcTank2.x -= 2;
      }
      if (keystate[SDLK_g] ){
      rcTank2.x += 2;
      }
      if (keystate[SDLK_r] ){
      rcTank2.y -= 2;
      }
      if (keystate[SDLK_f] ){
      rcTank2.y += 2;
      }

    if (rcTank2.x < 3 ){    // 0
      rcTank2.x = 3;
    }
    else if (rcTank2.x > SCREEN_WIDTH-TANK_SIZE){
      rcTank2.x = SCREEN_WIDTH-TANK_SIZE;
    }
    if (rcTank2.y < 3){
      rcTank2.y = 3;            // 0
    }
    else if (rcTank2.y > SCREEN_HEIGHT-TANK_SIZE){
      rcTank2.y = SCREEN_HEIGHT-TANK_SIZE;
    }
/// END ///


//////////////////////////
//    DRAW SPRITES      //
//////////////////////////

// draw grass //
    for (int x = 0; x < SCREEN_WIDTH / TANK_SIZE; x++){
      for (int y = 0; y < SCREEN_HEIGHT / TANK_SIZE; y++){
        rcGrass.x = x * TANK_SIZE;
        rcGrass.y = y * TANK_SIZE;
        SDL_BlitSurface(grass, NULL, screen, &rcGrass);
      }
    }

    // draw tank 1 //
    SDL_BlitSurface(tank, NULL, screen, &rcTank);

    // draw tank 2 //
    SDL_BlitSurface(tank2, NULL, screen, &rcTank2);

    // draw rock //
    SDL_BlitSurface(rock, NULL, screen, &rcRock);

    // draw tree //
    SDL_BlitSurface(tree, NULL, screen, &rcTree);

///////////////////////
//  END            //
///////////////////////


    // update screen //
    SDL_UpdateRect(screen, 0, 0, 0, 0);
  }
  // clean up sdl //
  SDL_FreeSurface(tank);
  SDL_FreeSurface(tank2);
  SDL_FreeSurface(grass);
  SDL_FreeSurface(rock);
  SDL_FreeSurface(bullet);
  SDL_Quit();

  return 0;
}


r3dhatter 05-30-2004 12:36 PM

Also. are there good tutorials for this kind of stuff? I searched on google and found a few, but there must be more.

Dark_Helmet 05-30-2004 12:42 PM

Search for "Programming Linux Games". It's an entire book that's available for download, and written by some of the folks at Loki. They develop a small game in it and discuss things like collision detection. It's not too shabby, but I haven't put it all to use yet (still in the planning stages).


All times are GMT -5. The time now is 08:39 AM.