LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2004, 01:46 AM   #1
r3dhatter
Member
 
Registered: Dec 2003
Distribution: Debian (testing)
Posts: 210

Rep: Reputation: 30
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;
}

Last edited by r3dhatter; 05-30-2004 at 02:04 AM.
 
Old 05-30-2004, 12:36 PM   #2
r3dhatter
Member
 
Registered: Dec 2003
Distribution: Debian (testing)
Posts: 210

Original Poster
Rep: Reputation: 30
Also. are there good tutorials for this kind of stuff? I searched on google and found a few, but there must be more.
 
Old 05-30-2004, 12:42 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SDL] Platform game Cornholio Programming 5 03-17-2005 03:39 PM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
SDL/game permissions noxious Fedora 0 11-15-2004 01:58 PM
Is there a keyboard command for using bullets in Kword? kejoca General 0 06-16-2004 08:07 AM
bad collision detection in cube_2003_12_23 release LavaDevil94 Linux - Games 0 12-25-2003 12:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration