Advertisement
DarrenVortex

Ninja X Source Code

Jul 6th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <SDL/SDL_ttf.h>
  3. #include <SDL/SDL_image.h>
  4. #include <SDL/SDL_mixer.h>
  5. #include <string>
  6. #include <iostream>
  7. #include <sstream>
  8. #include "Timer.cpp"
  9.  
  10. using namespace std;
  11.  
  12. int SCREEN_WIDTH;
  13. int SCREEN_HEIGHT;
  14. int CAMERA_X = 0;
  15. const int FRAMES_PER_SECOND = 1000;
  16. const int SCENE_LIMIT = 2000;
  17. const int PLAYER_WIDTH = 50;
  18. const int PLAYER_HEIGHT = 50;
  19.  
  20. SDL_Surface *screen = NULL;
  21. SDL_Surface* player_standing = NULL;
  22. SDL_Surface* player_walking = NULL;
  23. SDL_Surface *message = NULL;
  24. SDL_Surface *background = NULL;
  25. SDL_Surface *ground = NULL;
  26.  
  27.  
  28. SDL_Rect grass_block;
  29. TTF_Font *font = NULL;
  30. Mix_Music *music = NULL;
  31. SDL_Event event;
  32. Timer fps;
  33. bool cap = true;
  34. bool quit = false;
  35. bool walking = false;
  36.  
  37. bool check_collision(SDL_Rect a, SDL_Rect b){
  38.     if( a.y + a.h <= b.y ) return false;
  39.     if( a.y >= b.y + b.h ) return false;
  40.     if( a.x + a.w <= b.x ) return false;
  41.     if( a.x >= b.x + b.w ) return false;
  42.     return true;
  43. }
  44.  
  45. void blit(int x, int y, SDL_Surface* source, SDL_Rect* clip = NULL, bool center = false){
  46.     SDL_Rect offset;
  47.     if(center) offset.x = x;
  48.     else offset.x = x - CAMERA_X;
  49.     offset.y = y;
  50.     SDL_BlitSurface(source,clip,screen,&offset);
  51. }
  52.  
  53. void blit_background(){
  54.     for(int x=0; x<SCENE_LIMIT; x+=1366){
  55.         blit(x,0,background);
  56.         blit(x,426,ground);
  57.     }
  58. }
  59.  
  60. string tostring(int i){
  61.     stringstream ss;
  62.     ss << i;
  63.     return ss.str();
  64. }
  65.  
  66.  
  67. class Player{
  68.    
  69.     private:
  70.     SDL_Rect player_obj;
  71.     int x_vel,y_vel;
  72.     static const int X_VELOCITY = 2;
  73.     static const int Y_VELOCITY = 2;
  74.     static const int STANDING = 0;
  75.     static const int WALKING = 1;
  76.     static const int JUMPING = 2;
  77.     static const int FALLING = 3;
  78.     static const int HITTING = 4;
  79.     static const int LEFT = 0;
  80.     static const int RIGHT = 1;
  81.     int status,direction,frame,offset,starttime;
  82.    
  83.     public:
  84.     Player(){
  85.         player_obj.x = SCREEN_WIDTH/2;
  86.         player_obj.y = 376;
  87.         status = STANDING;
  88.         direction = RIGHT;
  89.         frame = 0;
  90.         y_vel=x_vel=0;
  91.         starttime = SDL_GetTicks();
  92.     }
  93.    
  94.     void handle_input(){
  95.         if( event.type == SDL_KEYDOWN )
  96.         {
  97.             switch( event.key.keysym.sym )
  98.             {
  99.                 case SDLK_UP:
  100.                     if(player_obj.y >= 376){
  101.                         y_vel = -Y_VELOCITY;
  102.                         status = JUMPING;
  103.                     }
  104.                     break;
  105.                 case SDLK_LEFT:
  106.                     x_vel -= X_VELOCITY;
  107.                     status = WALKING;
  108.                     direction = LEFT;
  109.                     break;
  110.                 case SDLK_RIGHT:
  111.                     x_vel += X_VELOCITY;
  112.                     status = WALKING;
  113.                     direction = RIGHT;
  114.                     break;
  115.                 case SDLK_c:
  116.                     status = HITTING;
  117.                     break;
  118.             }
  119.         }
  120.         else if( event.type == SDL_KEYUP )
  121.         {          
  122.             switch( event.key.keysym.sym )
  123.             {
  124.                 case SDLK_LEFT:
  125.                     x_vel += X_VELOCITY;
  126.                     status = STANDING;
  127.                     direction = LEFT;
  128.                     break;
  129.                 case SDLK_RIGHT:
  130.                     x_vel -= X_VELOCITY;
  131.                     status = STANDING;
  132.                     direction = RIGHT;
  133.                     break;
  134.             }
  135.         }
  136.     }
  137.    
  138.     void move(){
  139.         if(player_obj.x == SCREEN_WIDTH/2){
  140.             CAMERA_X += x_vel;
  141.             if(CAMERA_X < 0 || CAMERA_X > SCENE_LIMIT){
  142.                 CAMERA_X -= x_vel;
  143.                 player_obj.x += x_vel;
  144.             }
  145.         }else{
  146.             player_obj.x += x_vel;
  147.             if(player_obj.x < 0 || player_obj.x + PLAYER_WIDTH > SCREEN_WIDTH || check_collision(player_obj,grass_block)) player_obj.x -= x_vel;
  148.         }
  149.        
  150.         player_obj.y += y_vel;
  151.         if(player_obj.y < 0 || player_obj.y + PLAYER_HEIGHT > SCREEN_HEIGHT || check_collision(player_obj,grass_block))
  152.             player_obj.y -= y_vel;
  153.            
  154.         if(player_obj.y < 326){
  155.             status = FALLING;
  156.             y_vel = Y_VELOCITY;
  157.         }else if(player_obj.y >= 376){
  158.             status = STANDING;
  159.             y_vel = 0;
  160.         }
  161.     }
  162.    
  163.     void show(){
  164.         if( x_vel < 0 ){ status = WALKING; direction = LEFT; }
  165.         else if( x_vel > 0 ){ status = WALKING; direction = RIGHT; }
  166.         else{ status = STANDING; }
  167.        
  168.         if(SDL_GetTicks() - starttime > 100){
  169.             starttime = SDL_GetTicks();
  170.             frame++;
  171.         }
  172.        
  173.         if( frame >= 4 ) frame = 0;
  174.         if(status==WALKING && direction == RIGHT){
  175.             SDL_Rect clip;
  176.             clip.x = frame%2 * 50;
  177.             clip.y = 0;
  178.             clip.h = 50;
  179.             clip.w = 50;
  180.             blit( this->get_xpos(), this->get_ypos(), player_walking, &clip, true);
  181.         }else if(status==WALKING && direction == LEFT){
  182.             SDL_Rect clip;
  183.             clip.x = frame%2 * 50;
  184.             clip.y = 50;
  185.             clip.h = 50;
  186.             clip.w = 50;
  187.             blit( this->get_xpos(), this->get_ypos(), player_walking, &clip, true);
  188.         }else if(status==STANDING && direction == RIGHT){
  189.             SDL_Rect clip;
  190.             clip.x = frame * 50;
  191.             clip.y = 0;
  192.             clip.h = 50;
  193.             clip.w = 50;
  194.             blit( this->get_xpos(), this->get_ypos(), player_standing, &clip, true);
  195.         }else if(status==STANDING && direction == LEFT){
  196.             SDL_Rect clip;
  197.             clip.x = frame * 50;
  198.             clip.y = 50;
  199.             clip.h = 50;
  200.             clip.w = 50;
  201.             blit( this->get_xpos(), this->get_ypos(), player_standing, &clip, true);
  202.         }else if(status==HITTING){
  203.            
  204.         }
  205.     }
  206.    
  207.     int get_xpos(){
  208.         return player_obj.x;
  209.     }
  210.    
  211.     int get_ypos(){
  212.         return player_obj.y;
  213.     }
  214.    
  215. };
  216.  
  217. int main(int argc, char* args[]){
  218.     SDL_Init(SDL_INIT_EVERYTHING);
  219.     TTF_Init();
  220.     Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
  221.     fps.start();
  222.     SDL_WM_SetCaption("Game!",NULL);
  223.        
  224.     const SDL_VideoInfo* info = SDL_GetVideoInfo();
  225.     SCREEN_WIDTH = info->current_w;
  226.     SCREEN_HEIGHT = info->current_h;
  227.     screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_FULLSCREEN);
  228.     player_walking = IMG_Load("../assets/ninja_walking.png");
  229.     player_standing = IMG_Load("../assets/ninja_standing.png");
  230.     font = TTF_OpenFont("../assets/arial.ttf",20);
  231.     music = Mix_LoadMUS("../assets/music.mp3");
  232.     background = IMG_Load("../assets/background.jpg");
  233.     ground = IMG_Load("../assets/ground.jpg");
  234.     Player player;
  235.    
  236.     while(!quit){
  237.         while(SDL_PollEvent(&event)){
  238.             if(event.type == SDL_QUIT) quit = true;
  239.             else if(event.type == SDL_KEYDOWN)
  240.                 if( event.key.keysym.sym == SDLK_RETURN ) cap = !cap;
  241.             player.handle_input();
  242.         }
  243.         player.move();
  244.         SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
  245.         blit_background();
  246.         player.show();
  247.         SDL_Flip(screen);
  248.         if( cap == true ) SDL_Delay(( 1000 / FRAMES_PER_SECOND ));
  249.     }
  250.    
  251.     SDL_FreeSurface(screen);
  252.     Mix_FreeMusic(music);
  253.     TTF_Quit();
  254.     SDL_Quit();
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement