Kaktusen

Kaktus - getIt

Feb 20th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2.  
  3. #include "classes.h"
  4.  
  5. using namespace std;
  6.  
  7. bool gameLoop = true;
  8. int window_x = 640;
  9. int window_y = 480;
  10.  
  11. SDL_Surface *screen = SDL_SetVideoMode(window_x, window_y, 32, SDL_SWSURFACE);
  12. SDL_Surface *background = SDL_LoadBMP("images/background.bmp");
  13. SDL_Surface *tileset = SDL_LoadBMP("images/tileset.bmp");
  14.  
  15. int drawUnit(int x, int y, int i);
  16.  
  17. void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
  18.     SDL_Rect rect;
  19.  
  20.     rect.x = x;
  21.     rect.y = y;
  22.  
  23.     SDL_BlitSurface(source, NULL, destination, &rect);
  24. }
  25.  
  26. void load_sprite(int x, int y, SDL_Surface *source, SDL_Surface *destination, int bx, int by, int width, int height) {
  27.     SDL_Rect rect;
  28.     SDL_Rect offset;
  29.  
  30.     offset.x = x;
  31.     offset.y = y;
  32.  
  33.     rect.x = bx;
  34.     rect.y = by;
  35.     rect.w = width;
  36.     rect.h = height;
  37.  
  38.     SDL_BlitSurface(source, &rect, destination, &offset);
  39. }
  40.  
  41. int main(int argc, char *args[]) {
  42.     SDL_Init(SDL_INIT_EVERYTHING);
  43.     SDL_WM_SetCaption("Kaktus - getIt", NULL);
  44.  
  45.     Uint8 *key;
  46.  
  47.     Uint32 colorkey = SDL_MapRGB( tileset->format, 255, 0, 255);
  48.     SDL_SetColorKey( tileset, SDL_SRCCOLORKEY, colorkey );
  49.  
  50.     cPlayer.x = 32;
  51.     cPlayer.y = 0;
  52.     cPlayer.xspeed = 1;
  53.     cPlayer.yspeed = 1;
  54.  
  55.     cKey.x = 40;
  56.     cKey.y = 340;
  57.  
  58.     cCpu.unlock = false;
  59.  
  60.  
  61.     //gameloop
  62.     while(gameLoop) {
  63.         SDL_PumpEvents();
  64.  
  65.         key = SDL_GetKeyState(NULL);
  66.  
  67.         if(key[SDLK_DOWN] || key[SDLK_s]) {
  68.             cPlayer.y+=cPlayer.yspeed;
  69.         }
  70.  
  71.         if(key[SDLK_UP] || key[SDLK_w]) {
  72.             cPlayer.y-=cPlayer.yspeed;
  73.         }
  74.  
  75.         if(key[SDLK_RIGHT] || key[SDLK_d]) {
  76.             cPlayer.x+=cPlayer.xspeed;
  77.         }
  78.  
  79.         if(key[SDLK_LEFT] || key[SDLK_a]) {
  80.             cPlayer.x-=cPlayer.xspeed;
  81.         }
  82.  
  83.         if(key[SDLK_ESCAPE]) {
  84.             gameLoop = false;
  85.         }
  86.  
  87.         if(!cCpu.unlock) {
  88.             if((cPlayer.y + 32 > cKey.y && cPlayer.y < cKey.y + 32) || (cPlayer.y > cKey.y + 32 && cPlayer.y <cKey.y)) {
  89.                 if((cPlayer.x + 16 > cKey.x && cPlayer.x < cKey.x + 18) || (cPlayer.x < cKey.x + 18 && cPlayer.x + 16 > cKey.x)) {
  90.                     cKey.x = rand() % (640 - 16) + (0-16);
  91.                     cKey.y = rand() % 480 + (0+8);
  92.  
  93.                     cCpu.unlock = true;
  94.                 }
  95.             }
  96.         }
  97.  
  98.  
  99.         //if((cPlayer.x + 16 > 0 && cPlayer.x < (640-32)) || (cPlayer.x < (640-32) && cPlayer.x + 16 > 0));
  100.         if((cPlayer.x + 16 > 0 && cPlayer.x < (640-32)) || (cPlayer.x < (640-32) && cPlayer.x + 16 > 0)) {
  101.             if((cPlayer.y + 32 > (480/2) && cPlayer.y < (480/2) + 32)) {
  102.                 cPlayer.y++;
  103.             }
  104.         }
  105.  
  106.         if((cPlayer.x + 16 > 0 && cPlayer.x < (640-32)) || (cPlayer.x < (640-32) && cPlayer.x + 16 > 0)) {
  107.             if(cPlayer.y > (480/2) + 32 && cPlayer.y < (480/2)) {
  108.                 cPlayer.y++;
  109.             }
  110.         }
  111.  
  112.  
  113.         if(cPlayer.x < window_x-640) {
  114.             cPlayer.x++;
  115.         } else if(cPlayer.x + 16 > window_x) {
  116.             cPlayer.x--;
  117.         }
  118.  
  119.         if(cPlayer.y < window_y - 480) {
  120.             cPlayer.y++;
  121.         } else if(cPlayer.y + 32 > window_y) {
  122.             cPlayer.y--;
  123.         }
  124.  
  125.  
  126.  
  127.         apply_surface(0, 0, background, screen);
  128.  
  129.         if(!cCpu.unlock) {
  130.             drawUnit(0, 0, 2);
  131.             drawUnit(cKey.x, cKey.y, 4);
  132.         } else if(cCpu.unlock) {
  133.             drawUnit(0, 0, 3);
  134.         }
  135.  
  136.         for(int i = 0; i<(640/32); i++) {
  137.             drawUnit((i*31), (480/2), 5);
  138.         }
  139.  
  140.         drawUnit(cPlayer.x, cPlayer.y, 1);
  141.  
  142.  
  143.         SDL_Flip(screen);
  144.     }
  145.  
  146.  
  147.     SDL_FreeSurface(background);
  148.     SDL_FreeSurface(tileset);
  149.     SDL_Quit();
  150. }
  151.  
  152. int drawUnit(int x, int y, int i) {
  153.  
  154.     //player
  155.     if(i == 1) {
  156.         load_sprite(x, y, tileset, screen, 1, 1, 16, 32);
  157.     }
  158.  
  159.     //Locked
  160.     else if(i == 2) {
  161.         load_sprite(x, y, tileset, screen, 32+ 2, 1, 32, 32);
  162.     }
  163.  
  164.     //Unlocked
  165.     else if(i == 3) {
  166.         load_sprite(x, y, tileset, screen, 64+3, 1, 32, 32);
  167.     }
  168.  
  169.     //key
  170.     else if(i == 4) {
  171.         load_sprite(x, y, tileset, screen, 96+4, 1, 31, 18);
  172.     }
  173.  
  174.     //line
  175.     else if(i == 5) {
  176.         load_sprite(x, y, tileset, screen, (96+32)+5, 1, 32, 32);
  177.     }
  178.  
  179.     return i;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment