In this game when you shoot the bullet passes right through the enemy, and I was hopping the community here could help me out.
here is my code:
Code:
#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include <string>
#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;
int collision_x(SDL_Rect A, SDL_Rect B){
int aLeft = A.x;
int aRight =A.x+ A.w-1;
int aUp = A.y;
int aDown = A.y+A.h-1;
int bLeft = B.x;
int bRight = B.x+B.w-1;
int bUp = B.y;
int bDown = B.y+B.h-1;
if(aLeft == bRight && aUp >= bDown){
return 1; /* collision case */
}
if(aRight == bLeft && aUp >= bDown){
return 2;
}
return 0;
}
int collision_y(SDL_Rect A, SDL_Rect B){
if(A.h >= B.y){
return 1;
}
if(A.y <= B.h){
return 2;
}
return 0;
}
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.right = 1;
break;
case SDLK_LEFT://
person.left = -1;
break;
case SDLK_UP://
if(!person.inAir){
person.inAir = true;
}
break;
case SDLK_DOWN://
person.down = 1;
break;
default: break;
}
}
if(event.type == SDL_KEYUP){
switch(event.key.keysym.sym){
case SDLK_RIGHT:person.right = 0; break;
case SDLK_LEFT:person.left = 0; break;
case SDLK_UP: person.up = 0; break;
case SDLK_DOWN:person.down = 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);
SDL_Quit;
}
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.left = 0;
person.right = 0;
person.up = 0;
person.down = 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.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(collision_x(person.coll, space) != 1){
person.x += person.left;
}
if(collision_x(person.coll, space) != 2){
person.x += person.right;
}
if(collision_y(person.coll, space) != 1){
person.y += person.down;
}
if(collision_y(person.coll, space) != 2){
person.y += person.up;
}
apply_surface(person.x, person.y, person.person, screen);
if(collision_x(shoot.coll, enemy.coll) == 2){
enemy.alive = false;
class_init();
}
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;
if(collision_x(shoot.coll, space) == 1){
enemy.alive = false;
shoot.draw = false;
}
}
SDL_Delay(1000/110);
SDL_Flip(screen);
}
clean_up();
return 0;
}