The only time it is stopping the character from going out of the screen is when it doesn't move from it's original x position it doesn't go left, and it doesn't let you shoot more then one time.
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;
bullet shoot;
int collision_x(SDL_Rect A, SDL_Rect B){
if(A.w == B.x){
return 1;
}
if(A.x == B.w){
return 2;
}
return 0;
}
int collision_y(SDL_Rect A, SDL_Rect B){
if(A.y == B.h){
return 1;
}
if(A.h == B.y){
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://
if(collision_x(person.coll, space) != 1){
person.xVel = 1;
}
break;
case SDLK_LEFT://
if(collision_x(person.coll, space) != 2){
person.xVel = -1;
}
break;
case SDLK_UP://
if(collision_y(person.coll, space) != 1){
person.yVel = -1;
}
break;
case SDLK_DOWN://
if(collision_y(person.coll, space) != 2){
person.yVel = 1;
}
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_UP: person.yVel = 0; break;
case SDLK_DOWN:person.yVel = 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;
}
void clean_up(){
SDL_FreeSurface(person.person);
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.x = 0;
person.y = 453;
person.w = person.w;
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.yVel = 0;
person.person = NULL;
//bullet init
shoot.x = person.x + 19;
shoot.y = person.y + 14;
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;
shoot.draw = false;
shoot.bullet = NULL;
}
int main(int argc, char *argv[]){
if(Init() == false){
return 1;
}
class_init();
person.person = load_image("person.png");
shoot.bullet = load_image("bullet.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;
handle_events();
person.x += person.xVel;
person.y += person.yVel;
apply_surface(person.x, person.y, person.person, screen);
if(shoot.draw == false){
shoot.draw = false;
shoot.x = person.x + 19;
shoot.y = person.y + 14;
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;
}
if(shoot.draw == true){
apply_surface(shoot.x, shoot.y, shoot.bullet, screen);
shoot.x += 3;
if(collision_x(shoot.coll, space) == 1){
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;
}
}
SDL_Delay(1000/100);
SDL_Flip(screen);
}
clean_up();
return 0;
}