Advertisement
Guest User

Game code

a guest
May 6th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.95 KB | None | 0 0
  1. #include"SDL/SDL.h"
  2. #include"SDL/SDL_ttf.h"
  3. #include"SDL/SDL_image.h"
  4. #include<sstream>
  5. #include"SDL/SDL_mixer.h"
  6. #include<string>
  7. #include<vector>
  8. #include<iostream>
  9.  
  10. //Dimensions
  11. const int SCREEN_WIDTH = 1000;
  12. const int SCREEN_HEIGHT = 450;
  13. const int BPP = 32;
  14.  
  15. //Screen, event and font
  16. SDL_Surface* screen = NULL;
  17. SDL_Event event;
  18. TTF_Font* font;
  19.  
  20. class projectile;
  21.  
  22. std::vector<projectile> allProjectiles;
  23.  
  24. //Background camera (allows me to change the position of the background)
  25. SDL_Rect backgroundCam = {0, 50, 1000, 500};
  26.  
  27. //Music
  28. Mix_Music* songs[1];
  29.  
  30. void handleMusic()
  31. {
  32.      static int time = SDL_GetTicks();
  33.      static int playing = 0;
  34.      
  35.      if( SDL_GetTicks() - time >= 100000 && (!playing) )
  36.      {
  37.          Mix_PlayMusic( songs[1], 0 );
  38.          time = SDL_GetTicks();
  39.          playing = 1;
  40.      }
  41.      
  42.      if( (SDL_GetTicks() - time >= 18000) && (playing) )
  43.      {
  44.          Mix_PlayMusic( songs[0], 0 );
  45.          time = SDL_GetTicks();
  46.          playing = 0;
  47.      }
  48. }
  49.  
  50. void blitSurface( int srcX, int srcY, SDL_Surface* source, SDL_Rect* clip = NULL )
  51. {
  52.      
  53.      SDL_Rect offsets;
  54.      offsets.x = srcX;
  55.      offsets.y = srcY;
  56.      
  57.      SDL_BlitSurface( source, clip, screen, &offsets );
  58. }
  59.  
  60. int init( std::string caption )
  61. {
  62.     if( SDL_Init(SDL_INIT_EVERYTHING) == -1 )
  63.     {
  64.         return -1;
  65.     }
  66.  
  67.     if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
  68.     {
  69.         return -1;
  70.     }
  71.    
  72.     if( TTF_Init() == -1 )
  73.     {
  74.         return -1;
  75.     }
  76.    
  77.     if( (screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_SWSURFACE )) == NULL )
  78.     {
  79.         return -1;
  80.     }
  81.    
  82.     SDL_WM_SetCaption( caption.c_str(), NULL );
  83.    
  84.     return 0;
  85. }
  86.  
  87. void clearUp()
  88. {
  89.      TTF_CloseFont( font );
  90.      Mix_FreeMusic( songs[0] );
  91.      Mix_FreeMusic( songs[1] );
  92.      
  93.      SDL_Quit();
  94.      TTF_Quit();
  95.      Mix_CloseAudio();
  96. }
  97.  
  98. void clearScreen()
  99. {
  100.       SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255 ) );
  101. }
  102.  
  103. SDL_Surface* loadImage( std::string path )
  104. {                
  105.       SDL_Surface* loadedImage = NULL;
  106.      
  107.       SDL_Surface* optimizedImage = NULL;
  108.      
  109.       loadedImage = IMG_Load( path.c_str() );
  110.      
  111.       if( loadedImage != NULL )
  112.       {
  113.           optimizedImage = SDL_DisplayFormat( loadedImage );
  114.           SDL_FreeSurface( loadedImage );
  115.       }
  116.      
  117.        if( optimizedImage != NULL )
  118.        {
  119.            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 0, 255 );
  120.            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
  121.        }
  122.        
  123.        return optimizedImage;
  124. }
  125.  
  126. int checkCollison( SDL_Rect* rect1, SDL_Rect* rect2 )
  127. {
  128.      if(rect1->y >= rect2->y + rect2->h)
  129.         return 0;
  130.                
  131.      if(rect1->x >= rect2->x + rect2->w)
  132.         return 0;
  133.                
  134.      if(rect1->y + rect1->h <= rect2->y)
  135.         return 0;
  136.                
  137.      if(rect1->x + rect1->w <= rect2->x)
  138.         return 0;
  139.                
  140.      return 1;                    
  141. }
  142.  
  143. void displayTTF( std::string text, int x, int y, int red = 0, int green = 0, int blue = 0 )
  144. {
  145.      
  146.      SDL_Color colour = { red, green, blue };
  147.      SDL_Surface* temp = TTF_RenderText_Solid( font, text.c_str(), colour );
  148.      blitSurface( x, y, temp );
  149.      
  150.      SDL_FreeSurface( temp );
  151.      temp = NULL;
  152. }
  153.  
  154. void displayTTF( int num, int x, int y, int red = 0, int green = 0, int blue = 0 )
  155. {
  156.       SDL_Color colour = { red, green, blue };
  157.      
  158.       std::stringstream stream;
  159.       stream << num;
  160.      
  161.       SDL_Surface* temp = TTF_RenderText_Solid( font, stream.str().c_str(), colour );
  162.      
  163.       blitSurface( x, y, temp );
  164.      
  165.       SDL_FreeSurface( temp );
  166.       temp = NULL;
  167. }
  168.  
  169. class projectile
  170. {
  171.       public:
  172.      
  173.       int x, y, w, h, velocity;
  174.      
  175.       std::string filePath;
  176.      
  177.       SDL_Surface* sprite;
  178.              
  179.       projectile( int X, int Y, int VEL, int W, int H, std::string path ): x(X), y(Y), w(W), h(H), velocity(VEL), filePath(path) { sprite = loadImage( path.c_str() ); }
  180.        
  181.       projectile( const projectile& proj )
  182.       {
  183.                   this->x = proj.x;
  184.                   this->y = proj.y;
  185.                   this->w = proj.w;
  186.                   this->h = proj.h;
  187.                   this->velocity = proj.velocity;
  188.                   this->filePath = proj.filePath;
  189.                  
  190.                   this->sprite = loadImage( filePath.c_str() );
  191.       }
  192.      
  193.      ~projectile(){ SDL_FreeSurface(sprite); }
  194.      
  195.       void show(){ blitSurface( x, y, sprite ); }
  196. };
  197.  
  198. projectile spawnProjectile( int x, int y, int w, int h, int velocity, std::string path )
  199. {
  200.            projectile proj( x, y, velocity, w, h, path );
  201.            return proj;
  202. }
  203.  
  204. class player;
  205.  
  206. class enemy
  207. {
  208.       protected:
  209.      
  210.       SDL_Surface* spriteSheet;
  211.      
  212.       SDL_Rect walk_left[3];
  213.      
  214.       int x, y, w, h, xVel, hitDamage, frame;      
  215.      
  216.       virtual void setClips();  
  217.      
  218.       void handle_animation();
  219.      
  220.       public:
  221.      
  222.       enemy( std::string path, int X, int Y, int W, int H, int damage = 5, int VEL = -2 );
  223.       ~enemy();
  224.      
  225.       void move();
  226.      
  227.       void show();
  228.      
  229.       void shootBullet();
  230.      
  231.       int getDamage() const { return hitDamage; }
  232.      
  233.       SDL_Rect getStats();
  234. };
  235.  
  236. void enemy::shootBullet()
  237. {
  238.      static int counter = SDL_GetTicks();
  239.      
  240.      if( SDL_GetTicks() - counter >= 1000 )
  241.      {
  242.          allProjectiles.push_back( spawnProjectile( x - 10, y, 10, 10, -3, "Lazer.png" ) );
  243.          counter = SDL_GetTicks();
  244.      }
  245. }
  246.  
  247. SDL_Rect enemy::getStats()
  248. {
  249.          SDL_Rect temp = {x, y, w, h};
  250.          return temp;
  251. }
  252.  
  253. void enemy::show()
  254. {
  255.      blitSurface( x, y, spriteSheet, &walk_left[frame] );
  256. }
  257.  
  258. void enemy::setClips()
  259. {
  260. }
  261.  
  262. void enemy::move()
  263. {
  264.      x += xVel;
  265.      
  266.      if( x + w < 0 )
  267.      {
  268.          x = SCREEN_WIDTH;
  269.          y = rand() % SCREEN_HEIGHT;
  270.      }
  271.      
  272.      handle_animation();
  273. }
  274.  
  275. void enemy::handle_animation()
  276. {
  277.      frame  = (SDL_GetTicks() / 150) % 3;
  278.      
  279.      if( frame > 3 )
  280.      {
  281.          frame = 0;
  282.      }
  283. }
  284.  
  285. enemy::enemy( std::string path, int X, int Y, int W, int H, int damage, int VEL ):
  286.  
  287. x(X),
  288. y(Y),
  289. w(W),
  290. h(H),
  291. xVel(VEL),
  292. hitDamage(damage),
  293. frame(0)
  294.  
  295. {
  296.          setClips();
  297.          spriteSheet = loadImage( path.c_str() );
  298. }
  299.  
  300. enemy::~enemy()
  301. {
  302.                SDL_FreeSurface( spriteSheet );
  303. }
  304.  
  305.  
  306. class bunny : public enemy
  307. {
  308.       private:
  309.              
  310.      void setClips();
  311.      
  312.       public:
  313.      
  314.       bunny( std::string path, int X, int Y, int W, int H, int damage, int VEL ):
  315.              enemy::enemy( path, X, Y, W, H, damage, VEL )
  316.       {
  317.          setClips();    
  318.       }
  319. };
  320.  
  321. void bunny::setClips()
  322. {
  323.      walk_left[0].x = 0;
  324.      walk_left[0].y = 0;
  325.      walk_left[0].w = 39;
  326.      walk_left[0].h = 44;
  327.      
  328.      walk_left[1].x = 43;
  329.      walk_left[1].y = 2;
  330.      walk_left[1].w = 39;
  331.      walk_left[1].h = 44;
  332.      
  333.      walk_left[2].x = 87;
  334.      walk_left[2].y = 5;
  335.      walk_left[2].w = 39;
  336.      walk_left[2].h = 44;
  337. }
  338.  
  339. bunny createBunny( int x, int y )
  340. {
  341.       bunny bunny( "Bunny.png", x, y, 39, 44, 5, -1 );
  342.       return bunny;
  343. }
  344.  
  345. class player
  346. {
  347.       private:
  348.      
  349.       int x, y, w, h, yVel;
  350.      
  351.       SDL_Surface* sprite;
  352.      
  353.       public:
  354.      
  355.       void move();
  356.      
  357.       void show();
  358.      
  359.       void handle_events();
  360.      
  361.       player( int X, int Y, int W, int H, std::string path );
  362.      
  363.       ~player();
  364. };
  365.  
  366. player::player( int X, int Y, int W, int H, std::string path ):
  367.  
  368. x(X),
  369. y(W),
  370. w(W),
  371. h(H),
  372. yVel(0)
  373.  
  374. {
  375.        sprite = loadImage( path.c_str() );
  376. }
  377.  
  378. player::~player()
  379. {
  380.                  SDL_FreeSurface( sprite );
  381. }
  382.  
  383. void player::handle_events()
  384. {
  385.      
  386.      if( event.type == SDL_KEYDOWN )
  387.      {
  388.          switch( event.key.keysym.sym )
  389.          {
  390.                  case(SDLK_w): yVel-= 3; break;
  391.                  case(SDLK_s): yVel+= 3; break;
  392.          }
  393.      }
  394.      
  395.      if( event.type == SDL_KEYUP )
  396.      {
  397.          switch( event.key.keysym.sym )
  398.          {
  399.                  case(SDLK_w): yVel+= 3; break;
  400.                  case(SDLK_s): yVel-= 3; break;
  401.          }
  402.      }
  403. }
  404.  
  405. void player::move()
  406. {  
  407.      y += yVel;
  408.      
  409.      if( (y + h) > SCREEN_HEIGHT - 10 )
  410.      {
  411.          y -= yVel;
  412.          backgroundCam.y++;
  413.      }
  414.  
  415.      if( backgroundCam.y + SCREEN_HEIGHT > backgroundCam.h )
  416.      {
  417.          backgroundCam.y--;
  418.      }
  419.      
  420.      if( y < 10 )
  421.      {
  422.          y -= yVel;
  423.          backgroundCam.y-=2;
  424.      }
  425.      
  426.      if( backgroundCam.y < 0 )
  427.      {
  428.          backgroundCam.y+=2;
  429.      }
  430.      
  431. }
  432.  
  433. void player::show()
  434. {
  435.      SDL_Rect clip;
  436.      clip.x = 385;
  437.      clip.y = 11;
  438.      
  439.      blitSurface( x, y, sprite, &clip );
  440. }
  441. //THIS DELETES THE REDUNDANT BULLETS
  442. void handleProjectiles()
  443. {
  444.    if( !allProjectiles.empty() )
  445.    {  
  446.       std::vector<projectile>::iterator it = allProjectiles.begin();
  447.      
  448.       for( ; it != allProjectiles.end(); it++ )
  449.       {
  450.            it->x += it->velocity;    
  451.            it->show();
  452.            
  453.            if( it->x + it->w < 0 )
  454.            {
  455.                allProjectiles.erase( allProjectiles.begin(), it );
  456.            }
  457.       }
  458.    }
  459. }
  460.  
  461. int main( int argv, char* argc[] )
  462. {
  463.        //Initialisation stage
  464.        init( "Space Game!" );
  465.        
  466.        font = TTF_OpenFont( "arial.ttf", 15 );
  467.              
  468.        songs[0] = Mix_LoadMUS( "music.ogg" );
  469.        songs[1] = Mix_LoadMUS( "music3.ogg" );
  470.              
  471.        Mix_PlayMusic( songs[0], 0 );
  472.        Mix_VolumeMusic( 20 );
  473.        
  474.        //Determines whether the game will run
  475.        bool quit = false;
  476.        
  477.        player player(  50, 0, 153, 36, "Superman.png" );
  478.            
  479.        SDL_Surface* background = loadImage( "Background.JPG" );
  480.        
  481.        bunny bun = createBunny( 800, 20 );
  482.        
  483.        while( !quit )
  484.        {
  485.               //Event handling
  486.               while( SDL_PollEvent(&event) )
  487.               {
  488.                      if( event.type == SDL_QUIT )
  489.                      {
  490.                          quit = true;
  491.                      }
  492.                      
  493.                      player.handle_events();
  494.               }
  495.              
  496.               player.move();
  497.               bun.move();
  498.              
  499.               //Clears the previous frame
  500.               clearScreen();
  501.              
  502.               //Rendering
  503.               //blitSurface( 0, 0, background, &backgroundCam );
  504.               player.show();
  505.               bun.show();
  506.               bun.shootBullet();
  507.               handleProjectiles();
  508.              
  509.               SDL_Flip( screen );
  510.               SDL_Delay(20);
  511.              
  512.               handleMusic();
  513.        }
  514.        
  515.        SDL_FreeSurface( background );
  516.        
  517.        clearUp();
  518.        
  519.        return 0;
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement