Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_ttf.h>
  3.  
  4.  
  5. class instructions
  6. {
  7.  
  8. public:
  9.  
  10.  
  11. void apply(int x,int y,SDL_Surface *source,SDL_Surface *destination)
  12. {
  13.  
  14. SDL_Rect set;
  15. set.x=x;
  16. set.y=y;
  17.  
  18. SDL_BlitSurface(source,NULL,destination,&set);
  19.  
  20. }
  21.  
  22.  
  23.  
  24. void draw(int srcX,int srcY,int dstX,int dstY,int width,int height,
  25. SDL_Surface *source,SDL_Surface *destination)
  26. {
  27. SDL_Rect src;
  28. src.x=srcX;
  29. src.y=srcY;
  30. src.w=width;
  31. src.h=height;
  32.  
  33. SDL_Rect dst;
  34. dst.x=dstX;
  35. dst.y=dstY;
  36. dst.w=width;
  37. dst.h=height;
  38.  
  39.  
  40. SDL_BlitSurface(source,&src,destination,&dst);
  41.  
  42.  
  43. }
  44.  
  45.  
  46. void give(int x,int y,int w,int h,SDL_Rect &fake)
  47. {
  48. fake.x=x;
  49. fake.y=y;
  50. fake.w=w;
  51. fake.h=h;
  52. }
  53.  
  54.  
  55.  
  56.  
  57. bool CheckCollision(float Ax,float Ay,float Aw,float Ah,
  58. float Bx,float By,float Bw,float Bh)
  59.  
  60. {
  61.  
  62. if(Ay+Ah<By)
  63. return false;
  64.  
  65. else if(Ay>By+Bh)
  66. return false;
  67.  
  68. else if(Ax+Aw<Bx)
  69. return false;
  70.  
  71. else if(Ax>Bx+Bw)
  72. return false;
  73.  
  74.  
  75. return true;
  76.  
  77. }
  78.  
  79. };
  80.  
  81. SDL_Surface *screen=NULL;
  82.  
  83. SDL_Surface *heroe=NULL;
  84. SDL_Surface *imagebackground=NULL;
  85.  
  86.  
  87. SDL_Event event;
  88. bool run=true;
  89.  
  90.  
  91. int srcX1=0;
  92. int srcY1=0;
  93.  
  94. int dstX1=-80;
  95. int dstY1=820;
  96.  
  97. int width1=32;
  98. int height1=32;
  99.  
  100.  
  101.  
  102. int ScreenWidth=820;
  103. int ScreenHeight=850;
  104.  
  105.  
  106. int x;
  107. int y;
  108.  
  109.  
  110. int main(int argc, char** argv)
  111. {
  112.  
  113. SDL_Init(SDL_INIT_EVERYTHING);
  114.  
  115. SDL_WM_SetCaption("RGP game",NULL);
  116.  
  117. screen=SDL_SetVideoMode(1000,1000,32,SDL_SWSURFACE);
  118.  
  119.  
  120.  
  121. instructions instr;
  122.  
  123.  
  124. heroe=SDL_LoadBMP("heroe.bmp");
  125. imagebackground=SDL_LoadBMP("picturegame.bmp");
  126.  
  127. SDL_SetColorKey(heroe,SDL_SRCCOLORKEY,SDL_MapRGB(heroe->format,0xff,0xff,0xff));
  128.  
  129.  
  130. Uint32 backround=SDL_MapRGB(screen->format,0x00,0xff,0xff);
  131. Uint32 color=SDL_MapRGB(screen->format,0x00,0x00,0xff);
  132.  
  133.  
  134. SDL_Rect obstacle,obstacle1,obstacle2,obstacle3,obstacle4;
  135. instr.give(150,970,200,40,obstacle);
  136. instr.give(200,910,40,60,obstacle1);
  137. instr.give(500,930,40,70,obstacle2);
  138. instr.give(540,970,200,30,obstacle3);
  139. instr.give(720,930,40,70,obstacle4);
  140.  
  141. const int speed=9;
  142. SDL_Rect camera;
  143. instr.give(0,0,1000,1000,camera);
  144.  
  145.  
  146. bool up=false;
  147. bool down=false;
  148. bool right=false;
  149. bool left=false;
  150. bool changeColor=false;
  151.  
  152.  
  153.  
  154. while(run)
  155. {
  156. if(SDL_PollEvent(&event))
  157. {
  158. switch(event.type)
  159. {
  160. case SDL_QUIT:
  161. run=false;
  162. break;
  163.  
  164. case SDL_KEYDOWN:
  165. switch(event.key.keysym.sym)
  166. {
  167.  
  168. case SDLK_DOWN:
  169. changeColor=true;
  170. break;
  171.  
  172. case SDLK_UP:
  173. up=true;
  174. down=false;
  175. break;
  176.  
  177. case SDLK_RIGHT:
  178. right=true;
  179. break;
  180.  
  181. case SDLK_LEFT:
  182. left=true;
  183. break;
  184.  
  185. }
  186. break;
  187.  
  188. case SDL_KEYUP:
  189. switch(event.key.keysym.sym)
  190. {
  191.  
  192. case SDLK_DOWN:
  193. changeColor=false;
  194. break;
  195.  
  196. case SDLK_UP:
  197. up=false;
  198. down=true;
  199. break;
  200.  
  201. case SDLK_RIGHT:
  202. right=false;
  203. break;
  204.  
  205. case SDLK_LEFT:
  206. left=false;
  207. break;
  208.  
  209.  
  210. }
  211. break;
  212.  
  213.  
  214.  
  215. }
  216.  
  217.  
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224. if(changeColor==true)
  225. {
  226. backround=SDL_MapRGB(screen->format,0x60,0x60,0x60);
  227. }
  228.  
  229.  
  230.  
  231. if(up==true)
  232. {
  233. dstY1--;
  234. }
  235.  
  236.  
  237.  
  238. if(down==true)
  239. {
  240. dstY1++;
  241.  
  242. }
  243.  
  244.  
  245.  
  246. if(right==true && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
  247. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
  248. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
  249. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
  250. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true))
  251. {
  252. dstX1++;
  253.  
  254. x+=speed;
  255. camera.x+=speed;
  256. if(camera.x>=6600)
  257. camera.x=0;
  258.  
  259. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
  260. dstX1-=2;
  261.  
  262. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
  263. dstX1-=2;
  264.  
  265. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
  266. dstX1-=2;
  267.  
  268. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
  269. dstX1-=2;
  270.  
  271. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)
  272. dstX1-=2;
  273. }
  274.  
  275.  
  276.  
  277. if(left==true && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
  278. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
  279. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
  280. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
  281. && !(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true))
  282. {
  283. dstX1--;
  284.  
  285. x-=speed;
  286. camera.x-=speed;
  287. if(camera.x<=0)
  288. camera.x=6600;
  289.  
  290. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle.x,obstacle.y,obstacle.w,obstacle.h)==true)
  291. dstX1+=2;
  292.  
  293. if( instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle1.x,obstacle1.y,obstacle1.w,obstacle1.h)==true)
  294. dstX1+=2;
  295.  
  296. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle2.x,obstacle2.y,obstacle2.w,obstacle2.h)==true)
  297. dstX1+=2;
  298.  
  299. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle3.x,obstacle3.y,obstacle3.w,obstacle3.h)==true)
  300. dstX1+=2;
  301.  
  302. if(instr.CheckCollision(dstX1,dstY1,width1,height1,obstacle4.x,obstacle4.y,obstacle4.w,obstacle4.h)==true)
  303. dstX1+=2;
  304. }
  305.  
  306. //////////////////////////////
  307. //////////////////////////////
  308. if(dstX1<-130)
  309. {
  310. dstX1=-130;
  311. }
  312.  
  313. if(dstY1<-85)
  314. {
  315. dstY1=-85;
  316. }
  317.  
  318. //////////////////////////////
  319. //////////////////////////////
  320.  
  321. if(dstX1+width1 > ScreenWidth)
  322. {
  323. dstX1=ScreenWidth-width1;
  324.  
  325. }
  326.  
  327. if(dstY1+height1 > ScreenHeight)
  328. {
  329. dstY1=ScreenHeight-height1;
  330. }
  331.  
  332. ///////////////////////////////////
  333. ///////////////////////////////////
  334.  
  335.  
  336.  
  337.  
  338. SDL_FillRect(screen,&screen->clip_rect,backround);
  339.  
  340. SDL_BlitSurface(imagebackground,&camera,screen,NULL);
  341.  
  342. SDL_FillRect(screen,&obstacle,color);
  343. SDL_FillRect(screen,&obstacle1,color);
  344. SDL_FillRect(screen,&obstacle2,color);
  345. SDL_FillRect(screen,&obstacle3,color);
  346. SDL_FillRect(screen,&obstacle4,color);
  347.  
  348.  
  349.  
  350.  
  351.  
  352. instr.apply(dstX1,dstY1,heroe,screen);
  353.  
  354. instr.draw(srcX1,srcY1,dstX1,dstY1,width1,height1,heroe,screen);
  355.  
  356.  
  357.  
  358. SDL_Flip(screen);
  359. }
  360.  
  361.  
  362. SDL_Quit();
  363. return 0;
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement