#include #include SDL_Surface *screen=NULL; SDL_Surface *text=NULL; SDL_Surface *heroe=NULL; SDL_Surface *imagebackground=NULL; SDL_Event event; bool run=true; int srcX1=0; int srcY1=0; int dstX1=5; int dstY1=840; int ScreenWidth=1000; int ScreenHeight=1000; class instructions { public: void apply(int srcX,int srcY,int x,int y,int w,int h,SDL_Surface *source,SDL_Surface *destination) { SDL_Rect set; set.x=x; set.y=y; set.w=w; set.h=h; SDL_Rect src; src.x=srcX; src.y=srcY; src.w=w; src.h=h; SDL_BlitSurface(source,&src,destination,&set); } 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+AhBy+Bh) return false; else if(Ax+AwBx+Bw) return false; return true; } int showmenu(SDL_Surface *screen,TTF_Font *font) { int x,y; const int NUMMENU=2; const char *labels[NUMMENU]={"Continue","Exit"}; SDL_Surface *menus[NUMMENU]; bool selected[NUMMENU]={0,0}; SDL_Color color[2]={{255,255,255},{255,0,0}}; menus[0]=TTF_RenderText_Solid(font,labels[0],color[0]); menus[1]=TTF_RenderText_Solid(font,labels[1],color[0]); SDL_Rect pos[NUMMENU]; pos[0].x=screen->clip_rect.w/2 - menus[0]->clip_rect.w/2; pos[0].y=screen->clip_rect.h/2 - menus[0]->clip_rect.h; pos[1].x=screen->clip_rect.w/2 - menus[0]->clip_rect.w/2; pos[1].y=screen->clip_rect.h/2 + menus[0]->clip_rect.h; SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00)); SDL_Event event; while(true) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: for(int i=0;i=pos[1].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h) { if(!selected[i]) { selected[i]=1; SDL_FreeSurface(menus[i]); menus[i]=TTF_RenderText_Solid(font,labels[i],color[1]); } } else { if(selected[i]) { selected[i]=0; SDL_FreeSurface(menus[i]); menus[i]=TTF_RenderText_Solid(font,labels[i],color[0]); } } } break; case SDL_MOUSEBUTTONDOWN: x=event.button.x; y=event.button.y; for(int i=0;i=pos[i].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h) { for(int j=0;jformat,0xff,0xff,0xff)); Uint32 backround=SDL_MapRGB(screen->format,0x00,0xff,0xff); Uint32 color=SDL_MapRGB(screen->format,0x00,0x00,0xff); Uint32 color1=SDL_MapRGB(screen->format,0x90,0x20,0x20); 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); SDL_Rect player; instr.give(10,960,40,50,player); bool up=false; bool down=false; bool right=false; bool left=false; bool changeColor=false; bool up1=false; bool down1=false; bool right1=false; bool left1=false; bool changeColor1=false; int i; i=instr.showmenu(screen,font); if(i==1) run=false; SDL_Rect heroe_rect; instr.give(dstX1,dstY1,heroe->w,heroe->h,heroe_rect); 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; case SDLK_s: changeColor1=true; break; case SDLK_w: up1=true; down1=false; break; case SDLK_d: right1=true; break; case SDLK_a: left1=true; break; case SDLK_ESCAPE: int i=instr.showmenu(screen,font); if(i==1) run=false; 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; case SDLK_s: changeColor1=false; break; case SDLK_w: up1=false; down1=true; break; case SDLK_d: right1=false; break; case SDLK_a: left1=false; break; } break; } } if(changeColor==true) { backround=SDL_MapRGB(screen->format,0x80,0x60,0x60); } if(up==true) { player.y--; } if(down==true && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)) { player.y++; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) player.y-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) player.y-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) player.y-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) player.y-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true) player.y-=1; } if(right==true && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)) { player.x++; camera.x+=speed; if(camera.x>=6600) camera.x=0; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) player.x-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) player.x-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) player.x-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) player.x-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true) player.x-=1; } if(left==true && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) && !(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)) { player.x--; camera.x-=speed; if(camera.x<=0) camera.x=6600; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) player.x+=1; if( instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) player.x+=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) player.x+=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) player.x+=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true) player.x+=1; } ////////////////////////////// ////////////////////////////// if(player.x<0) { player.x=0; } if(player.y<0) { player.y=0; } ////////////////////////////// ////////////////////////////// if(player.x+player.w > ScreenWidth-2) { player.x=ScreenWidth-2-player.w; } if(player.y+player.h > ScreenHeight-2) { player.y=ScreenHeight-2-player.h; } if(player.y+player.h<900) { up=false; down=true; } /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// if(changeColor1==true) { backround=SDL_MapRGB(screen->format,0x80,0x80,0x20); } if(up1==true) { dstY1--; } if(down1==true) { dstY1++; } if(right1==true && !(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) && !(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) && !(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) && !(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) && !(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)) { dstX1++; camera.x+=speed; if(camera.x>=6600) camera.x=0; if(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true) dstX1-=1; if(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true) dstX1-=1; if(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true) dstX1-=1; if(instr.CheckCollision(heroe_rect.x,heroe_rect.y,heroe_rect.w,heroe_rect.h,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true) dstX1-=1; if(instr.CheckCollision(player.x,player.y,player.w,player.h,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true) dstX1-=1; } if(left1==true) { dstX1--; camera.x-=speed; if(camera.x<=0) camera.x=6600; } ///////////////////////// ///////////////////////// if(dstX1<0) { dstX1=0; } if(dstY1<0) { dstY1=0; } ///////////////////////// ///////////////////////// if(dstX1+heroe->w > ScreenWidth+80) { dstX1=ScreenWidth+80-heroe->w; } if(dstY1+heroe->h > ScreenHeight+100) { dstY1=ScreenHeight+100-heroe->h; } ///////////////////////////////// ///////////////////////////////// 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); SDL_FillRect(screen,&player,color1); instr.apply(srcX1, srcY1, dstX1, dstY1, heroe->w,heroe->h, heroe, screen); SDL_BlitSurface(text,NULL,screen,NULL); SDL_Flip(screen); } SDL_FreeSurface(text); TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); return 0; }