#include "SDL.h" #include "SDL_image.h" #include "SDL_ttf.h" #include "Myincludes/getimageninja.h" #include #include #include #include using namespace std; const int SCREEN_BPP = 32; const int screenwidth = 480; const int screenheight = 270; SDL_Surface *sprites = NULL; SDL_Surface *background = NULL; SDL_Surface *buffer1 = NULL; SDL_Surface *scoretext = NULL; TTF_Font *font = NULL; SDL_Color textColor = { 0, 0, 255 }; bool run = true; short speed = 45; short movenum = 0; short pausemovement = 0; short jumpheight; const short maxjump = 110; bool clingable = true; int floor = screenheight; short numplats = 6; short difficulty = 1; bool gamestart = false; short topplat; long counter = 0; int score = 0; int highscore = 0; short superjump = 0; short superjumpcounter = 0; bool foundfloor = false;//reset check for found the platform below ninja short numenemies = 3; short i = 0; objects ninja; objects platform[7]; objects enemy[3]; SDL_Surface *load_image(std::string filename) { SDL_Surface* loadedImage = NULL; SDL_Surface* optimizedImage = NULL; loadedImage = IMG_Load(filename.c_str()); if(loadedImage != NULL) { optimizedImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); if(optimizedImage != NULL) { SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF )); } } return optimizedImage; } void blitAlphaImageToScreen(int picx, int picy, int width, int height, SDL_Surface* source, int x, int y) { SDL_Rect clip; SDL_Rect offset; clip.x = picx; clip.y = picy; clip.w = width; clip.h = height; offset.x = x; offset.y = y; SDL_BlitSurface(source, &clip, buffer1, &offset);//blit to screen } bool initsdl() { TTF_Init(); if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } buffer1 = SDL_SetVideoMode(screenwidth, screenheight, SCREEN_BPP, SDL_SWSURFACE ); SDL_WM_SetCaption( "Ninja", NULL ); sprites = load_image("ninja.png");//load ninjasprites background = load_image("background.png");//loadbackground font = TTF_OpenFont("Ninjafont.ttf", 20);//load font return true; } int getrandom(int min, int max) { return ((rand()%max) + min); } void initgame() { srand(time(0)); ifstream fin("score.dat", ios::binary); fin.read((char *)(&highscore), sizeof(highscore)); fin.close(); ninja = getobject(1,8,ninja);//load ninja in default state ninja.x = 0; ninja.y = screenheight; //set ninja start position for (i = 0; i <= numplats;i++)//initialize platforms { platform[i] = getobject(6,1,platform[i]); platform[i].x = getrandom(0,screenwidth - platform[i].width); platform[i].y = i * 40; } for (i = 0; i < numenemies;i++) { enemy[i].action = 1;//1 = alive 2 = dead enemy[i] = getobject(1,8,enemy[i]); enemy[i].platformnum = getrandom(0,numplats); enemy[i].y = platform[enemy[i].platformnum].y - platform[enemy[i].platformnum].height; enemy[i].x = platform[enemy[i].platformnum].x +getrandom(0,100); enemy[i].movex = getrandom(-1,1); enemy[i].movenum = 5; } difficulty = 1; gamestart = false; run = true; clingable = true; counter = 0; score = 0; } void checkplatforms(int i2) { if (gamestart == true) platform[i2].y += difficulty;//if game is started then move platforms if (platform[i2].y > screenheight + 32 + platform[i2].height)//if platform goes off of screen { platform[i2].y = 0 - platform[i2].height; platform[i2].x = getrandom(0,screenwidth - platform[i2].width); } if (foundfloor == false) { if (ninja.x + ninja.width > platform[i2].x && ninja.x < platform[i2].x + platform[i2].width && ninja.y < platform[i2].y) { floor = platform[i2].y - platform[i2].height; foundfloor = true; } else { floor = screenheight; } } } void platforms() { foundfloor = false; int lowest = 280;//reset lowest platform if(gamestart == true) { counter++; score++; } if (counter >= 500 && speed > 30) { speed--; counter = 0; if (difficulty < 4) difficulty ++; } for (i = 0; i <= numplats;i++)//get the highest platform { if (platform[i].y < lowest) { lowest = platform[i].y; topplat = i; } } for(short i2 = topplat; i2 <= numplats;i2++) checkplatforms(i2); for(i = 0; i < topplat;i++) checkplatforms(i); if (floor != screenheight && gamestart == true) ninja.y += difficulty; if (floor != screenheight && ninja.y == floor) gamestart = true; } void checkevents() { // ninja.actions 0 = nothing; 1 = running; 3 = crouch; 4 = sword; 5 = crouch sword;6 = jump; //7 = clinging to wall SDL_Event event; SDL_PollEvent(&event); Uint8 *keystates = SDL_GetKeyState( NULL ); if(event.type == SDL_QUIT) { run = false; } if(keystates[SDLK_DOWN] && ninja.action != 7)//duck { if(ninja.action == 0 || ninja.action == 1 || ninja.action == 3) { if (movenum <= 4) { ninja = getobject(5,3,ninja); } else if (movenum > 4) { ninja = getobject(5,4,ninja); } } else if (ninja.action == 5) if (movenum <= 4) { ninja.x -= 18 - ninja.width; ninja = getobject(5,3,ninja); } else if (movenum > 4) { ninja = getobject(5,4,ninja); } if (ninja.action != 6) ninja.action = 3; } else if (ninja.action != 6 && ninja.action != 7) ninja.action = 0; if(event.key.keysym.sym == SDLK_SPACE && ninja.action != 7)//sword { if(ninja.action == 0 || ninja.action == 1) { if (movenum <= 4) { ninja.x -= 39 - ninja.width; ninja = getobject(2,2,ninja); } else if (movenum > 4) { ninja = getobject(2,3,ninja); } ninja.action = 4; } else if(ninja.action == 3) { if (movenum <= 4) { ninja.x -= 39 - ninja.width; ninja = getobject(5,2,ninja); } else if (movenum > 4) { ninja = getobject(5,5,ninja); } ninja.action = 5; } pausemovement = 1; } if(keystates[SDLK_UP] && pausemovement == 0)//jump { if ((ninja.x > 0 && ninja.x + ninja.width < screenwidth) && ninja.action != 7) { if (jumpheight == 0 && ninja.y >= floor) { jumpheight = maxjump + superjump; superjump = 0; } ninja.action = 6; } else if (ninja.action == 7) { jumpheight = maxjump + superjump; superjump = 0; ninja.action = 6; } if (ninja.x <= 0 && clingable == true) { ninja.action = 7; ninja = getobject(2,6,ninja); ninja.x = 0; clingable = false; pausemovement = 2; } else if (ninja.x + ninja.width >= screenwidth && clingable == true) { ninja.action = 7; ninja = getobject(2,5,ninja); ninja.x = screenwidth - ninja.width; clingable = false; pausemovement = 2; } } if (pausemovement == 0 && ninja.action != 7) { //if no events are happening if(keystates[SDLK_RIGHT])//go right { if (ninja.x < screenwidth - ninja.width) ninja.x += 10; if (ninja.y >= floor) { if (ninja.x + ninja.width >= screenwidth) ninja.x = screenwidth - ninja.width; if (ninja.action != 6) { if (movenum < 5) { movenum = 5; } movenum++; ninja = getobject(1,movenum,ninja); if (movenum == 7) { movenum = 5; } ninja.action = 1; } } } else if(keystates[SDLK_LEFT])//go left { if (ninja.x > 0) ninja.x -= 10; if (ninja.y >= floor) { if (ninja.x < 0) ninja.x = 0; if (ninja.action != 6) { if (movenum > 4) { movenum = 2; } movenum++; ninja = getobject(1,movenum,ninja); if (movenum == 4) { movenum = 2; } ninja.action = 1; } } } else if ((movenum >= 2 || movenum <= 7) && ninja.action != 3 && ninja.action != 6 && ninja.action != 7) //check if moved and put in default for which dir { if (movenum <= 4)//if ninja used sword left { ninja.x += ninja.width - 17; ninja = getobject(1,1,ninja); } if (movenum <= 4) ninja = getobject(1,1,ninja); else if (movenum > 4) ninja = getobject(1,8,ninja); ninja.action = 0; } } if (pausemovement > 0) pausemovement --; } void jump() { if (ninja.action == 3 && superjump < 50) superjumpcounter++; if (superjumpcounter == 10) { superjump += 10; superjumpcounter = 0; } if ((ninja.y == screenheight && gamestart == true) || (ninja.y > screenheight && ninja.action == 7)) { if(score > highscore) { ofstream fout("score.dat", ios::binary); fout.write((char *)(&score), sizeof(score)); fout.close(); } initgame(); } if (ninja.y != floor && ninja.action != 7) ninja.action = 6; if (ninja.action == 6){ if (jumpheight > 0) { jumpheight -=10; ninja.y -=10; } else if(ninja.y < floor) ninja.y +=10; if (ninja.y > floor) { ninja.y = floor; clingable = true; } ninja = getobject(3,movenum,ninja); movenum++; if (movenum == 7) movenum = 1; if (ninja.y >= floor) ninja.action = 0; } if (ninja.action == 7 && gamestart == true) ninja.y += 5; if (ninja.x > 75 && ninja.x < screenwidth - ninja.width - 75) clingable = true; } void enemies() { for(short i = 0;i < numenemies;i++) {//put enemy on platform if (enemy[i].x <= platform[enemy[i].platformnum].x) enemy[i].movex = difficulty;//move right if goes to left of platform else if (enemy[i].x >= platform[enemy[i].platformnum].x + (platform[enemy[i].platformnum].width - enemy[i].width)) enemy[i].movex = -1 * difficulty;//move left if goes to right of platform if (enemy[i].y <= 0 && enemy[i].action == 0) { enemy[i].action = 1;//bring enemy to life enemy[i].x = getrandom(platform[enemy[i].platformnum].x,100); } if (enemy[i].y > screenheight)// kill enemy if he's below the screen and put him on a new platform { enemy[i].action = 0; enemy[i].platformnum = getrandom(0,numplats); } if (enemy[i].movex > 0)//moveimages right { enemy[i].movenum ++; if (enemy[i].movenum == 8) enemy[i].movenum = 5; } else if (enemy[i].movex < 0)//moveimages left { enemy[i].movenum --; if (enemy[i].movenum == 1) enemy[i].movenum = 4; } enemy[i] = getobject(1,enemy[i].movenum,enemy[i]); enemy[i].y = platform[enemy[i].platformnum].y - platform[enemy[i].platformnum].height; enemy[i].x += enemy[i].movex; if (enemy[i].action == 1)//if enemy is alive if (ninja.x > enemy[i].x && ninja.x < enemy[i].x + enemy[i].width)//if ninja is between x of enemy if (ninja.y + ninja.height > enemy[i].y && ninja.y < enemy[i].y + enemy[i].height) if(ninja.y - (ninja.height / 2) > 0)//if it's not above the screen initgame(); } } void collision() { } void draw() { char scorechar[7]; blitAlphaImageToScreen(0,0, screenwidth, screenheight, background, 0, 0); for(i = 0;i <= numplats;i++) blitAlphaImageToScreen(platform[i].picx,platform[i].picy, platform[i].width, platform[i].height, sprites, platform[i].x, platform[i].y - platform[i].height); for(i = 0;i <= numenemies;i++) if (enemy[i].action != 0) blitAlphaImageToScreen(enemy[i].picx,enemy[i].picy - enemy[i].height, enemy[i].width, enemy[i].height, sprites, enemy[i].x, enemy[i].y - enemy[i].height); blitAlphaImageToScreen(ninja.picx,ninja.picy - ninja.height, ninja.width, ninja.height, sprites, ninja.x, ninja.y - ninja.height); SDL_Flip(buffer1); } void clean_up() { SDL_FreeSurface(sprites); SDL_FreeSurface(background); SDL_FreeSurface(buffer1); SDL_FreeSurface(scoretext); TTF_CloseFont(font); SDL_Quit(); TTF_Quit(); } int main( int argc, char* args[] ) { initsdl(); initgame(); while(run) { platforms(); checkevents(); jump(); enemies(); collision(); draw(); SDL_Delay(speed); } clean_up(); return 0; }