Advertisement
thecplusplusguy

Simple sidescroller game - enemy.cpp

Jul 14th, 2011
2,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 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 enemy.cpp a class (implementation) for the enemy.
  6. #include "enemy.h"
  7.  
  8. enemy::enemy(SDL_Surface* img,int x,int y,int xVel,int yVel)
  9. {
  10.     //set the variables, nothing new
  11.     image=img;
  12.     box.x=x;
  13.     box.y=y;
  14.     box.w=image->w/2;
  15.     box.h=image->h;
  16.     xvel=xVel;
  17.     yvel=yVel;
  18.     ground=0;   //is the enemy on the ground?
  19.     //we set the clips for the image, so we can do animation, by blittin first one part of the image, than the other one
  20.     for(int i=0;i<2;i++)
  21.     {
  22.         clips[i].x=i*40;
  23.         clips[i].y=0;
  24.         clips[i].w=clips[i].h=40;
  25.     }
  26.     frame=0.0;  //initialize frame is 0
  27. }
  28.  
  29. void enemy::show(SDL_Surface* screen)
  30. {
  31.     SDL_Rect tmp={box.x-coord.x,box.y,40,40};   //calculate the relative coordinate, so if the enemy at 120px coordinate and the camera
  32.     //at 100px coordinate, than we show it on 20px coordinate
  33.     SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&tmp);    //we just blit it. (int)(frame+0.5) is basically the round
  34. }
  35.  
  36. SDL_Rect* enemy::getRect()
  37. {
  38.     return &box;
  39. }
  40.  
  41. void enemy::move(std::vector<std::vector<int> >& map)
  42. {
  43.     //calculate the start and the end coordinate (see game.cpp)
  44.     int start=(baseclass::coord.x-(baseclass::coord.x%baseclass::TILE_SIZE))/baseclass::TILE_SIZE-2;
  45.     int end=(baseclass::coord.x+baseclass::coord.w+(baseclass::TILE_SIZE-(baseclass::coord.x+baseclass::coord.w)%baseclass::TILE_SIZE))/50+2;
  46.     if(start<0)
  47.         start=0;
  48.     if(end>map[0].size())
  49.         end=map[0].size();
  50.     bool nc=0;  //if this remains 0 until the end of the loops, than we fall (no ground under the enemy)
  51.     for(int i=0;i<map.size();i++)//go through the map
  52.         for(int j=start;j<end;j++)
  53.         {
  54.             if(map[i][j]==0)
  55.                 continue;
  56.             SDL_Rect destrect={j*50,i*50,50,50};
  57.             if(collision(&box,&destrect))   //if there was a collision
  58.             {
  59.                 if(destrect.y>=box.y+box.h-11)//and it'a the ground (so the tile is under the enemy)
  60.                 {
  61.                     ground=1;   //then we don't fall any more and we are on the ground
  62.                     yvel=0;
  63.                     nc=1;
  64.                 }else
  65.                     xvel=-xvel; //else change the direction in the x axis
  66.             }
  67.         }
  68.     if(!nc) //if there is no ground under the enemy
  69.         yvel=5; //than fall
  70.        
  71.  
  72.     box.x+=xvel;    //just move
  73.     box.y+=yvel;
  74.     frame+=0.1; //and animate
  75.     if(frame>=1.4)  //if we're at frame 3
  76.         frame=0.0;  //set back
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement