Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

SpaceZombies!

By: a guest on Nov 9th, 2012  |  syntax: C++  |  size: 7.81 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<string>
  2. #include<SDL.h>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. //declare surfaces.
  8. SDL_Surface* floor = NULL; //background tile.
  9. SDL_Surface* player = NULL; //player tile
  10. SDL_Surface* zombie = NULL; //enemy tile
  11. SDL_Surface* colonist = NULL; //friendly tile
  12. SDL_Surface* screen = NULL; //display layer.
  13.  
  14. //declare attributes of the screen
  15. const int SCREEN_HEIGHT = 500;
  16. const int SCREEN_WIDTH = 1000;
  17. const int SCREEN_BPP = 32;
  18.  
  19. bool quit;
  20.  
  21. //array containing the coordinates for all entities.
  22. SDL_Rect object[50];
  23. //array containing type of unit
  24. int type[50]; // player = 1, colonists = 2, zombies = 3
  25.  
  26. //amount of baddies
  27. int enemyAmount = 4;
  28. //whether colliding
  29. bool isCol[50];
  30.  
  31. int amount;
  32. bool collide;
  33.  
  34.  
  35.  
  36. //event
  37. SDL_Event move;
  38.  
  39. //function for conversion of 24 bit bmps into 32 bit bmps
  40. SDL_Surface *load_image( std::string filename ) //load bmp
  41. {
  42.     //loaded 24 bit bmp is placed here:
  43.     SDL_Surface* loadedImage = NULL;
  44.     //optimized 32 bit bmp is placed here:
  45.     SDL_Surface* optimizedImage = NULL;
  46.  
  47.     //the bmp is loaded as "filename"
  48.     loadedImage = SDL_LoadBMP( filename.c_str() );
  49.    
  50.     //if the image loads without errors:
  51.     if( loadedImage != NULL )
  52.     {
  53.         //convert to 32 bit bmp
  54.         optimizedImage = SDL_DisplayFormat( loadedImage );
  55.         //clear RAM of 24 bit image
  56.         SDL_FreeSurface( loadedImage );
  57.     }
  58.    
  59.     if( optimizedImage != NULL )
  60.     {
  61.         //Map the color key
  62.         Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );
  63.        
  64.         //Set all pixels of colour transparent
  65.         SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
  66.     }
  67.    
  68.     //send the new 32 bit file back to the program
  69.     return optimizedImage;
  70. }
  71.  
  72. //Function to blit the surface
  73. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
  74. {
  75.     //temporary rectangle is created to hold offset values.
  76.     //SDL_Rect declares a rectangle with four attributes: offsets( x, y ) and dimensions( h, w )
  77.     SDL_Rect offset;    
  78.     //declare rectangle offsets:
  79.     offset.x = x;
  80.     offset.y = y;
  81.    
  82.     //blit the surface. ( surface used in function, ?, surface that is blitted to, offsets )
  83.     SDL_BlitSurface ( source, NULL, destination, &offset );    
  84. }
  85.  
  86. void drawFloor()
  87. {
  88.     int n = 1;
  89.     int nl = 1;
  90.     int xl = 20;
  91.     int yl = 40;
  92.  
  93.        while( nl < 23 )
  94.        {
  95.            if( n == 37 )
  96.            {
  97.                 xl = 20;
  98.                 yl += 20;
  99.                 n = 1;
  100.                 nl++;
  101.            }
  102.            else
  103.            {
  104.                 apply_surface( xl, yl, floor, screen );
  105.                 n++;                
  106.                 xl += 20;              
  107.            }          
  108.  
  109.        }
  110.  
  111. }
  112.  
  113. bool start()
  114. {
  115.     //Start SDL and end program if errors are encountered.
  116.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1)
  117.     {
  118.         return -1;
  119.     }
  120.    
  121.     //sets up screen, ends program if issues.
  122.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  123.     if( screen == NULL )
  124.     {
  125.         return -1;
  126.     }
  127.    
  128.     //Title of the window
  129.     SDL_WM_SetCaption( "ROOM MOVER", NULL );
  130.    
  131.    
  132.     //start position
  133.     object[1].x = 240;
  134.     object[1].y = 240;
  135.     //declare player as player
  136.     type[1] = 3;
  137.    
  138.     //test sci
  139.     object[2].x = 240;
  140.     object[2].y = 100;
  141.     type[2] = 2;
  142.    
  143.     //test zamb
  144.     object[3].x = 240;
  145.     object[3].y = 340;
  146.     type[3] = 1;
  147.    
  148.     return true;
  149. }
  150.  
  151. bool stop()
  152. {    
  153.     //clear the RAM
  154.     SDL_FreeSurface( floor );    
  155.    
  156.     //end SDL
  157.     SDL_Quit();
  158.     //end program
  159.     return 0;
  160.    
  161. }
  162.  
  163. //function that blits the objects onto the screen
  164. int drawGuys()
  165. {
  166.  
  167.     int objCount = 1;
  168.    
  169.     while( objCount < enemyAmount )
  170.     {
  171.         if( type[objCount] == 1 ) //if its the player
  172.         {
  173.         apply_surface( object[objCount].x, object[objCount].y, player, screen ); //draw as player
  174.         objCount ++;//check next thing
  175.         }
  176.         else if( type[objCount] == 2 )  //if its a colonist
  177.         {
  178.         apply_surface( object[objCount].x, object[objCount].y, colonist, screen ); //draw as colonist
  179.         objCount ++;//check next thing
  180.         }
  181.         else if( type[objCount] == 3 )  //if its a zombie
  182.         {
  183.         apply_surface( object[objCount].x, object[objCount].y, zombie, screen ); //draw as zombie
  184.         objCount ++;//check next thing
  185.         }
  186.         else  //if its none of the above
  187.         {
  188.         objCount ++;//check next thing
  189.         }
  190.        
  191.     }
  192.  
  193. }
  194.  
  195.  
  196. int draw()
  197. {
  198.     drawFloor();
  199.     drawGuys();
  200.     SDL_Flip( screen );    
  201. }
  202.  
  203. //checks two objects colliding and determines zombification and game loss.
  204. int actCollision( SDL_Rect offender, SDL_Rect defender, int offenderType, int defenderType )
  205. {
  206.  
  207.     //if the one moving into the space is a zombie
  208.     if ( offender.y == defender.y && offender.x == defender.x && offenderType == 3 && defenderType == 2 )
  209.     {
  210.         defenderType = 3;// the one occupying the space is now a zombie
  211.         return (defenderType);
  212.     }
  213.     // if player
  214.     else if ( offender.y == defender.y && offender.x == defender.x && offenderType == 3 && defenderType == 1 )
  215.     {
  216.         stop(); // game over
  217.     }
  218.  
  219. }
  220.  
  221. int checkCollision()
  222. {
  223.     int compare1 = 1;
  224.     int compare2 = 1;
  225.    
  226.     while( compare1 < 49 )
  227.     {
  228.         if( compare2 == 49 )
  229.         {
  230.             compare1 ++;
  231.             compare2 = 1;
  232.         }
  233.         else
  234.         {
  235.         if( compare1 != compare2 )
  236.         {
  237.             actCollision( object[compare1], object[compare2], type[compare1], type[compare2] );
  238.             compare2 ++;
  239.         }
  240.         else
  241.         {
  242.             compare2 ++;
  243.         }
  244.         }
  245.     }
  246.  
  247. }
  248.  
  249.  
  250.  
  251. int main( int argc, char* args[] )
  252. {
  253.     start();
  254.  
  255.    
  256.     //Load Images to surfaces
  257.     floor = load_image( "tile.bmp" );
  258.     player = load_image( "man.bmp" );
  259.     colonist = load_image( "sci.bmp" );
  260.     zombie = load_image( "zomb.bmp" );
  261.    
  262.  
  263.    
  264.     while( quit == false )        
  265.         {
  266.                 while( SDL_PollEvent( &move ) )
  267.                 {
  268.            
  269.                         if (move.type == SDL_KEYDOWN)
  270.                         {
  271.                
  272.                                 if (move.key.keysym.sym == SDLK_w)
  273.                                 {
  274.                                     object[1].y -= 20;
  275.                                     checkCollision();
  276.                                     draw();
  277.  
  278.                                 }
  279.                                 else if (move.key.keysym.sym == SDLK_s)
  280.                                 {
  281.                                     object[1].y += 20;
  282.                                     checkCollision();
  283.                                     draw();
  284.  
  285.                                 }
  286.                                 else if (move.key.keysym.sym == SDLK_d)
  287.                                 {
  288.                                     object[1].x += 20;
  289.                                     checkCollision();
  290.                                     draw();
  291.  
  292.                                 }
  293.                                 else if (move.key.keysym.sym == SDLK_a)
  294.                                 {
  295.                                     object[1].x -= 20;
  296.                                     checkCollision();
  297.                                     draw();
  298.  
  299.                                 }
  300.                         }
  301.                             //looks for the 'x' of the window to be closed, the acts accordingly
  302.  
  303.                         else if (move.type == SDL_QUIT)
  304.                         {
  305.                                     quit = true;
  306.                         }
  307.                 }
  308.         }
  309.    
  310.     stop();
  311.    
  312. }
clone this paste RAW Paste Data