Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <SDL_ttf.h>
- class instructions
- {
- public:
- void apply(int x,int y,SDL_Surface *source,SDL_Surface *destination)
- {
- SDL_Rect set;
- set.x=x;
- set.y=y;
- SDL_BlitSurface(source,NULL,destination,&set);
- }
- void draw(int srcX,int srcY,int dstX,int dstY,int width,int height,
- SDL_Surface *source,SDL_Surface *destination)
- {
- SDL_Rect src;
- src.x=srcX;
- src.y=srcY;
- src.w=width;
- src.h=height;
- SDL_Rect dst;
- dst.x=dstX;
- dst.y=dstY;
- dst.w=width;
- dst.h=height;
- SDL_BlitSurface(source,&src,destination,&dst);
- }
- void give(int x,int y,int w,int h,SDL_Rect &fake)
- {
- fake.x=x;
- fake.y=y;
- fake.w=w;
- fake.h=h;
- }
- bool CheckCollision(float Ax,float Ay,float Aw,float Ah,
- float Bx,float By,float Bw,float Bh)
- {
- if(Ay+Ah<By)
- return false;
- else if(Ay>By+Bh)
- return false;
- else if(Ax+Aw<Bx)
- return false;
- else if(Ax>Bx+Bw)
- return false;
- return true;
- }
- };
- SDL_Surface *screen=NULL;
- SDL_Surface *heroe=NULL;
- SDL_Surface *imagebackground=NULL;
- SDL_Event event;
- bool run=true;
- int srcX1=0;
- int srcY1=0;
- int dstX1=-80;
- int dstY1=820;
- int width1=32;
- int height1=32;
- int ScreenWidth=820;
- int ScreenHeight=850;
- int x;
- int y;
- int main(int argc, char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_WM_SetCaption("RGP game",NULL);
- screen=SDL_SetVideoMode(1000,1000,32,SDL_SWSURFACE);
- instructions instr;
- heroe=SDL_LoadBMP("heroe.bmp");
- imagebackground=SDL_LoadBMP("picturegame.bmp");
- SDL_SetColorKey(heroe,SDL_SRCCOLORKEY,SDL_MapRGB(heroe->format,0xff,0xff,0xff));
- Uint32 backround=SDL_MapRGB(screen->format,0x00,0xff,0xff);
- Uint32 color=SDL_MapRGB(screen->format,0x00,0x00,0xff);
- SDL_Rect obstacle,obstacle1,obstacle2,obstacle3,obstacle4;
- instr.give(150,970,200,40,obstacle);
- instr.give(200,910,40,60,obstacle1);
- instr.give(500,930,40,70,obstacle2);
- instr.give(540,970,200,30,obstacle3);
- instr.give(720,930,40,70,obstacle4);
- const int speed=9;
- SDL_Rect camera;
- instr.give(0,0,1000,1000,camera);
- bool up=false;
- bool down=false;
- bool right=false;
- bool left=false;
- bool changeColor=false;
- while(run)
- {
- if(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- run=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_DOWN:
- changeColor=true;
- break;
- case SDLK_UP:
- up=true;
- down=false;
- break;
- case SDLK_RIGHT:
- right=true;
- break;
- case SDLK_LEFT:
- left=true;
- break;
- }
- break;
- case SDL_KEYUP:
- switch(event.key.keysym.sym)
- {
- case SDLK_DOWN:
- changeColor=false;
- break;
- case SDLK_UP:
- up=false;
- down=true;
- break;
- case SDLK_RIGHT:
- right=false;
- break;
- case SDLK_LEFT:
- left=false;
- break;
- }
- break;
- }
- }
- if(changeColor==true)
- {
- backround=SDL_MapRGB(screen->format,0x60,0x60,0x60);
- }
- if(up==true)
- {
- dstY1--;
- }
- if(down==true)
- {
- dstY1++;
- }
- if(right==true && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true))
- {
- dstX1++;
- x+=speed;
- camera.x+=speed;
- if(camera.x>=6600)
- camera.x=0;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
- dstX1-=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
- dstX1-=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
- dstX1-=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
- dstX1-=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)
- dstX1-=2;
- }
- if(left==true && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
- && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true))
- {
- dstX1--;
- x-=speed;
- camera.x-=speed;
- if(camera.x<=0)
- camera.x=6600;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
- dstX1+=2;
- if( instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
- dstX1+=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
- dstX1+=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
- dstX1+=2;
- if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)
- dstX1+=2;
- }
- //////////////////////////////
- //////////////////////////////
- if(dstX1<-130)
- {
- dstX1=-130;
- }
- if(dstY1<-85)
- {
- dstY1=-85;
- }
- //////////////////////////////
- //////////////////////////////
- if(dstX1+width1 > ScreenWidth)
- {
- dstX1=ScreenWidth-width1;
- }
- if(dstY1+height1 > ScreenHeight)
- {
- dstY1=ScreenHeight-height1;
- }
- ///////////////////////////////////
- ///////////////////////////////////
- SDL_FillRect(screen,&screen->clip_rect,backround);
- SDL_BlitSurface(imagebackground,&camera,screen,NULL);
- SDL_FillRect(screen,&obstacle,color);
- SDL_FillRect(screen,&obstacle1,color);
- SDL_FillRect(screen,&obstacle2,color);
- SDL_FillRect(screen,&obstacle3,color);
- SDL_FillRect(screen,&obstacle4,color);
- instr.apply(dstX1,dstY1,heroe,screen);
- instr.draw(srcX1,srcY1,dstX1,dstY1,width1,height1,heroe,screen);
- SDL_Flip(screen);
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement