Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This example program is created by thecplusplusuy for demonstration purposes. It's a simple mario like side-scroller game:
- //http://www.youtube.com/user/thecplusplusguy
- //Free source, modify if you want, LGPL licence (I guess), I would be happy, if you would not delete the link
- //so other people can see the tutorial
- //this file is player.cpp Our player
- #include "player.h"
- player::player(SDL_Surface* img)
- {
- //initialize a few variables, nothin new
- image=img;
- box.x=0;
- box.y=0;
- box.w=50;
- box.h=100;
- xvel=0;
- yvel=0;
- //set the clips for the animation
- for(int i=0;i<4;i++)
- {
- clips[i].x=i*50;
- clips[i].y=0;
- clips[i].w=clips[i].h=50;
- }
- ground=0;
- jump=0;
- direction='r';
- frame=0.0;
- moving=0;
- health=10;
- }
- player::~player()
- {
- SDL_FreeSurface(image); //free the image
- }
- SDL_Rect* player::getRect()
- {
- return &box;
- }
- int player::getXvel()
- {
- return xvel;
- }
- void player::setXvel(int vel)
- {
- xvel=vel;
- }
- void player::show(SDL_Surface* screen)
- {
- SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&box); //show the current frame at the current position
- }
- void player::setMoving(bool b)
- {
- moving=b;
- }
- void player::setDirection(char c)
- {
- if((c=='r' || c=='l') && direction!=c)
- {
- direction=c;
- if(direction=='r') //if we are going right than we start with the 0.0 image
- frame=0.0;
- else
- frame=1.6;//else with the 1.6 image
- }
- }
- void player::move(const std::vector<std::vector<int> >& map)
- {
- //calculate the start and the end (see enemy.cpp)
- int start=(baseclass::coord.x-(baseclass::coord.x%baseclass::TILE_SIZE))/baseclass::TILE_SIZE;
- int end=(baseclass::coord.x+baseclass::coord.w+(baseclass::TILE_SIZE-(baseclass::coord.x+baseclass::coord.w)%baseclass::TILE_SIZE))/50;
- if(start<0)
- start=0;
- if(end>map[0].size())
- end=map[0].size();
- bool nc=0; //if nc remains 0 then there are no ground under us
- for(int i=0;i<map.size();i++)
- for(int j=start;j<end;j++)
- {
- if(map[i][j]==0)
- continue;
- SDL_Rect destrect={j*50-baseclass::coord.x,i*50,50,50}; //calculate relative coordinates (see game.cpp)
- if(collision(&box,&destrect)) //if we collide with a tile
- {
- if(destrect.y>=box.y+box.h-11) //if this tile is under us
- {
- ground=1; //we are not falling anymore
- yvel=0;
- nc=1;
- }else if(destrect.y+destrect.h<=box.y+11) //if this tile is above us
- {
- box.y++; //we set the y+1 so we don't stuck
- yvel=5; //and we are falling
- }
- 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
- {
- xvel=0; //set our velocity to 0
- box.x--; //and x+1, so we don't stuck
- }else if(box.x<=destrect.x+destrect.w && box.y+box.h>=destrect.y+6) //if the tile us lefter then us
- {
- xvel=0; //do the same
- box.x++;
- }
- }
- }
- if(!nc && !jump) //if we are not jumpin and we don't have ground under our leg
- {
- yvel=5; //then we fall
- }
- if(jump && yvel<5) //if we are jumping and our yvel is less than 5 we increase the yvel
- yvel++;
- else
- jump=0; //else we are not jumping
- //move
- box.x+=xvel;
- box.y+=yvel;
- if(moving) //if we are moving
- {
- frame+=0.2; //then animate
- if(direction=='r' && frame >=1.4)
- frame=0.0;
- else if(direction=='l' && frame >=3.4)
- frame=1.5;
- }
- }
- void player::setJump() //set the player to jump
- {
- if(ground && !jump) //if we are on the ground and we are not jumping
- {
- jump=1; //jump
- ground=0;
- yvel=-17; //starting speed (high)
- box.y-=5; //we don't stuck
- }
- }
- int player::getHealth()
- {
- return health;
- }
- void player::setHealth(int h)
- {
- health=h;
- }
- char player::getDirection()
- {
- return direction;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement