Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL/SDL.h>
- #include <SDL/SDL_ttf.h>
- #include <SDL/SDL_image.h>
- #include <SDL/SDL_mixer.h>
- #include <string>
- #include <iostream>
- #include <sstream>
- #include "Timer.cpp"
- using namespace std;
- int SCREEN_WIDTH;
- int SCREEN_HEIGHT;
- int CAMERA_X = 0;
- const int FRAMES_PER_SECOND = 1000;
- const int SCENE_LIMIT = 2000;
- const int PLAYER_WIDTH = 50;
- const int PLAYER_HEIGHT = 50;
- SDL_Surface *screen = NULL;
- SDL_Surface* player_standing = NULL;
- SDL_Surface* player_walking = NULL;
- SDL_Surface *message = NULL;
- SDL_Surface *background = NULL;
- SDL_Surface *ground = NULL;
- SDL_Rect grass_block;
- TTF_Font *font = NULL;
- Mix_Music *music = NULL;
- SDL_Event event;
- Timer fps;
- bool cap = true;
- bool quit = false;
- bool walking = false;
- bool check_collision(SDL_Rect a, SDL_Rect b){
- if( a.y + a.h <= b.y ) return false;
- if( a.y >= b.y + b.h ) return false;
- if( a.x + a.w <= b.x ) return false;
- if( a.x >= b.x + b.w ) return false;
- return true;
- }
- void blit(int x, int y, SDL_Surface* source, SDL_Rect* clip = NULL, bool center = false){
- SDL_Rect offset;
- if(center) offset.x = x;
- else offset.x = x - CAMERA_X;
- offset.y = y;
- SDL_BlitSurface(source,clip,screen,&offset);
- }
- void blit_background(){
- for(int x=0; x<SCENE_LIMIT; x+=1366){
- blit(x,0,background);
- blit(x,426,ground);
- }
- }
- string tostring(int i){
- stringstream ss;
- ss << i;
- return ss.str();
- }
- class Player{
- private:
- SDL_Rect player_obj;
- int x_vel,y_vel;
- static const int X_VELOCITY = 2;
- static const int Y_VELOCITY = 2;
- static const int STANDING = 0;
- static const int WALKING = 1;
- static const int JUMPING = 2;
- static const int FALLING = 3;
- static const int HITTING = 4;
- static const int LEFT = 0;
- static const int RIGHT = 1;
- int status,direction,frame,offset,starttime;
- public:
- Player(){
- player_obj.x = SCREEN_WIDTH/2;
- player_obj.y = 376;
- status = STANDING;
- direction = RIGHT;
- frame = 0;
- y_vel=x_vel=0;
- starttime = SDL_GetTicks();
- }
- void handle_input(){
- if( event.type == SDL_KEYDOWN )
- {
- switch( event.key.keysym.sym )
- {
- case SDLK_UP:
- if(player_obj.y >= 376){
- y_vel = -Y_VELOCITY;
- status = JUMPING;
- }
- break;
- case SDLK_LEFT:
- x_vel -= X_VELOCITY;
- status = WALKING;
- direction = LEFT;
- break;
- case SDLK_RIGHT:
- x_vel += X_VELOCITY;
- status = WALKING;
- direction = RIGHT;
- break;
- case SDLK_c:
- status = HITTING;
- break;
- }
- }
- else if( event.type == SDL_KEYUP )
- {
- switch( event.key.keysym.sym )
- {
- case SDLK_LEFT:
- x_vel += X_VELOCITY;
- status = STANDING;
- direction = LEFT;
- break;
- case SDLK_RIGHT:
- x_vel -= X_VELOCITY;
- status = STANDING;
- direction = RIGHT;
- break;
- }
- }
- }
- void move(){
- if(player_obj.x == SCREEN_WIDTH/2){
- CAMERA_X += x_vel;
- if(CAMERA_X < 0 || CAMERA_X > SCENE_LIMIT){
- CAMERA_X -= x_vel;
- player_obj.x += x_vel;
- }
- }else{
- player_obj.x += x_vel;
- if(player_obj.x < 0 || player_obj.x + PLAYER_WIDTH > SCREEN_WIDTH || check_collision(player_obj,grass_block)) player_obj.x -= x_vel;
- }
- player_obj.y += y_vel;
- if(player_obj.y < 0 || player_obj.y + PLAYER_HEIGHT > SCREEN_HEIGHT || check_collision(player_obj,grass_block))
- player_obj.y -= y_vel;
- if(player_obj.y < 326){
- status = FALLING;
- y_vel = Y_VELOCITY;
- }else if(player_obj.y >= 376){
- status = STANDING;
- y_vel = 0;
- }
- }
- void show(){
- if( x_vel < 0 ){ status = WALKING; direction = LEFT; }
- else if( x_vel > 0 ){ status = WALKING; direction = RIGHT; }
- else{ status = STANDING; }
- if(SDL_GetTicks() - starttime > 100){
- starttime = SDL_GetTicks();
- frame++;
- }
- if( frame >= 4 ) frame = 0;
- if(status==WALKING && direction == RIGHT){
- SDL_Rect clip;
- clip.x = frame%2 * 50;
- clip.y = 0;
- clip.h = 50;
- clip.w = 50;
- blit( this->get_xpos(), this->get_ypos(), player_walking, &clip, true);
- }else if(status==WALKING && direction == LEFT){
- SDL_Rect clip;
- clip.x = frame%2 * 50;
- clip.y = 50;
- clip.h = 50;
- clip.w = 50;
- blit( this->get_xpos(), this->get_ypos(), player_walking, &clip, true);
- }else if(status==STANDING && direction == RIGHT){
- SDL_Rect clip;
- clip.x = frame * 50;
- clip.y = 0;
- clip.h = 50;
- clip.w = 50;
- blit( this->get_xpos(), this->get_ypos(), player_standing, &clip, true);
- }else if(status==STANDING && direction == LEFT){
- SDL_Rect clip;
- clip.x = frame * 50;
- clip.y = 50;
- clip.h = 50;
- clip.w = 50;
- blit( this->get_xpos(), this->get_ypos(), player_standing, &clip, true);
- }else if(status==HITTING){
- }
- }
- int get_xpos(){
- return player_obj.x;
- }
- int get_ypos(){
- return player_obj.y;
- }
- };
- int main(int argc, char* args[]){
- SDL_Init(SDL_INIT_EVERYTHING);
- TTF_Init();
- Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
- fps.start();
- SDL_WM_SetCaption("Game!",NULL);
- const SDL_VideoInfo* info = SDL_GetVideoInfo();
- SCREEN_WIDTH = info->current_w;
- SCREEN_HEIGHT = info->current_h;
- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_FULLSCREEN);
- player_walking = IMG_Load("../assets/ninja_walking.png");
- player_standing = IMG_Load("../assets/ninja_standing.png");
- font = TTF_OpenFont("../assets/arial.ttf",20);
- music = Mix_LoadMUS("../assets/music.mp3");
- background = IMG_Load("../assets/background.jpg");
- ground = IMG_Load("../assets/ground.jpg");
- Player player;
- while(!quit){
- while(SDL_PollEvent(&event)){
- if(event.type == SDL_QUIT) quit = true;
- else if(event.type == SDL_KEYDOWN)
- if( event.key.keysym.sym == SDLK_RETURN ) cap = !cap;
- player.handle_input();
- }
- player.move();
- SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
- blit_background();
- player.show();
- SDL_Flip(screen);
- if( cap == true ) SDL_Delay(( 1000 / FRAMES_PER_SECOND ));
- }
- SDL_FreeSurface(screen);
- Mix_FreeMusic(music);
- TTF_Quit();
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement