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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
05-30-2004, 01:46 AM
#1
Member
Registered: Dec 2003
Distribution: Debian (testing)
Posts: 210
Rep:
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 .
05-30-2004, 12:36 PM
#2
Member
Registered: Dec 2003
Distribution: Debian (testing)
Posts: 210
Original Poster
Rep:
Also. are there good tutorials for this kind of stuff? I searched on google and found a few, but there must be more.
05-30-2004, 12:42 PM
#3
Senior Member
Registered: Jan 2003
Posts: 2,786
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 02:53 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News