Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<stdio.h>
- #include<string>
- #include<SDL2/SDL.h>
- #include<SDL2/SDL_image.h>
- #include<string.h>
- #include<SDL2/SDL_ttf.h>
- enum keys{
- useless, left, right, up, back, end, show_high_score, anything,
- start, rstart,
- quitbutton, rquit,
- instructions, rinstructions,
- highscore_e, rhighscore,
- bouncee,
- gameover,
- nextlevel, rnextlevel,
- menu, rmenu,
- Score, score__,
- background, greendot,
- level_one, level_two, level_three,
- rback,
- description, originalback, box, text, input,
- bomb
- , point, ticks, no, yes, rno, ryes, another_text
- };
- const int screen_width=1280;
- const int screen_height=680;
- const int gravity=2;
- int score=0, cnt=0, prevscore=0;
- int high_score;
- int a[1000], b[1000], death_x[1000], death_y[1000];
- int tick, tick1, tick2;
- char times[100];
- bool saved_game=false, load_saved_game=false, saved_state=false;
- int initialTime=0;
- int level_id=0;
- SDL_Window* window=NULL;
- SDL_Renderer* game_renderer=NULL;
- SDL_Texture* tex=NULL;
- TTF_Font *gFont = NULL;
- bool init();
- bool loadMedia();
- void closes();
- //functions handling movement, collision and platform detections, score, poison, time
- bool detect(SDL_Rect, SDL_Rect);
- void move();
- bool x_range(int, int , int);
- bool solid_footing();
- bool solid_footing_ver();
- void set1(int a[], int b[]);
- void count_point();
- int event_id();
- void score_calc();
- void score_box();
- void sort();
- void time_func();
- void green();
- //functions handling displays
- int Menu();
- int game_over();
- int score_display();
- int high_score_display();
- int save_game();
- void game_state_saving();
- int load_from_file();
- int actual_loading();
- //levels and level builders
- int level1();
- int level2();
- void build_level1();
- void build_level2();
- void build_level3();
- //a function to make rectangle handling easier
- void rect(SDL_Rect, int , int, int , int, int, int, int , int);
- struct score_structure
- {
- char a[100];
- int i;
- };
- struct rectangle
- {
- SDL_Rect a[300];
- int number_of;
- int id[300], count;
- };
- rectangle platforms, dots, death;
- class texture
- {
- public:
- texture();
- ~texture();
- bool loadFromFile( std::string path );
- #if defined(_SDL_TTF_H) || defined(SDL_TTF_H)
- //Creates image from font string
- bool loadFromRenderedText( std::string textureText, SDL_Color textColor );
- #endif
- void free();
- void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE );
- //Gets image dimensions
- int getWidth();
- int getHeight();
- private:
- //The actual hardware texture
- SDL_Texture* mTexture;
- //Image dimensions
- int mWidth;
- int mHeight;
- };
- texture::texture()
- {
- //Initialize
- mTexture = NULL;
- mWidth = 0;
- mHeight = 0;
- }
- texture::~texture()
- {
- //Deallocate
- free();
- }
- bool texture::loadFromFile( std::string path )
- {
- //Get rid of preexisting texture
- free();
- //The final texture
- SDL_Texture* newTexture = NULL;
- //Load image at specified path
- SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
- if( loadedSurface == NULL )
- {
- printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
- }
- else
- {
- //Color key image
- if(path!="description.png")
- SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0xFF, 0xFF, 0xFF ) );
- //Create texture from surface pixels
- newTexture = SDL_CreateTextureFromSurface( game_renderer, loadedSurface );
- if( newTexture == NULL )
- {
- printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
- }
- else
- {
- //Get image dimensions
- mWidth = loadedSurface->w;
- mHeight = loadedSurface->h;
- }
- //Get rid of previous loaded surface
- SDL_FreeSurface( loadedSurface );
- }
- //Return success
- mTexture = newTexture;
- return mTexture != NULL;
- }
- #if defined(_SDL_TTF_H) || defined(SDL_TTF_H)
- bool texture::loadFromRenderedText( std::string textureText, SDL_Color textColor )
- {
- //Get rid of pre-existing texture
- free();
- //Render text surface
- SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor );
- if( textSurface != NULL )
- {
- //Create texture from surface pixels
- mTexture = SDL_CreateTextureFromSurface( game_renderer, textSurface );
- if( mTexture == NULL )
- {
- printf( "%s\n", SDL_GetError() );
- }
- else
- {
- //Get image dimensions
- mWidth = textSurface->w;
- mHeight = textSurface->h;
- }
- //Get rid of previous surface
- SDL_FreeSurface( textSurface );
- }
- else
- {
- printf( "%s\n", TTF_GetError() );
- }
- //Return success
- return mTexture != NULL;
- }
- #endif
- void texture::free()
- {
- //Frees texture if it exists
- if( mTexture != NULL )
- {
- SDL_DestroyTexture( mTexture );
- mTexture = NULL;
- mWidth = 0;
- mHeight = 0;
- }
- }
- void texture::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
- {
- //Set rendering space and render to screen
- SDL_Rect renderQuad = { x, y, mWidth, mHeight };
- //Set clip rendering dimension
- if( clip != NULL )
- {
- renderQuad.w = clip->w;
- renderQuad.h = clip->h;
- }
- //Render to screen
- SDL_RenderCopyEx( game_renderer, mTexture, clip, &renderQuad, angle, center, flip );
- }
- int texture::getWidth()
- {
- return mWidth;
- }
- int texture::getHeight()
- {
- return mHeight;
- }
- texture character_texture;
- texture backgrounds[100];
- struct dummy
- {
- static const int dummyw = 20;
- static const int dummyh = 20;
- static const int vel = 10;
- void detect_points();
- void detect_death();
- void render();
- void handle(int, int);
- int posX, posY;
- int velX, velY;
- };
- void green()
- {
- char s[100];
- sprintf(s, "POISON: %d", cnt);
- SDL_Color t={2, 36, 37};
- backgrounds[greendot].loadFromRenderedText(s, t);
- if(cnt!=0) backgrounds[greendot].render(1000, 630);
- }
- dummy character;
- void dummy::detect_death()
- {
- SDL_Rect a={character.posX, character.posY, dummyw, character.dummyh};
- for(int i=0; i<=death.number_of; i++)
- {
- SDL_Rect b={ death.a[i].x , death.a[i].y , 20, 20};
- if(detect(a, b))
- {
- if(death.id[i]==-1)
- {
- cnt++;
- green();
- }
- death.id[i]=0;
- }
- }
- }
- void dummy::detect_points() //x=character.posX of char y=character.posY of char
- {
- SDL_Rect a={character.posX, character.posY, dummyw, character.dummyh};
- for(int i=0; i<=dots.number_of; i++)
- {
- SDL_Rect b={ dots.a[i].x , dots.a[i].y , 20, 20};
- if(detect(a, b))
- {
- dots.id[i]=0;
- }
- }
- }
- void dummy::render()
- {
- //Show the dot
- character_texture.render( character.posX, character.posY );
- }
- void dummy::handle(int keynum, int level)
- {
- if(keynum==right)
- {
- velX=vel;
- character.velY=0;
- move();
- if(!solid_footing_ver())
- {
- velX=0;
- while(!solid_footing_ver())
- {
- character.velY+=gravity;
- move();
- backgrounds[background].render(0, 0);
- if(level ==1) build_level1();
- else if(level ==2) build_level2();
- else build_level3();
- character.render();
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- SDL_Delay(25);
- }
- }
- }
- if(keynum==left)
- {
- velX=-vel;
- character.velY=0;
- move();
- if(!solid_footing_ver())
- {
- velX=0;
- while(!solid_footing_ver())
- {
- character.velY+=gravity;
- move();
- backgrounds[background].render(0, 0);
- if(level ==1) build_level1();
- else if(level ==2) build_level2();
- else build_level3();
- character.render();
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- SDL_Delay(25);
- }
- }
- }
- if(keynum==up)
- {
- velX=0;
- character.velY=-22;
- bool jump=true;
- while(jump)
- {
- character.velY+=gravity;
- move();
- backgrounds[background].render(0, 0);
- if(level ==1) build_level1();
- else if(level ==2) build_level2();
- else build_level3();
- character.render();
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- SDL_Delay(25);
- if(velY>=0)
- {
- if(solid_footing_ver()) jump=false;
- }
- }
- }
- if(keynum==up+right || keynum==up+left)
- {
- if(keynum==up+right) velX=vel;
- else if(keynum==up+left) velX=0-vel;
- character.velY=-22;
- bool jump=true;
- while(jump)
- {
- character.velY+=gravity;
- move();
- backgrounds[background].render(0, 0);
- if(level ==1) build_level1();
- else if(level ==2) build_level2();
- else build_level3();
- character.render();
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- SDL_Delay(25);
- if(velY>=0)
- {
- if(solid_footing()) jump=false;
- }
- }
- }
- }
- void game_state_saving()
- {
- FILE *gameS = fopen("game_save_info.txt", "w");
- fprintf(gameS, "%d\n", level_id);
- fprintf(gameS, "%d %d\n", character.posX, character.posY);
- fprintf(gameS, "%d\n", dots.number_of);
- for(int n=0; n<=dots.number_of; n++)
- {
- fprintf(gameS, "%d %d %d\n", a[n], b[n], dots.id[n]);
- }
- fprintf(gameS, "%d\n", death.number_of);
- for(int n=0; n<=death.number_of; n++)
- {
- fprintf(gameS, "%d %d %d\n", death_x[n], death_y[n], death.id[n]);
- }
- fprintf(gameS, "%d %d\n", score, prevscore);
- fprintf(gameS, "%d\n", tick);
- fprintf(gameS, "%d\n", cnt);
- fclose(gameS);
- return;
- }
- int save_game()
- {
- char s[100]="Save the game for later?";
- SDL_Color co={255, 255, 255};
- backgrounds[another_text].loadFromRenderedText(s, co);
- bool yes_button=false, no_button=false;
- bool stop = false;
- SDL_Event e;
- while(!stop)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type == SDL_QUIT)
- return end;
- else if(e.type == SDL_MOUSEBUTTONDOWN)
- {
- int p, q;
- SDL_GetMouseState(&p, &q);
- if(p>380 && p<280+252 && q>270 && q<270+205)
- {
- game_state_saving();
- return menu;
- }
- else if(p>700 && p<700+252 && q>270 && q<270+205)
- {
- FILE *p=fopen("game_save_info.txt", "w");
- fprintf(p, "%d\n", 0);
- fclose(p);
- return no;
- }
- }
- }
- int p, q;
- SDL_GetMouseState(&p, &q);
- if(p>380 && p<280+252 && q>270 && q<270+205)
- yes_button=true;
- else if(p>700 && p<700+252 && q>270 && q<270+205)
- no_button=true;
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- backgrounds[another_text].render(200, 150);
- if(yes_button) backgrounds[ryes].render(380, 270);
- else backgrounds[yes].render(380, 270);
- if(no_button) backgrounds[rno].render(700, 270);
- else
- {
- backgrounds[no].render(700, 270);
- }
- SDL_RenderPresent(game_renderer);
- yes_button=false;
- no_button=false;
- }
- }
- int event_id(int level,const Uint8* ks, SDL_Event e)
- {
- if(ks[SDL_SCANCODE_RIGHT] && !ks[SDL_SCANCODE_UP] && !ks[SDL_SCANCODE_LEFT])
- {
- character.handle(right, level);
- }
- else if(!ks[SDL_SCANCODE_RIGHT] && !ks[SDL_SCANCODE_UP] && ks[SDL_SCANCODE_LEFT])
- {
- character.handle(left, level);
- }
- else if(ks[SDL_SCANCODE_UP] && !ks[SDL_SCANCODE_RIGHT] && !ks[SDL_SCANCODE_LEFT])
- {
- character.handle(up, level);
- }
- else if(ks[SDL_SCANCODE_UP] && ks[SDL_SCANCODE_RIGHT] && !ks[SDL_SCANCODE_LEFT])
- {
- character.handle(up+right, level);
- }
- else if(ks[SDL_SCANCODE_UP] && !ks[SDL_SCANCODE_RIGHT] && ks[SDL_SCANCODE_LEFT])
- {
- character.handle(up+left, level);
- }
- }
- bool x_range(int x, int sth, int wi)
- {
- if(x>sth-18 && x<sth+wi-2)
- return true;
- return false;
- }
- bool solid_footing()
- {
- int top=character.posY, bottom=character.posY+character.dummyh;
- for(int i=0; i<=platforms.number_of; i++)
- {
- if(x_range(character.posX, platforms.a[i].x, platforms.a[i].w))
- {
- if(bottom==platforms.a[i].y) return true;
- else if(top<=platforms.a[i].y && bottom>platforms.a[i].y)
- {
- character.posY=platforms.a[i].y-character.dummyh;
- return true;
- }
- else if(bottom + character.velY + gravity <= platforms.a[i].y) continue;
- else if((platforms.a[i].y-bottom)<(character.velY+gravity) && x_range(character.posX+character.velX, platforms.a[i].x, platforms.a[i].w) && character.posY<platforms.a[i].y)
- {
- character.posY=platforms.a[i].y-character.dummyh;
- return true;
- }
- }
- }
- return false;
- }
- bool solid_footing_ver()
- {
- int top=character.posY, bottom=character.posY+character.dummyh;
- for(int i=0; i<=platforms.number_of; i++)
- {
- if(x_range(character.posX, platforms.a[i].x, platforms.a[i].w))
- {
- if(bottom==platforms.a[i].y) return true;
- else if(top<platforms.a[i].y && bottom>platforms.a[i].y)
- {
- character.posY=platforms.a[i].y-character.dummyh;
- return true;
- }
- else if(bottom + character.velY + gravity <= platforms.a[i].y) continue;
- else if((platforms.a[i].y-bottom)<(bottom+character.velY+gravity) && character.posY<platforms.a[i].y)
- {
- character.posY=platforms.a[i].y-character.dummyh;
- return true;
- }
- }
- }
- return false;
- }
- void move()
- {
- //Move the dot left or right
- character.posX += character.velX;
- //If the dot went too far to the left or right
- if( ( character.posX < 0 ) || ( character.posX + character.dummyh > screen_width ) )
- {
- //Move back
- character.posX -= character.velX;
- }
- character.posY += character.velY;
- //If the dot went too far up or down
- if( ( character.posY < 0 ) || ( character.posY + character.dummyh > screen_height ) )
- {
- //Move back
- if(character.posY<0)
- character.posY -= character.velY;
- else
- {
- character.posY=screen_height-character.dummyh;
- }
- }
- character.detect_points();
- character.detect_death();
- }
- bool detect (SDL_Rect a, SDL_Rect b)
- {
- int leftA, leftB;
- int rightA, rightB;
- int topA, topB;
- int bottomA, bottomB;
- //Calculate the sides of rect A
- leftA = a.x;
- rightA = a.x + a.w;
- topA = a.y;
- bottomA = a.y + a.h;
- //Calculate the sides of rect B
- leftB = b.x;
- rightB = b.x + b.w;
- topB = b.y;
- bottomB = b.y + b.h;
- //If any of the sides from A are outside of B
- if( bottomA <= topB )
- {
- return false;
- }
- if( topA >= bottomB )
- {
- return false;
- }
- if( rightA <= leftB )
- {
- return false;
- }
- if( leftA >= rightB )
- {
- return false;
- }
- //If none of the sides from A are outside B
- return true;
- }
- bool init()
- {
- bool success = true;
- if(SDL_Init(SDL_INIT_VIDEO)<0)
- {
- success= false;
- printf("%s\n", SDL_GetError());
- }
- else
- {
- if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
- {
- printf( "Warning: Linear texture filtering not enabled!" );
- }
- window= SDL_CreateWindow("Bouncee", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_SHOWN);
- if(window==NULL)
- {
- success =false;
- printf("%s\n", SDL_GetError());
- }
- else
- {
- game_renderer=SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- if(game_renderer==NULL)
- {
- success=false;
- printf("%s\n", SDL_GetError());
- }
- else
- {
- SDL_SetRenderDrawColor( game_renderer, 0x66, 0xF2, 0xFF, 0xFF );
- int imgFlags = IMG_INIT_PNG;
- if( !( IMG_Init( imgFlags ) & imgFlags ) )
- {
- printf( "%s\n", IMG_GetError() );
- success = false;
- }
- if( TTF_Init() == -1 )
- {
- printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
- success = false;
- }
- }
- }
- }
- return success;
- }
- bool loadMedia()
- {
- bool success=true;
- gFont = TTF_OpenFont( "berkshireswash-regular.ttf", 40 );
- if( gFont == NULL )
- {
- printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() );
- success = false;
- }
- if(!backgrounds[no].loadFromFile( "no.bmp" ))
- {
- printf("no\n");
- success=false;
- }
- if(!backgrounds[rno].loadFromFile( "no_red.bmp" ))
- {
- printf("red no\n");
- success=false;
- }
- if(!backgrounds[yes].loadFromFile( "yes.bmp" ))
- {
- printf("yes\n");
- success=false;
- }
- if(!backgrounds[ryes].loadFromFile( "yes_red.bmp" ))
- {
- printf("yes red\n");
- success=false;
- }
- if(!backgrounds[point].loadFromFile("14.png"))
- {
- success=false;
- }
- if(!backgrounds[bomb].loadFromFile( "bomb.png" ))
- {
- printf("failed ot load bomb\n");
- success=false;
- }
- if(!backgrounds[description].loadFromFile( "description.png" ))
- {
- printf("failed to load description\n");
- success=false;
- }
- if(!backgrounds[originalback].loadFromFile( "bouncee.png" ))
- {
- printf("failed to load bouncee\n");
- success=false;
- }
- if(!backgrounds[back].loadFromFile( "back.bmp" ))
- {
- printf("failed to load back button\n");
- success=false;
- }
- if(!backgrounds[rback].loadFromFile( "back_red.bmp" ))
- {
- printf("failed to load red back button\n");
- success=false;
- }
- if( !character_texture.loadFromFile( "dot1.bmp" ) )
- {
- printf( "Failed to load dot texture!\n" );
- success = false;
- }
- if(!backgrounds[start].loadFromFile("start.bmp"))
- {
- printf("failed to load start texture\n");
- success = false;
- }
- if(!backgrounds[quitbutton].loadFromFile( "quit.bmp" ))
- {
- printf("failed to load quit texture\n");
- success = false;
- }
- if(!backgrounds[instructions].loadFromFile( "instructions.bmp" ))
- {
- printf("failed to load instructions texture\n");
- success = false;
- }
- if(!backgrounds[highscore_e].loadFromFile( "highscore.bmp" ))
- {
- printf("failed to load highscore texture\n");
- success = false;
- }
- if(!backgrounds[rstart].loadFromFile( "start_red.bmp" ))
- {
- printf("failed to load red start texture\n");
- success = false;
- }
- if(!backgrounds[rquit].loadFromFile( "quit_red.bmp" ))
- {
- printf("failed to load quit red texture\n");
- success = false;
- }
- if(!backgrounds[rinstructions].loadFromFile( "instructions_red.bmp" ))
- {
- printf("failed to load red instrucitons texture\n");
- success = false;
- }
- if(!backgrounds[rhighscore].loadFromFile( "highscore_red.bmp" ))
- {
- printf("failed to load red highscore texture\n");
- success = false;
- }
- if(!backgrounds[bouncee].loadFromFile("menubackground.bmp"))
- {
- printf("failed to load bouncee texture\n");
- success=false;
- }
- if(!backgrounds[menu].loadFromFile("menu.bmp"))
- {
- printf("failed to lead menu tecx\n");
- success=false;
- }
- if(!backgrounds[rmenu].loadFromFile("menu_red.bmp"))
- {
- printf("failed to load red menu\n");
- success=false;
- }
- if(!backgrounds[nextlevel].loadFromFile("nextlevel.bmp"))
- {
- printf("failed to laod next level te\n");
- success=false;
- }
- if(!backgrounds[rnextlevel].loadFromFile("nextlevel_red.bmp"))
- {
- printf("failed to load red next level\n");
- success=false;
- }
- if(!backgrounds[gameover].loadFromFile("gameover.bmp"))
- {
- printf("failed to load game over tec\n");
- success=false;
- }
- if(!backgrounds[score__].loadFromFile( "score.bmp" ) )
- {
- printf("failed to load score bg\n");
- success=false;
- }
- if(!backgrounds[background].loadFromFile( "background.bmp" ))
- {
- printf("failed to load last background?");
- success=false;
- }
- return success;
- }
- void closes()
- {
- character_texture.free();
- SDL_DestroyTexture(tex);
- tex=NULL;
- SDL_DestroyRenderer(game_renderer);
- game_renderer=NULL;
- SDL_DestroyWindow(window);
- window=NULL;
- IMG_Quit();
- SDL_Quit();
- }
- int high_score_display()
- {
- int x, y, k;
- score_structure sc[10];
- char dis[1000];
- SDL_Color textColor={255, 255, 255};
- FILE *p=fopen("score.txt", "r");
- for(k=0; k<=4; k++)
- {
- fscanf(p, "%d %s", &sc[k].i, sc[k].a);
- }
- fclose(p);
- SDL_SetRenderDrawColor(game_renderer, 0, 255, 237, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- sprintf(dis, "HIGH SCORE", high_score);
- backgrounds[Score].loadFromRenderedText(dis, textColor);
- backgrounds[Score].render((screen_width/4)-50, screen_height/4);
- for(k=0, x=(screen_width/4)-50, y=screen_height/4 + 50; k<=4; k++, y+=50)
- {
- sprintf(dis, "%d. %d %s", k+1, sc[k].i, sc[k].a);
- backgrounds[Score].loadFromRenderedText(dis, textColor);
- backgrounds[Score].render(x, y);
- }
- SDL_RenderPresent(game_renderer);
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- {
- quit=true;
- return end;
- }
- if(e.type==SDL_KEYDOWN)
- {
- if(e.key.keysym.sym==SDLK_RETURN)
- {
- quit=true;
- return useless;
- }
- }
- }
- SDL_SetRenderDrawColor(game_renderer, 0, 255, 237, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- sprintf(dis, "HIGH SCORE", high_score);
- backgrounds[Score].loadFromRenderedText(dis, textColor);
- backgrounds[Score].render((screen_width/4)-50, screen_height/4);
- for(k=0, x=(screen_width/4)-50, y=screen_height/4 + 50; k<=4; k++, y+=50)
- {
- sprintf(dis, "%d. %d %s", k+1, sc[k].i, sc[k].a);
- backgrounds[Score].loadFromRenderedText(dis, textColor);
- backgrounds[Score].render(x, y);
- }
- SDL_RenderPresent(game_renderer);
- }
- }
- int description_()
- {
- SDL_RenderClear(game_renderer);
- backgrounds[description].render(0, 0);
- SDL_RenderPresent(game_renderer);
- SDL_Event e;
- bool quit=false;
- while(!quit)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- return end;
- else if(e.type==SDL_KEYDOWN)
- {
- if(e.key.keysym.sym==SDLK_RETURN)
- return anything;
- }
- }
- backgrounds[description].render(0, 0);
- SDL_RenderPresent(game_renderer);
- }
- }
- int actual_loading()
- {
- int read, level;
- FILE *gameFile = fopen("game_save_info.txt", "r");
- fscanf(gameFile, "%d", &level);
- fscanf(gameFile, "%d %d", &character.posX, &character.posY);
- fscanf(gameFile, "%d", &dots.number_of);
- for(int m=0; m<= dots.number_of; m++)
- {
- fscanf(gameFile, "%d %d %d", &a[m], &b[m], &dots.id[m]);
- }
- fscanf(gameFile, "%d", &death.number_of);
- for(int m=0; m <= death.number_of; m++)
- {
- fscanf(gameFile, "%d %d %d", &death_x[m], &death_y[m], &death.id[m]);
- }
- fscanf(gameFile, "%d %d", &score, &prevscore);
- fscanf(gameFile, "%d", &initialTime);
- fscanf(gameFile, "%d", &cnt);
- fclose(gameFile);
- load_saved_game = true;
- return level;
- }
- int load_from_file()
- {
- char s[]="Load previously saved game?";
- SDL_Color co={255, 255, 255};
- backgrounds[another_text].loadFromRenderedText(s, co);
- bool yes_button=false, no_button=false;
- bool stop = false;
- SDL_Event e;
- while(!stop)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type == SDL_QUIT)
- return end;
- else if(e.type == SDL_MOUSEBUTTONDOWN)
- {
- int p, q;
- SDL_GetMouseState(&p, &q);
- if(p>380 && p<280+252 && q>270 && q<270+205)
- {
- int level=actual_loading();
- load_saved_game=true;
- return level;
- }
- else if(p>700 && p<700+252 && q>270 && q<270+205)
- {
- return no;
- }
- }
- }
- int p, q;
- SDL_GetMouseState(&p, &q);
- if(p>380 && p<280+252 && q>270 && q<270+205)
- yes_button=true;
- else if(p>700 && p<700+252 && q>270 && q<270+205)
- no_button=true;
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- backgrounds[another_text].render(200, 150);
- if(yes_button) backgrounds[ryes].render(380, 270);
- else backgrounds[yes].render(380, 270);
- if(no_button) backgrounds[rno].render(700, 270);
- else backgrounds[no].render(700, 270);
- SDL_RenderPresent(game_renderer);
- yes_button=false;
- no_button=false;
- }
- }
- int Menu()
- {
- FILE *gameS=fopen("game_save_info.txt", "r");
- int read;
- fscanf(gameS, "%d", &read);
- fclose(gameS);
- if(read!=0) {saved_game=true;}
- else saved_game=false;
- bool start_button=false, quit_button=false, description_button = false, highscore_button=false;
- int success=1;
- SDL_Event e;
- bool cancel=false;
- while(!cancel)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- return 0;
- else if(e.type== SDL_MOUSEBUTTONDOWN)
- {
- int x, y;
- SDL_GetMouseState( &x, &y );
- if(x>=60 && x<=260 && y>=320 && y<=520)
- {
- if(saved_game)
- {
- int display1 = load_from_file();
- if(display1==no) return level_one;
- else
- return display1;
- }
- else
- return level_one;
- }
- else if(x>=1020 && x<=1220 && y>=320 && y<=520) return end;
- else if(x>=34+320 && x<=34+320+252 && y>=320 && y<=520)
- {
- int display2=description_();
- if(display2==end)
- return end;
- else if(display2==anything)
- continue;
- }
- else if(x>=700 && x<=900 && y>=320 && y<=520)
- {
- int display3=high_score_display();
- if(display3==end)
- return end;
- else continue;
- }
- }
- }
- int x, y;
- SDL_GetMouseState( &x, &y );
- if(x>=34 && x<=34+252 && y>=320 && y<=520)
- {
- start_button=true;
- }
- else if(x>=34+320 && x<=34+320+252 && y>=320 && y<=520)
- {
- description_button=true;
- }
- else if(x>=34+(320*2) && x<=34+(320*2)+252 && y>=320 && y<=520)
- {
- highscore_button=true;
- }
- else if(x>=34+(320*3) && x<=34+(320*3)+252 && y>=320 && y<=520)
- {
- quit_button=true;
- }
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[bouncee].render(0,0);
- backgrounds[originalback].render(0, 0);
- if(start_button) backgrounds[rstart].render(34, 320);
- else
- backgrounds[start].render(34, 320);
- if(description_button) backgrounds[rinstructions].render(34+320, 320);
- else
- backgrounds[instructions].render(34+320, 320);
- if(highscore_button) backgrounds[rhighscore].render(34+(320*2), 320);
- else
- backgrounds[highscore_e].render(34+(320*2), 320);
- if(quit_button) backgrounds[rquit].render(34+(320*3), 320);
- else
- backgrounds[quitbutton].render(34+(320*3), 320);
- SDL_RenderPresent(game_renderer);
- start_button=false; quit_button=false; description_button = false; highscore_button=false;
- }
- /*
- four options: start , instructions, high score, quit
- start returns 1
- quit returns 0
- instructions renders an image with instructions
- high score displays a list with three names/only the high score
- */
- return success;
- }
- void set_death(int i[], int j[])
- {
- srand(SDL_GetTicks());
- for(int k=0; k<=death.number_of; k++)
- {
- i[k]=rand();
- i[k]%=1200;
- j[k]=rand();
- j[k]%=500;
- }
- }
- void score_calc()
- {
- score=0;
- for(int i=0; i<=dots.number_of-1; i++)
- {
- if(dots.id[i]==0)
- {
- score+=2500;
- }
- }
- if(prevscore!=0) score+=prevscore;
- score-=((cnt*811)+(((tick*(tick+1))/2)*11));
- }
- void time_func()
- {
- tick2=SDL_GetTicks();
- tick = initialTime + ( ( tick2 - tick1 ) / 1000 );
- sprintf(times, "TIME: %ds", tick);
- SDL_Color c={2, 36, 37};
- backgrounds[ticks].loadFromRenderedText(times, c);
- backgrounds[ticks].render(screen_width/2 -50, 630);
- }
- void clear_file()
- {
- FILE *p=fopen("game_save_info.txt", "w");
- fprintf(p, "%d\n", 0);
- fclose(p);
- return ;
- }
- int level1()
- {
- tick1=SDL_GetTicks();
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- bool back_button=false;
- int level=1, ano;
- level_id = level_one;
- if(!load_saved_game)
- {
- score=0; prevscore=0;
- dots.number_of=30;
- cnt=0;
- platforms.number_of=25;
- death.number_of=15;
- set1(a, b);
- set_death(death_x, death_y);
- character.posX=0;
- character.posY=580;
- character.velX=0;
- character.velY=0;
- memset(dots.id, -1, sizeof(dots.id));
- memset(death.id, -1, sizeof(death.id));
- }
- else load_saved_game=false;
- character.velX=0;
- character.velY=0;
- platforms.number_of=25;
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- const Uint8* ks = SDL_GetKeyboardState( NULL );
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- {
- quit=true;
- return end;
- }
- else if(e.type==SDL_MOUSEBUTTONDOWN)
- {
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>20 && x<100 && y>620 && y<680)
- {
- int save_game_response = save_game();
- if(save_game_response==no)
- {
- clear_file();
- initialTime=0;
- score_calc();
- return menu;
- }
- else if(save_game_response==end)
- {
- return end;
- }
- else
- {
- return menu;
- }
- }
- }
- else ano=event_id(level, ks, e);
- }
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>20 && x<100 && y>620 && y<680)
- back_button=true;
- if(ano==end) return end;
- int count=0;
- for(int k=0; k<=death.number_of; k++)
- {
- if(death.id[k]==0)
- count++;
- if(count==5)
- {
- clear_file();
- score_calc();
- initialTime=0;
- return gameover;
- }
- }
- if(dots.id[30]==0)
- {
- clear_file();
- score_calc();
- initialTime=0;
- return nextlevel;
- }
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- build_level1();
- character.render();
- if(back_button) backgrounds[rback].render(20, 630);
- else
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- back_button=false;
- }
- }
- void rect(SDL_Rect ground[], int index, int x, int y, int w, int h, int i, int j, int k, int show_flag)
- {
- if(show_flag){
- ground[index]={x, y, w, h};
- SDL_SetRenderDrawColor(game_renderer, i , j , k , 255);
- SDL_RenderFillRect(game_renderer, &ground[index]);
- }
- }
- void set1(int a[], int b[])
- {
- srand(SDL_GetTicks());
- for(int i=0; i<=dots.number_of-1; i++)
- {
- a[i]=rand();
- a[i]%=1270;
- b[i]=rand();
- b[i]%=570;
- }
- }
- void build_level1()
- {
- a[30]=1270; b[30]=150;
- int c[50], d[50];
- c[0]=170; d[0]=580;
- c[1]=190; d[1]=480;
- c[2]=300; d[2]=500;
- c[3]=500; d[3]=540;
- c[4]=640; d[4]=510;
- c[5]=780; d[5]=490;
- c[6]=900; d[6]=530;
- c[7]=1180; d[7]=490;
- c[8]=220; d[8]=410;
- c[9]=20; d[9]=220;
- c[10]=150; d[10]=300;
- c[11]=780; d[11]=350;
- c[12]=0; d[12]=410;
- c[13]=800; d[13]=150;
- c[14]=1230; d[14]=430;
- c[15]=1020; d[15]=350;
- c[16]=690; d[16]=400;
- c[17]=910; d[17]=160;
- c[18]=480; d[18]=220;
- c[19]=1100; d[19]=50;
- c[20]=440; d[20]=340;
- c[21]=900; d[21]=250;
- c[22]=700; d[22]=250;
- c[23]=210; d[23]=150;
- c[24]=580; d[24]=50;
- c[25]=0; d[25]=600;
- for(int k=0; k<=dots.number_of-1; k++)
- {
- for(int m=0; m<platforms.number_of; m++)
- {
- if(a[k]>(c[m]-20) && a[k]<(c[m]+80) && b[k]>(d[m]-20) && b[k]<(d[m]+5))
- {
- b[k]-=20;
- }
- }
- }
- for(int k=0; k<=death.number_of; k++)
- {
- for(int m=0; m<platforms.number_of; m++)
- {
- if(death_x[k]>(c[m]-20) && death_x[k]<(c[m]+80) && death_y[k]>(d[m]-20) && death_y[k]<(d[m]+5))
- {
- death_y[k]-=20;
- }
- }
- }
- int i;
- for(i=0; i<=dots.number_of-1; i++)
- {
- rect(dots.a, i, a[i], b[i], 20, 20, 255, 106, 128, dots.id[i]);
- if(dots.id[i])backgrounds[point].render(a[i], b[i]);
- }
- rect(dots.a, i, a[i], b[i], 10, 20, 68, 85, 90, dots.id[i]);
- for(i=0; i<=24; i++)
- {
- rect(platforms.a, i, c[i], d[i], 50, 5, 175, 175, 175, 1);
- }
- rect(platforms.a, i, c[i], d[i], screen_width, 80, 2, 36, 47, 1);
- rect(platforms.a, i+1, 0, 625, screen_width, 680-625, 175, 175, 199, 1);
- for(i=0; i<=death.number_of; i++)
- {
- rect(death.a, i, death_x[i], death_y[i], 20, 20, 100, 255, 50, death.id[i]);
- if(death.id[i])backgrounds[bomb].render(death_x[i], death_y[i]);
- }
- }
- int game_over()
- {
- bool menu_button=false;
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- return end;
- if(e.type== SDL_MOUSEBUTTONDOWN)
- {
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>700 && x<(700+200) && y>270 && y<470)
- return menu;
- }
- }
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>700 && x<(700+200) && y>270 && y<470)
- {
- menu_button=true;
- }
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- backgrounds[gameover].render(380, 270);
- if(menu_button) backgrounds[rmenu].render(700, 270);
- else
- backgrounds[menu].render(700, 270);
- SDL_RenderPresent(game_renderer);
- menu_button=false;
- }
- }
- void build_level2()
- {
- a[dots.number_of]=1270; b[dots.number_of]=150;
- int c[500], d[500];
- c[0]=0; d[0]=340;
- c[1]=60; d[1]=280;
- c[2]=120; d[2]=220;
- c[3]=180; d[3]=160;
- c[4]=240; d[4]=100;
- c[5]=360; d[5]=100;
- c[6]=420; d[6]=160;
- c[7]=480; d[7]=220;
- c[8]=540; d[8]=280;
- c[9]=600; d[9]=340;
- c[10]=660; d[10]=400;
- c[11]=720; d[11]=460;
- c[12]=780; d[12]=520;
- c[13]=840; d[13]=580;
- c[14]=970; d[14]=580;
- c[15]=1030; d[15]=520;
- c[16]=1090; d[16]=460;
- c[17]=1150; d[17]=400;
- c[18]=1210; d[18]=340;
- c[19]=60; d[19]=400;
- c[20]=120; d[20]=460;
- c[21]=180; d[21]=520;
- c[22]=240; d[22]=580;
- c[23]=360; d[23]=580;
- c[24]=420; d[24]=520;
- c[25]=480; d[25]=460;
- c[26]=540; d[26]=400;
- c[27]=600; d[27]=340;
- c[28]=660; d[28]=280;
- c[29]=720; d[29]=220;
- c[30]=780; d[30]=160;
- c[31]=840; d[31]=100;
- c[32]=970; d[32]=100;
- c[33]=1030; d[33]=160;
- c[34]=1090; d[34]=220;
- c[35]=1150; d[35]=280;
- c[36]=1210; d[36]=340;
- c[37]=0; d[37]=620;
- c[38]=0; d[38]=600;
- for(int k=0; k<=dots.number_of-1; k++)
- {
- for(int m=0; m<platforms.number_of; m++)
- {
- if(a[k]>(c[m]-20) && a[k]<(c[m]+80) && b[k]>(d[m]-20) && b[k]<(d[m]+5))
- {
- b[k]-=20;
- }
- }
- }
- for(int k=0; k<=death.number_of; k++)
- {
- for(int m=0; m<platforms.number_of; m++)
- {
- if(death_x[k]>(c[m]-20) && death_x[k]<(c[m]+80) && death_y[k]>(d[m]-20) && death_y[k]<(d[m]+5))
- {
- death_y[k]-=20;
- }
- }
- }
- int i;
- for(i=0; i<dots.number_of; i++)
- {
- rect(dots.a, i, a[i], b[i], 20, 20, 255, 106, 128, dots.id[i]);
- if(dots.id[i]) backgrounds[point].render(a[i], b[i]);
- }
- rect(dots.a, i, a[i], b[i], 10, 20, 68, 85, 90, dots.id[i]);
- //rect(dots.a, i+1, a[i+1], b[i+1], screen_width, 40, 21, 244, 238, dots.id[i+1]);
- for(i=0; i<platforms.number_of; i++)
- {
- rect(platforms.a, i, c[i], d[i], 80, 5, 175, 175, 175, 1);
- }
- rect(platforms.a, i, c[i], d[i], screen_width, 80, 175, 175, 175, 1);
- rect(platforms.a, i, c[i+1], d[i+1], screen_width, 20, 0xa1, 0x24, 0x24, 1);
- for(i=0; i<=death.number_of; i++)
- {
- rect(death.a, i, death_x[i], death_y[i], 10, 10, 100, 255, 50, death.id[i]);
- if(death.id[i])backgrounds[bomb].render(death_x[i], death_y[i]);
- }
- }
- int level2()
- {
- tick1=SDL_GetTicks();
- bool back_button = false;
- level_id=level_two;
- int level=2, ano;
- if(!load_saved_game)
- {
- cnt=0; prevscore=score;
- dots.number_of=35;
- platforms.number_of=37;
- death.number_of=17;
- set1(a, b);
- set_death(death_x, death_y);
- character.posX=0;
- character.posY=320;
- character.velX=0;
- character.velY=0;
- memset(dots.id, -1, sizeof(dots.id));
- memset(death.id, -1, sizeof(death.id));
- }
- else load_saved_game=false;
- character.velX=0;
- character.velY=0;
- platforms.number_of=37;
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- const Uint8* ks = SDL_GetKeyboardState( NULL );
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- {
- quit=true;
- return end;
- }
- else if(e.type==SDL_MOUSEBUTTONDOWN)
- {
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>20 && x<100 && y>620 && y<680)
- {
- int save_game_response = save_game();
- if(save_game_response==no)
- {
- clear_file();
- score_calc();
- initialTime=0;
- return menu;
- }
- else if(save_game_response==end)
- {
- return end;
- }
- else
- {
- return menu;
- }
- }
- }
- else ano=event_id(level, ks, e);
- }
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>20 && x<100 && y>620 && y<680)
- back_button=true;
- if(ano==end)
- return end;
- int count=0;
- for(int k=0; k<=death.number_of; k++)
- {
- if(death.id[k]==0)
- count++;
- if(count==5)
- {
- score_calc();
- initialTime=0;
- clear_file();
- return gameover;
- }
- }
- if(dots.id[dots.number_of]==0)
- {
- score_calc();
- initialTime=0;
- clear_file();
- return nextlevel;
- }
- if(level==2 && character.posY>=580)
- {
- score_calc();
- initialTime=0;
- clear_file();
- return gameover;
- }
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- build_level2();
- character.render();
- if(back_button) backgrounds[rback].render(20, 630);
- else
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- back_button=false;
- }
- }
- int next_level()
- {
- bool next_level_button=false, menu_button=false;
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- return end;
- if(e.type== SDL_MOUSEBUTTONDOWN)
- {
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>700 && x<(700+200) && y>270 && y<470)
- return menu;
- if(x>380 && x<(580) && y>270 && y<470)
- return nextlevel;
- }
- }
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>700 && x<(700+200) && y>270 && y<470)
- {
- menu_button=true;
- }
- else if(x>380 && x<(580) && y>270 && y<470)
- {
- next_level_button=true;
- }
- SDL_SetRenderDrawColor(game_renderer, 255, 255, 255, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- if(next_level_button) backgrounds[rnextlevel].render(380, 270);
- else
- backgrounds[nextlevel].render(380, 270);
- if(menu_button) backgrounds[rmenu].render(700, 270);
- else
- backgrounds[menu].render(700, 270);
- SDL_RenderPresent(game_renderer);
- next_level_button=false;
- menu_button=false;
- }
- }
- void sort(score_structure a[])
- {
- for(int b=1; b<=6; b++)
- {
- for(int k=1; k<6-b+1; k++)
- {
- if(a[k].i<a[k+1].i)
- {
- score_structure temp=a[k];
- a[k]=a[k+1];
- a[k+1]=temp;
- }
- }
- }
- }
- int input_name(score_structure sc[], int k)
- {
- bool quit = false;
- //Event handler
- SDL_Event e;
- //Set text color as black
- SDL_Color textColor = { 255, 255, 255, 0xFF };
- //The current input text.
- std::string inputText = "Anonymous";
- backgrounds[input].loadFromRenderedText( inputText.c_str(), textColor );
- backgrounds[text].loadFromRenderedText("NEW HIGH SCORE ! ENTER NAME : ", textColor);
- //Enable text input
- SDL_StartTextInput();
- //While application is running
- while( !quit )
- {
- //The rerender text flag
- bool renderText = false;
- //Handle events on queue
- while( SDL_PollEvent( &e ) != 0 )
- {
- //User requests quit
- if( e.type == SDL_QUIT )
- {
- quit = true;
- return end;
- }
- //Special key input
- else if( e.type == SDL_KEYDOWN )
- {
- //Handle backspace
- if( e.key.keysym.sym == SDLK_BACKSPACE && inputText.length() > 0 )
- {
- //lop off character
- inputText.pop_back();
- renderText = true;
- }
- //Handle copy
- else if( e.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
- {
- SDL_SetClipboardText( inputText.c_str() );
- }
- //Handle paste
- else if( e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
- {
- inputText = SDL_GetClipboardText();
- renderText = true;
- }
- else if(e.key.keysym.sym==SDLK_RETURN)
- {
- SDL_StopTextInput();
- strcpy(sc[k].a, inputText.c_str());
- return anything;
- }
- }
- //Special text input event
- else if( e.type == SDL_TEXTINPUT )
- {
- //Not copy or pasting
- if( !( SDL_GetModState() & KMOD_CTRL && ( e.text.text[ 0 ] == 'c' || e.text.text[ 0 ] == 'C' || e.text.text[ 0 ] == 'v' || e.text.text[ 0 ] == 'V' ) ) )
- {
- //Append character
- inputText += e.text.text;
- renderText = true;
- }
- }
- }
- //Rerender text if needed
- if( renderText )
- {
- //Text is not empty
- if( inputText != "" )
- {
- //Render new text
- backgrounds[input].loadFromRenderedText( inputText.c_str(), textColor );
- }
- //Text is empty
- else
- {
- //Render space texture
- backgrounds[input].loadFromRenderedText( " ", textColor );
- }
- }
- //Clear screen
- SDL_SetRenderDrawColor( game_renderer, 0xFF, 0xFF, 0xFF, 0xFF );
- SDL_RenderClear(game_renderer );
- //Render text textures
- backgrounds[background].render(0, 0);
- backgrounds[text].render( ( screen_width - backgrounds[text].getWidth() ) / 2, 100 );
- backgrounds[input].render( ( screen_width - backgrounds[input].getWidth() ) / 2, 200 );
- //Update screen
- SDL_RenderPresent( game_renderer);
- }
- }
- int score_display()
- {
- char dis[1000], temp[100];
- score_structure sc[10];
- SDL_Color textColor;
- int k;
- FILE *p=fopen("score.txt", "r");
- for(k=1; k<=5; k++)
- {
- fscanf(p, "%d %s", &sc[k].i, sc[k].a);
- }
- sc[k].i=score; strcpy(sc[k].a, "aiosd");
- sort(sc);
- for(k=1; k<=5; k++)
- {
- if(sc[k].i==score)
- {
- int ret=input_name(sc, k);
- if(ret==end) return end;
- sprintf(dis, "NEW HIGH SCORE : %d", score);
- break;
- }
- }
- p=fopen("score.txt", "w");
- for(k=1; k<=5; k++){
- fprintf(p, "%d %s\n", sc[k].i, sc[k].a);
- }
- fclose(p);
- if(score>=0 && k>5)
- {
- sprintf(dis, "SCORE : %d", score);
- }
- else if(score<0)
- {
- SDL_Color textColor={0xa1, 0x24, 0x24};
- sprintf(dis, "SCORE : %d EPIC FAILURE", score);
- }
- if(score>=0) textColor={255, 255, 255};
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- {
- quit=true;
- return end;
- }
- if(e.type==SDL_KEYDOWN)
- {
- if(e.key.keysym.sym==SDLK_RETURN)
- {
- quit=true;
- return menu;
- }
- }
- }
- SDL_SetRenderDrawColor(game_renderer, 0, 255, 237, 255);
- SDL_RenderClear(game_renderer);
- backgrounds[score__].render(0, 0);
- backgrounds[Score].loadFromRenderedText(dis, textColor);
- backgrounds[Score].render(screen_width/4, screen_height/4);
- SDL_RenderPresent(game_renderer);
- }
- }
- void score_box()
- {
- score_calc();
- char s[100];
- sprintf(s, "SCORE:%d", score);
- SDL_Color c={2, 36, 37};
- backgrounds[box].loadFromRenderedText(s, c);
- backgrounds[box].render(200, 630);
- }
- void build_level3()
- {
- a[dots.number_of]=1270; b[dots.number_of]=150;
- int c[500], d[500];
- for(int k=0; k<platforms.number_of; k++)
- {
- if(k%2==0) c[k]=0;
- else c[k]=screen_width/2 + 60;
- }
- int h=70; int k;
- for(k=0; k<platforms.number_of-1; k++)
- {
- if(k==0) d[k]=h;
- else {
- d[k]=h+d[k-1];
- }
- }
- c[k]=0; d[k]=600;
- c[k+1]=0; d[k+1]=620;
- for(k=0; k<platforms.number_of-1; k++)
- {
- rect(platforms.a, k, c[k], d[k], screen_width/2, 5, 175, 175 ,175, 1);
- }
- rect(platforms.a, k, c[k], d[k], screen_width, 20, 0xa1, 0x24, 0x24, 1);
- rect(platforms.a, k+1, c[k+1], d[k+1], screen_width, 60, 175, 175, 175, 1 );
- for( k=0; k<=dots.number_of-1; k++)
- {
- for(int m=0; m<platforms.number_of; m++)
- {
- if(a[k]>(c[m]-20) && a[k]<(c[m]+80) && b[k]>(d[m]-20) && b[k]<(d[m]+5))
- {
- b[k]-=20;
- }
- }
- }
- for( k=0; k<=death.number_of; k++)
- {
- for(int m=0; m<platforms.number_of; m++)
- {
- if(death_x[k]>(c[m]-20) && death_x[k]<(c[m]+80) && death_y[k]>(d[m]-20) && death_y[k]<(d[m]+5))
- {
- death_y[k]-=20;
- }
- }
- }
- int i;
- for(i=0; i<=death.number_of; i++)
- {
- rect(death.a, i, death_x[i], death_y[i], 10, 10, 100, 255, 50, death.id[i]);
- if(death.id[i])backgrounds[bomb].render(death_x[i], death_y[i]);
- }
- for(i=0; i<dots.number_of; i++)
- {
- rect(dots.a, i, a[i], b[i], 20, 20, 255, 106, 128, dots.id[i]);
- if(dots.id[i]) backgrounds[point].render(a[i], b[i]);
- }
- rect(dots.a, i, a[i], b[i], 20, 20, 255, 106, 128, dots.id[i]);
- }
- int level3()
- {
- tick1=SDL_GetTicks();
- bool back_button = false;
- level_id=level_two;
- int level=3, ano;
- if(!load_saved_game)
- {
- cnt=0; prevscore=score;
- dots.number_of=35;
- platforms.number_of=10;
- death.number_of=17;
- set1(a, b);
- set_death(death_x, death_y);
- character.posX=1250;
- character.posY=520;
- character.velX=0;
- character.velY=0;
- memset(dots.id, -1, sizeof(dots.id));
- memset(death.id, -1, sizeof(death.id));
- }
- else load_saved_game=false;
- character.velX=0;
- character.velY=0;
- platforms.number_of=10;
- bool quit=false;
- SDL_Event e;
- while(!quit)
- {
- const Uint8* ks = SDL_GetKeyboardState( NULL );
- while(SDL_PollEvent(&e)!=0)
- {
- if(e.type==SDL_QUIT)
- {
- quit=true;
- return end;
- }
- else if(e.type==SDL_MOUSEBUTTONDOWN)
- {
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>20 && x<100 && y>620 && y<680)
- {
- int save_game_response = save_game();
- if(save_game_response==no)
- {
- clear_file();
- score_calc();
- initialTime=0;
- return menu;
- }
- else if(save_game_response==end)
- {
- return end;
- }
- else
- {
- return menu;
- }
- }
- }
- else ano=event_id(level, ks, e);
- }
- int x, y;
- SDL_GetMouseState(&x, &y);
- if(x>20 && x<100 && y>620 && y<680)
- back_button=true;
- if(ano == end)
- return end;
- int count=0;
- for(int k=0; k<=death.number_of; k++)
- {
- if(death.id[k]==0)
- count++;
- if(count==5)
- {
- score_calc();
- initialTime=0;
- clear_file();
- return gameover;
- }
- }
- if(dots.id[dots.number_of]==0)
- {
- score_calc();
- initialTime=0;
- clear_file();
- return nextlevel;
- }
- if(level==3 && character.posY>=580)
- {
- score_calc();
- initialTime=0;
- clear_file();
- return gameover;
- }
- SDL_RenderClear(game_renderer);
- backgrounds[background].render(0, 0);
- build_level3();
- character.render();
- if(back_button) backgrounds[rback].render(20, 630);
- else
- backgrounds[back].render(20, 630);
- time_func();
- score_box();
- if(cnt!=0) green();
- SDL_RenderPresent(game_renderer);
- back_button=false;
- }
- }
- int main(int argc, char* args[])
- {
- int indicator=1;
- int levelUpVar=menu;
- if(init())
- {
- if(loadMedia())
- {
- while(levelUpVar)
- {
- if(levelUpVar==menu)
- {
- score=0; prevscore=0;
- levelUpVar=Menu();
- }
- if(levelUpVar==level_one)
- {
- levelUpVar=level1();
- if(levelUpVar!=end && levelUpVar!=nextlevel) indicator=score_display();
- if(indicator==end) levelUpVar=end;
- if(levelUpVar==gameover)
- {
- cnt=0;
- levelUpVar=game_over();
- }
- else if(levelUpVar==nextlevel)
- {
- levelUpVar=next_level();
- if(levelUpVar==menu)
- levelUpVar=score_display();
- if(levelUpVar==nextlevel)
- {
- levelUpVar=level_two;
- }
- }
- }
- if(levelUpVar==level_two)
- {
- levelUpVar=level2();
- if(levelUpVar!=end && levelUpVar!=nextlevel) indicator=score_display();
- if(indicator==end) levelUpVar=end;
- if(levelUpVar==gameover)
- {
- levelUpVar=game_over();
- }
- else if(levelUpVar==nextlevel)
- {
- levelUpVar=next_level();
- if(levelUpVar==menu)
- levelUpVar=score_display();
- if(levelUpVar==nextlevel)
- {
- levelUpVar=level_three;
- }
- }
- }
- if(levelUpVar==level_three)
- {
- levelUpVar=level3();
- if(levelUpVar!=end) indicator=score_display();
- if(indicator==end) levelUpVar=end;
- if(levelUpVar==gameover)
- {
- levelUpVar=game_over();
- }
- else if(levelUpVar==nextlevel)
- {
- levelUpVar=next_level();
- if(levelUpVar==menu)
- levelUpVar=score_display();
- if(levelUpVar==nextlevel)
- {
- levelUpVar=menu;
- }
- }
- }
- if(levelUpVar==end)
- {
- levelUpVar=0;
- }
- }
- }
- }
- closes();
- return 0;
- }
- /*
- (prototypes)
- texture class
- init
- close
- loadfromfile
- ttf
- free
- render
- getwidth
- getheight
- dummy class
- init
- move()
- render()
- init(SDL)
- loadMedia
- closes()
- menu()
- dummy: handle
- level1
- rect
- build_level1
- main
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement