Пример #1
0
//finally here. Whew that's a lot of functions
int main(int argc,char* args[]) {
    srand(time(NULL));  	//spin the wheel!
    int i;                  //loop counter
    int frame=0;            //total frames past

    if(init()==false) return 1;
    if(prepAssets()==false) return 1;
    if(Mix_PlayMusic(muBGM,-1)==-1) return 1;

    //read and store the string of appropriate language
    FILE *pLang;
    char strLang[25];       //language string
    if((pLang=fopen("text/en.WhyCantIHoldAllTheseFileExtensions","r"))!=NULL) {
        if(fgets(strLang,25,pLang)==NULL) return 1;
    }
    fclose(pLang);
    sfMenuPrompt=TTF_RenderText_Blended(fnMenu,strLang,clMenu);

    //read and store current highscore
    FILE *pHighScoreR;
    char strHighScore[10];
    if((pHighScoreR=fopen("text/highscore.WhyCantIHoldAllTheseFileExtensions","r"))!=NULL) {
        if(fgets(strHighScore,10,pHighScoreR)==NULL) return 1;
    }
    fclose(pHighScoreR);
    sfHighScore=TTF_RenderText_Shaded(fnHighScore,strHighScore,clHighScore,clDefault);
    iHighScore=atoi(strHighScore);		//string contents as an int

    //menu runs here
    while(quitMenu==false) {
        //display menu
        printb(0,0,sfMenu,sfScreen);
        printb((SCREEN_WIDTH-sfMenuPrompt->w)/2,315,sfMenuPrompt,sfScreen);
        //menu-only key controls
        while(SDL_PollEvent(&event)) {
            if(event.type==SDL_KEYDOWN) {
                switch(event.key.keysym.sym) {
                case SDLK_RETURN:
                    quitMenu=true;
                    break;
                case SDLK_ESCAPE:
                    quitMenu=true;
                    quitGame=true;
                    quitOver=true;
                    quitAll=true;
                    break;
                default:
                    ;
                }
            }

            //if the window gets X'd
            if(event.type==SDL_QUIT) {
                quitMenu=true;          //quit the menu
                quitGame=true;          //skip the game
                quitOver=true;
                quitAll=true;
            }
        }

        //refresh the screen
        if(SDL_Flip(sfScreen)==-1) return 1;
    }

    //game is starting! set up everything!
    tmTime.start();
    tmFPS.start();
    tmFPSUpd.start();
    tmDelta.start();
    tmMusic.start();

    //REPLAY LOOP
    while(quitAll==false) {
        randBullets();
        //game runs here
        while(quitGame==false) {
            //once wave time is up: level up and restart wave timer
            //setup phase is active
            if(waveZero==true&&tmTime.getTicks()>10000) {
                iWave++;
                nextWave();
                tmTime.start();
                waveZero=false;
                newBGM();
                tmScore.start();
                tmTimeAlive.start();
            }
            //setup phase is off
            else if(tmTime.getTicks()>WAVE_LENGTH) {
                iWave++;
                nextWave();
                tmTime.start();
            }

            //change music after 90 seconds
            if(tmMusic.getTicks()>90000) {
                newBGM();
                tmMusic.start();
            }

            //score acceleration
            if(tmTimeAlive.getTicks()>30000) iScoreAccel=13;
            else if(tmTimeAlive.getTicks()>15000) iScoreAccel=6;
            else if(tmTimeAlive.getTicks()>7500) iScoreAccel=3;
            else if(tmTimeAlive.getTicks()>0) iScoreAccel=1;

            //score timing
            if(tmScore.getTicks()>250) {
                iScore+=iScoreAccel;
                tmScore.start();
            }

            //1up timing
            if(tmTimeAlive.getTicks()>45000) {
                tmTimeAlive.start();
                iLife++;

                //don't let player have too many lives
                //if 1up is allowed, play sound
                if(iLife>5)iLife=5;
                else if(Mix_PlayChannel(-1,chGain,0)==-1) return 1;
            }

            //while there's science to do
            while(SDL_PollEvent(&event)) {
                //ship controls
                myship.handleInput();

                //other controls
                if(event.type==SDL_KEYDOWN) {
                    switch(event.key.keysym.sym) {
                    case SDLK_ESCAPE:
                        quitGame=true;
                        quitOver=true;
                        quitAll=true;
                        break;
                    case SDLK_x:
                        if(useBomb()==false) return 1;
                        break;
                    default:
                        ;
                    }
                }

                //if the window gets X'd
                if(event.type == SDL_QUIT) {
                    quitGame = true;
                    quitOver=true;
                    quitAll=true;
                }
            }


            //update screen data
            myship.move(tmDelta.getTicks());    //update ship's position
            tmDelta.start();                    //restart change of time timer
            printb(0,0,sfBG,sfScreen);          //print background
            myship.show();                      //print position to screen
            if(diedRecently==true) printb(120,0,sfDeathOverlay,sfScreen,NULL);
            if(bombedRecently==true) printb(120,0,sfBombFlash,sfScreen,NULL);

            if(waveZero==true) {					//reset bullets to original when looping game
                printb(0,0,sfHowTo,sfScreen,NULL);
                iMaxBul=-1;
            }
            for(i=0; i<=iMaxBul; i++) {
                //player has died: do all relevant tracking
                if(isCol(myship.hitbox,b[i].hitbox)) {
                    iLife--;
                    iBomb=3;
                    iScore-=50;
                    if(iLife==0) quitGame=true;
                    diedRecently=true;
                    b[i].hitbox.x=rand()%420-120;
                    b[i].hitbox.y=0;
                    tmDeathOverlay.start();
                    tmTimeAlive.start();
                    if(Mix_PlayChannel(-1,chDeath,0)==-1) return 1;
                }

                if(b[i].hitbox.x>515) b[i].hitbox.x=120;
                if(b[i].hitbox.x<120) b[i].hitbox.x=515;        //compensate for bullet width
                if(b[i].hitbox.y>480) {                         //because collision is counted from sScore of the picture
                    b[i].hitbox.y=0;                            //so bulletwidth had to be subtracted
                    b[i].xVel=rand()%5-2;                      //bullet can travel left or right
                    b[i].yVel=rand()%4+1;                       //can only travel down
                }
                b[i].hitbox.y+=b[i].yVel;
                b[i].hitbox.x+=b[i].xVel;
                printb(b[i].hitbox.x,b[i].hitbox.y,sfBullet,sfScreen,NULL);
            }

            //expiry dates for death and bomb notifications
            if(tmDeathOverlay.getTicks()>500) diedRecently=false;
            if(tmBombFlash.getTicks()>250) bombedRecently=false;

            //display all stats
            renderHUD();
            printb(7,50,sfHighScore,sfScreen,NULL);

            //refresh the screen
            if(SDL_Flip(sfScreen)==-1) return 1;

            //limit the frame rate
            if(tmFPS.getTicks()<1000/FRAMES_PER_SECOND) {
                SDL_Delay((1000/FRAMES_PER_SECOND)-tmFPS.getTicks());
                tmFPS.start();
            }

            frame++;    //one frame has passed

            //update this once per second
            if(tmFPSUpd.getTicks()>1000) {
                std::stringstream newCaption;
                newCaption<<frame/(tmFPS.getTicks()/1000.f)<<" fps";
                SDL_WM_SetCaption(newCaption.str().c_str(),NULL);
                tmFPSUpd.start();         //restart for the next one-second wait
            }
        }

        //store new high score, if there is one
        if(iScore>iHighScore) {
            FILE *pHighScoreW;
            if((pHighScoreW=fopen("text/highscore.WhyCantIHoldAllTheseFileExtensions","w"))!=NULL) {
                if(fprintf(pHighScoreW,"%d",iScore)==0) return 1;
            }
            fclose(pHighScoreW);
            newHighScore=true;
        }

        //stop playing music
        Mix_HaltMusic();

        //game over runs here
        while(quitOver==false) {
            //some key events
            while(SDL_PollEvent(&event)) {
                if(event.type==SDL_KEYDOWN) {
                    switch(event.key.keysym.sym) {
                    case SDLK_RETURN:
                        quitOver=true;
                        break;
                    case SDLK_ESCAPE:
                        quitOver=true;
                        quitAll=true;
                        break;
                    default:
                        ;
                    }
                }

                //if the window gets X'd
                if(event.type == SDL_QUIT) {
                    quitOver=true;
                    quitAll=true;
                }
            }

            //end surfaces
            std::stringstream finalScore;
            finalScore<<iScore;
            sfScore=TTF_RenderText_Blended(fnFinalScore,finalScore.str().c_str(),clMenu);

            //display restart prompt
            FILE *pRestart;
            char strRestart[30];
            if((pRestart=fopen("text/enr.WhyCantIHoldAllTheseFileExtensions","r"))!=NULL) {
                if(fgets(strRestart,30,pRestart)==NULL) return 1;
            }
            fclose(pRestart);
            sfRestart=TTF_RenderText_Blended(fnMenu,strRestart,clScore);

            //print everything
            printb(0,0,sfOverBG,sfScreen,NULL);
            printb((SCREEN_WIDTH-sfRestart->w)/2,385,sfRestart,sfScreen,NULL);
            printb((SCREEN_WIDTH-sfScore->w)/2,240,sfScore,sfScreen,NULL);
            if(newHighScore==true)	printb(430,280,sfNewHigh,sfScreen,NULL);

            //refresh the screen
            if(SDL_Flip(sfScreen)==-1) return 1;

            SDL_WM_SetCaption("Shutengu!!",NULL);
        }

        //reset loop conditions to allow replaying
        resetGame();
    }
    //user has now quit
    cleanUp();

    return 0;
}