int CollisionCheck() { GAME_Pause(); //Collision Check bird.wid = rect.w; bird.x = rect.x; bird.y = rect.y; upobs.wid = (block.upblock).w; upobs.x = (block.upblock).x; upobs.y = (block.upblock).y + (block.upblock).h; dowobs.wid =block. downblock.w; dowobs.x =block. downblock.x; dowobs.y =block. downblock.y ; if (1 == isCol(bird,upobs,dowobs,SCREEN_HEIGHT)) { collisionT++; printf("Collision!# %d \n",collisionT); return 0; } else if (0 == isCol(bird,upobs,dowobs,SCREEN_HEIGHT)) { BirdVeloY = 0.1; SetSDLRect(&rect,rect.x,0,NULL,NULL); } GAME_Resume(); return 0; }
bool TicTacToe::isGameOver(int r, int c, Player& p) { int s = p.getSymbol(); if(isFDiag(s) || isRDiag(s) || isRow(r, s) || isCol(c, s)) { cout << "Winner is Player:" << p.getSymbol() << endl; return true; } else return false; }
//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; }
Matrix *UserSpace::loadFrom(const char *filename, ...) { ifstream sfile(filename, ios::in); string line; Matrix *curM; int state = 0; string varName; int rows(0), cols(0); vector<OM_SUPPORT_TYPE> elements; int curRows(0), curCols(0); int error = 0; while(getline(sfile, line)) { if (line.size() == 0) continue; if (state == 0) // expecting a new matrix { if (isComment(line)) { curRows = 0; curCols = 0; if(containName(line)) { varName = getName(line); state = 0; } else if (isRow(line)) { rows = getRows(line); state = 0; } else if (isCol(line)) { cols = getCols(line); state = 0; } } else { curCols = readDataLine(elements, line); curRows++; if (cols != 0 && cols != curCols) { error = 1; break; } state = 1; } } else if (state = 1) // in the middle of reading data { if (isComment(line)) // a new matrix starts { if (curRows > 0) { if (rows != 0 && rows != curRows) { error = 1; break; } // store the data int dims[2]; dims[0] = curRows; dims[1] = curCols; const OM_SUPPORT_TYPE *e = &elements[0]; Matrix *m = new Matrix(NULL, 2, dims, e); if (varName.size() != 0) updateVar(varName.c_str(), m); else assert(0); // todo } curRows = 0; curCols = 0; varName = ""; state = 0; elements.clear(); // processing the current line if(containName(line)) { varName = getName(line); state = 0; } else if (isRow(line)) { rows = getRows(line); state = 0; } else if (isCol(line)) { cols = getCols(line); state = 0; } } else { // read data curCols = readDataLine(elements, line); curRows++; if (cols != 0 && cols != curCols) throw ExeException("Error loading the file\n"); state = 1; } } } if (state = 1) { if (curRows > 0) { if (rows != 0 && rows != curRows) { error = 1; } else { // store the data int dims[2]; dims[0] = curRows; dims[1] = curCols; Matrix *m = new Matrix(NULL, 2, dims, &elements[0]); if (varName.size() != 0) updateVar(varName.c_str(), m); else assert(0); // todo } } } sfile.close(); if (error == 1) throw ExeException("Error loading the file."); }