Advertisement
thecplusplusguy

Simple sidescroller game - player.cpp

Jul 14th, 2011
2,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. //This example program is created by thecplusplusuy for demonstration purposes. It's a simple mario like side-scroller game:
  2. //http://www.youtube.com/user/thecplusplusguy
  3. //Free source, modify if you want, LGPL licence (I guess), I would be happy, if you would not delete the link
  4. //so other people can see the tutorial
  5. //this file is player.cpp Our player
  6.  
  7. #include "player.h"
  8.  
  9. player::player(SDL_Surface* img)
  10. {
  11.     //initialize a few variables, nothin new
  12.     image=img;
  13.     box.x=0;
  14.     box.y=0;
  15.     box.w=50;
  16.     box.h=100;
  17.     xvel=0;
  18.     yvel=0;
  19.     //set the clips for the animation
  20.     for(int i=0;i<4;i++)
  21.     {
  22.         clips[i].x=i*50;
  23.         clips[i].y=0;
  24.         clips[i].w=clips[i].h=50;
  25.     }
  26.     ground=0;
  27.     jump=0;
  28.     direction='r';
  29.     frame=0.0;
  30.     moving=0;
  31.     health=10;
  32. }
  33.  
  34. player::~player()
  35. {
  36.     SDL_FreeSurface(image); //free the image
  37. }
  38.  
  39. SDL_Rect* player::getRect()
  40. {
  41.     return &box;
  42. }
  43.  
  44. int player::getXvel()
  45. {
  46.     return xvel;
  47. }
  48.  
  49. void player::setXvel(int vel)
  50. {
  51.     xvel=vel;
  52. }
  53.  
  54. void player::show(SDL_Surface* screen)
  55. {
  56.     SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&box); //show the current frame at the current position
  57. }
  58.  
  59.  
  60. void player::setMoving(bool b)
  61. {
  62.     moving=b;
  63. }
  64.  
  65. void player::setDirection(char c)
  66. {
  67.     if((c=='r' || c=='l') && direction!=c)
  68.     {
  69.         direction=c;
  70.         if(direction=='r') //if we are going right than we start with the 0.0 image
  71.             frame=0.0;
  72.         else
  73.             frame=1.6;//else with the 1.6 image
  74.     }
  75. }
  76.  
  77.  
  78. void player::move(const std::vector<std::vector<int> >& map)
  79. {
  80.     //calculate the start and the end (see enemy.cpp)
  81.     int start=(baseclass::coord.x-(baseclass::coord.x%baseclass::TILE_SIZE))/baseclass::TILE_SIZE;
  82.     int end=(baseclass::coord.x+baseclass::coord.w+(baseclass::TILE_SIZE-(baseclass::coord.x+baseclass::coord.w)%baseclass::TILE_SIZE))/50;
  83.     if(start<0)
  84.         start=0;
  85.     if(end>map[0].size())
  86.         end=map[0].size();
  87.     bool nc=0;  //if nc remains 0 then there are no ground under us
  88.     for(int i=0;i<map.size();i++)
  89.         for(int j=start;j<end;j++)
  90.         {
  91.             if(map[i][j]==0)
  92.                 continue;
  93.             SDL_Rect destrect={j*50-baseclass::coord.x,i*50,50,50}; //calculate relative coordinates (see game.cpp)
  94.             if(collision(&box,&destrect))   //if we collide with a tile
  95.             {
  96.                 if(destrect.y>=box.y+box.h-11)  //if this tile is under us
  97.                 {
  98.                     ground=1;   //we are not falling anymore
  99.                     yvel=0;
  100.                     nc=1;
  101.                 }else if(destrect.y+destrect.h<=box.y+11)   //if this tile is above us
  102.                 {
  103.                     box.y++;    //we set the y+1 so we don't stuck
  104.                     yvel=5;     //and we are falling
  105.                 }
  106.                 if(box.x+box.w>=destrect.x-5 && box.y+box.h>=destrect.y+6 && box.x+box.w<=destrect.x+20)    //if the tile is righter then us
  107.                 {
  108.                     xvel=0; //set our velocity to 0
  109.                     box.x--;    //and x+1, so we don't stuck
  110.                 }else if(box.x<=destrect.x+destrect.w && box.y+box.h>=destrect.y+6) //if the tile us lefter then us
  111.                 {
  112.                     xvel=0; //do the same
  113.                     box.x++;
  114.                 }
  115.             }
  116.         }
  117.     if(!nc && !jump)    //if we are not jumpin and we don't have ground under our leg
  118.     {
  119.         yvel=5; //then we fall
  120.     }
  121.     if(jump && yvel<5)  //if we are jumping and our yvel is less than 5 we increase the yvel
  122.         yvel++;
  123.     else
  124.         jump=0; //else we are not jumping
  125.        
  126.     //move
  127.     box.x+=xvel;
  128.     box.y+=yvel;
  129.     if(moving)  //if we are moving
  130.     {
  131.         frame+=0.2; //then animate
  132.         if(direction=='r' && frame >=1.4)
  133.             frame=0.0;
  134.         else if(direction=='l' && frame >=3.4)
  135.             frame=1.5;
  136.     }
  137. }
  138.  
  139.  
  140. void player::setJump()  //set the player to jump
  141. {
  142.     if(ground && !jump) //if we are on the ground and we are not jumping
  143.     {
  144.         jump=1; //jump
  145.         ground=0;
  146.         yvel=-17;   //starting speed (high)
  147.         box.y-=5;   //we don't stuck
  148.     }
  149. }
  150.  
  151. int player::getHealth()
  152. {
  153.     return health;
  154. }
  155.  
  156. void player::setHealth(int h)
  157. {
  158.     health=h;
  159. }
  160.  
  161. char player::getDirection()
  162. {
  163.     return direction;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement