Enemy disappearing with out collision with bullet

When I fire the bullet in the game it “kills” the enemy with out showing up on the screen

here is my code

Code:

#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include
#include “class.h”

using namespace std;

SDL_Surface *screen = NULL;

SDL_Rect space;

SDL_Event event;

bool quit = false;

character person;
villian enemy;
bullet shoot;

const int GRAVITATIONAL_ACCELARATION = -2;
const int MAX_FALL_HEIGHT = -12;
const int FALL_SPEED = 3;

bool collision(SDL_Rect A, SDL_Rect B){

int aLeft = A.x;
int aRight =A.x+ A.w;
int aUp = A.y;
int aDown = A.y+A.h;

int bLeft = B.x;
int bRight = B.x+B.w;
int bUp = B.y;
int bDown = B.y+B.h;

if(aDown <= bUp){
return false;
}
if(aUp >= bDown){
return false;
}
if(aRight >= bLeft){
return false;
}
if(aLeft <= bRight){
return false;
}
return true;
}

void handle_events(){
if(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
quit = true;
}
if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_ESCAPE: quit = true; break;
case SDLK_RIGHT://
person.xVel = 1;
break;
case SDLK_LEFT://
person.xVel = -1;
break;
case SDLK_UP://
if(!person.inAir){
person.inAir = true;
}
break;
default: break;
}
}
if(event.type == SDL_KEYUP){
switch(event.key.keysym.sym){
case SDLK_RIGHT:person.xVel = 0; break;
case SDLK_LEFT:person.xVel = 0; break;
case SDLK_SPACE://
if(shoot.draw == false){
shoot.draw = true;
}
break;
default: break;
}
}
}
}

void apply_surface(int X, int Y, SDL_Surface *source, SDL_Surface *destination){
SDL_Rect pos;
pos.x = X;
pos.y = Y;

SDL_BlitSurface(source, NULL, screen, &pos);

}

SDL_Surface *load_image(string filename){
SDL_Surface *surface = NULL;
SDL_Surface *optSurface = NULL;

surface = IMG_Load(filename.c_str());

if(surface != NULL){
    optSurface = SDL_DisplayFormat(surface);
    SDL_FreeSurface(surface);
    if(optSurface != NULL){
    SDL_SetColorKey( optSurface, SDL_SRCCOLORKEY, SDL_MapRGB( optSurface->format, 0, 0xFF, 0xFF ) );
    }
}
return optSurface;

}

bool load_files(){
return true;
}

void clean_up(){
SDL_FreeSurface(person.person);
SDL_FreeSurface(shoot.bullet);
SDL_FreeSurface(enemy.enemy);
}

bool Init(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
return false;
}

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

if(screen == NULL){
    return 0;
}
SDL_WM_SetCaption("Shooter", NULL);

space.x = 640;
space.y = 480;
space.w = 0;
space.h = 0;

return true;

}

void class_init(){
//charecter init
person.inAir = false;
person.airTime = 0;

person.x = 0;
person.y = 453;
person.w = person.x + 19;
person.h = person.y + 28;

person.coll.x = person.x;
person.coll.y = person.y;
person.coll.w = person.w;
person.coll.h = person.h;

person.xVel = 0;

person.person = NULL;

//bullet init
shoot.x = person.x + 19;
shoot.y = person.y + 13;
shoot.w = shoot.x + 7;
shoot.h = shoot.y + 3;
shoot.timer = 0;

shoot.coll.x = shoot.x;
shoot.coll.y = shoot.y;
shoot.coll.w = shoot.w;
shoot.coll.h = shoot.h;

shoot.draw = false;

shoot.bullet = NULL;

//enemy init
enemy.x = 619;
enemy.w = enemy.x + 21;
enemy.y = 451;

enemy.alive = true;

enemy.coll.x = enemy.x;
enemy.coll.w = enemy.w;

}

int main(int argc, char *argv[]){

if(Init() == false){
    return 1;
}
class_init();
if(load_files() == false){
    return 1;
}

person.person = load_image("person.png");
shoot.bullet = load_image("bullet.png");
enemy.enemy = load_image("enemy.png");

while(quit == false){
    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x237, 0x20, 0x48));

    person.w = person.x + 19;
    person.h = person.y + 28;
    person.coll.x = person.x;
    person.coll.y = person.y;
    person.coll.w = person.w;
    person.coll.h = person.h;

    if(shoot.w >= 640){
        shoot.draw = false;
    }

    if(shoot.draw == false){
    shoot.x = person.x + 19;
    shoot.y = person.y + 11;
    shoot.w = shoot.x + 7;
    shoot.h = shoot.y + 3;

    shoot.coll.x = shoot.x;
    shoot.coll.y = shoot.y;
    shoot.coll.w = shoot.w;
    shoot.coll.h = shoot.h;
    }

    while(person.inAir){
        person.up += GRAVITATIONAL_ACCELARATION;
        if(person.up <= MAX_FALL_HEIGHT){
            person.up = FALL_SPEED;
        }
        if(person.y == 453){
            person.up = 0;
            person.inAir = false;
        }
    }

    handle_events();

    if(person.x <= 0){
    person.x = 0;
    }
    if(person.w >= 640){
    person.x = 640 - 19;
    }
    person.x += person.xVel;

    apply_surface(person.x, person.y, person.person, screen);

    if(!collision(shoot.coll, enemy.coll) && shoot.draw){
        enemy.alive = false;
        shoot.draw = false;
    }
    if(enemy.alive){
        apply_surface(enemy.x, enemy.y, enemy.enemy, screen);
        enemy.x--;
    }

    if(shoot.draw == true){
        apply_surface(shoot.x, shoot.y, shoot.bullet, screen);
        shoot.x += 4;
    }

    SDL_Delay(1000/110);

    SDL_Flip(screen);
}

clean_up();
return 0;

}

Hey,

This particular mailing list is technically meant for discussion of the SDL
API, its usage, and its development. You should try asking gameplay
questions in a mailing list or forum that is geared towards that topic (try
http://gameprogrammer.com/mailinglist.html,
http://groups.google.com/group/sdl-games, or
http://www.gamedev.net/community/forums/forum.asp?forum_id=11).

However, I think you made a little mistake in the collision function:
if(aRight >= bLeft){
return false;
}
if(aLeft <= bRight){
return false;
}

Should be:
if(aRight <= bLeft){
return false;
}
if(aLeft >= bRight){
return false;
}

Jonny DOn Sat, Jul 3, 2010 at 12:52 PM, bijan311 wrote:

When I fire the bullet in the game it “kills” the enemy with out showing
up on the screen

here is my code

Code:

#include
#include
#include
#include “class.h”

using namespace std;

SDL_Surface *screen = NULL;

SDL_Rect space;

SDL_Event event;

bool quit = false;

character person;
villian enemy;
bullet shoot;

const int GRAVITATIONAL_ACCELARATION = -2;
const int MAX_FALL_HEIGHT = -12;
const int FALL_SPEED = 3;

bool collision(SDL_Rect A, SDL_Rect B){

int aLeft = A.x;
int aRight =A.x+ A.w;
int aUp = A.y;
int aDown = A.y+A.h;

int bLeft = B.x;
int bRight = B.x+B.w;
int bUp = B.y;
int bDown = B.y+B.h;

if(aDown <= bUp){
return false;
}
if(aUp >= bDown){
return false;
}
if(aRight >= bLeft){
return false;
}
if(aLeft <= bRight){
return false;
}
return true;
}

void handle_events(){
if(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
quit = true;
}
if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_ESCAPE: quit = true; break;
case SDLK_RIGHT://
person.xVel = 1;
break;
case SDLK_LEFT://
person.xVel = -1;
break;
case SDLK_UP://
if(!person.inAir){
person.inAir = true;
}
break;
default: break;
}
}
if(event.type == SDL_KEYUP){
switch(event.key.keysym.sym){
case SDLK_RIGHT:person.xVel = 0; break;
case SDLK_LEFT:person.xVel = 0; break;
case SDLK_SPACE://
if(shoot.draw == false){
shoot.draw = true;
}
break;
default: break;
}
}
}
}

void apply_surface(int X, int Y, SDL_Surface *source, SDL_Surface
*destination){
SDL_Rect pos;
pos.x = X;
pos.y = Y;

SDL_BlitSurface(source, NULL, screen, &pos);

}

SDL_Surface *load_image(string filename){
SDL_Surface *surface = NULL;
SDL_Surface *optSurface = NULL;

surface = IMG_Load(filename.c_str());

if(surface != NULL){
    optSurface = SDL_DisplayFormat(surface);
    SDL_FreeSurface(surface);
    if(optSurface != NULL){
    SDL_SetColorKey( optSurface, SDL_SRCCOLORKEY, SDL_MapRGB(

optSurface->format, 0, 0xFF, 0xFF ) );
}
}
return optSurface;
}

bool load_files(){
return true;
}

void clean_up(){
SDL_FreeSurface(person.person);
SDL_FreeSurface(shoot.bullet);
SDL_FreeSurface(enemy.enemy);
}

bool Init(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
return false;
}

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

if(screen == NULL){
    return 0;
}
SDL_WM_SetCaption("Shooter", NULL);

space.x = 640;
space.y = 480;
space.w = 0;
space.h = 0;

return true;

}

void class_init(){
//charecter init
person.inAir = false;
person.airTime = 0;

person.x = 0;
person.y = 453;
person.w = person.x + 19;
person.h = person.y + 28;

person.coll.x = person.x;
person.coll.y = person.y;
person.coll.w = person.w;
person.coll.h = person.h;

person.xVel = 0;

person.person = NULL;

//bullet init
shoot.x = person.x + 19;
shoot.y = person.y + 13;
shoot.w = shoot.x + 7;
shoot.h = shoot.y + 3;
shoot.timer = 0;

shoot.coll.x = shoot.x;
shoot.coll.y = shoot.y;
shoot.coll.w = shoot.w;
shoot.coll.h = shoot.h;

shoot.draw = false;

shoot.bullet = NULL;

//enemy init
enemy.x = 619;
enemy.w = enemy.x + 21;
enemy.y = 451;

enemy.alive = true;

enemy.coll.x = enemy.x;
enemy.coll.w = enemy.w;

}

int main(int argc, char *argv[]){

if(Init() == false){
    return 1;
}
class_init();
if(load_files() == false){
    return 1;
}

person.person = load_image("person.png");
shoot.bullet = load_image("bullet.png");
enemy.enemy = load_image("enemy.png");

while(quit == false){
    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format,

0x237, 0x20, 0x48));

    person.w = person.x + 19;
    person.h = person.y + 28;
    person.coll.x = person.x;
    person.coll.y = person.y;
    person.coll.w = person.w;
    person.coll.h = person.h;

    if(shoot.w >= 640){
        shoot.draw = false;
    }

    if(shoot.draw == false){
    shoot.x = person.x + 19;
    shoot.y = person.y + 11;
    shoot.w = shoot.x + 7;
    shoot.h = shoot.y + 3;

    shoot.coll.x = shoot.x;
    shoot.coll.y = shoot.y;
    shoot.coll.w = shoot.w;
    shoot.coll.h = shoot.h;
    }

    while(person.inAir){
        person.up += GRAVITATIONAL_ACCELARATION;
        if(person.up <= MAX_FALL_HEIGHT){
            person.up = FALL_SPEED;
        }
        if(person.y == 453){
            person.up = 0;
            person.inAir = false;
        }
    }

    handle_events();

    if(person.x <= 0){
    person.x = 0;
    }
    if(person.w >= 640){
    person.x = 640 - 19;
    }
    person.x += person.xVel;

    apply_surface(person.x, person.y, person.person, screen);

    if(!collision(shoot.coll, enemy.coll) && shoot.draw){
        enemy.alive = false;
        shoot.draw = false;
    }
    if(enemy.alive){
        apply_surface(enemy.x, enemy.y, enemy.enemy, screen);
        enemy.x--;
    }

    if(shoot.draw == true){
        apply_surface(shoot.x, shoot.y, shoot.bullet, screen);
        shoot.x += 4;
    }

    SDL_Delay(1000/110);

    SDL_Flip(screen);
}

clean_up();
return 0;

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

ok thanks