Browse Source

Add replay current level option

I changed playagain() so that the user can enter either 'y', 'n' or 'q'.
This way, in the main loop I implemented three options:
  - 'y': a new match with the current level of difficulty starts
  - 'n': the main menu is dislpayed
  - 'q': the game ends
Matteo Zeccoli Marazzini 4 years ago
parent
commit
254015718c
3 changed files with 63 additions and 45 deletions
  1. 4 8
      functions.cpp
  2. 1 1
      functions.hpp
  3. 58 36
      invaders.cpp

+ 4 - 8
functions.cpp

@@ -425,22 +425,18 @@ void load_enemies(e_list& enemies,int enemy_num)
 	}
 }
 		
-bool playagain(WINDOW* replay)     
+char playagain(WINDOW* replay)
 {
 	char answer;
 	
 	do
 	{
 		wattron(replay,COLOR_PAIR(2));
-		mvwprintw(replay,1,4,"Play again? [y/n] ");
+		mvwprintw(replay,1,3,"Play again? [y/n/q] ");
 		answer=tolower(wgetch(replay));
-		if(answer=='y')
-			return true;
-		if(answer=='n')
-			return false;
-	}while(answer!='y'&&answer!='n');
+	}while(answer!='y' && answer!='n' && answer!='q');
 	
-	return false;	//just to get rid of the compiler's warning
+	return answer;
 }
 
 bool gameover(player& _player,b_list& bombs)		//Checks if a bomb/enemy/boss hit the player.

+ 1 - 1
functions.hpp

@@ -57,7 +57,7 @@ void change_commands(int* commands);
 
 void load_enemies(e_list& enemies,int enemy_num);
 
-bool playagain(WINDOW*);
+char playagain(WINDOW*);
 
 bool gameover(player&,b_list&);
 

+ 58 - 36
invaders.cpp

@@ -255,26 +255,37 @@ int main(int argc,char** argv)
 			replay=newwin(3,25,26,20);
 			box(replay,ACS_VLINE,ACS_HLINE);
 		
-			if(playagain(replay))                           //playagain() returns true if player wants to play again, false otherwise
+			switch(playagain(replay))	// playagain() returns 'y', 'n' or 'q'
 			{
-				delwin(replay);
-				timeout(500);
-				score=0;
-				erase();
-				refresh();
-				level = choose_level(commands);     	// get desired level
-				setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
-				load_enemies(enemies,ENEMY_NUM);
-				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
-				erase();
-				refresh();
-				continue;  
+				case 'y':
+					delwin(replay);
+					timeout(500);
+					score=0;
+					erase();
+					refresh();
+					load_enemies(enemies,ENEMY_NUM);
+					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					erase();
+					refresh();
+					continue;
+				case 'n':
+					delwin(replay);
+					timeout(500);
+					score=0;
+					erase();
+					refresh();
+					level = choose_level(commands);     	// get desired level
+					setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
+					load_enemies(enemies,ENEMY_NUM);
+					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					erase();
+					refresh();
+					continue;
+				case 'q':
+					endwin();
+					cout<<lightgreen<<"Game exited correctly."<<none<<endl;
+					return 0;
 			}
-			else { 
-				endwin();
-				cout<<lightgreen<<"Game exited correctly."<<none<<endl;
-				return 0;
-			}	
 		}
 				
 			
@@ -287,25 +298,36 @@ int main(int argc,char** argv)
 			replay=newwin(3,25,17,20);
 			box(replay,ACS_VLINE,ACS_HLINE);
 			
-			if(playagain(replay))                                        //playagain() returns true if player wants to play again, false otherwise
+			switch(playagain(replay))                           //playagain() returns 'y', 'n' or 'q'
 			{
-				delwin(replay);
-				timeout(500);
-				score=0;
-				erase();
-				refresh();
-				level = choose_level(commands);     	// get desired level
-				setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
-				load_enemies(enemies,ENEMY_NUM);
-				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets,level,chflag);       // reset box, player, enemy and delete all bullets and bombs
-				erase();
-				refresh();
-				continue;  
-			}
-			else {
-				endwin();
-				cout<<lightgreen<<"Game exited correctly."<<none<<endl;
-				return 0;
+				case 'y':
+					delwin(replay);
+					timeout(500);
+					score=0;
+					erase();
+					refresh();
+					load_enemies(enemies,ENEMY_NUM);
+					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					erase();
+					refresh();
+					continue;
+				case 'n':
+					delwin(replay);
+					timeout(500);
+					score=0;
+					erase();
+					refresh();
+					level = choose_level(commands);     	// get desired level
+					setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
+					load_enemies(enemies,ENEMY_NUM);
+					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					erase();
+					refresh();
+					continue;
+				case 'q':
+					endwin();
+					cout<<lightgreen<<"Game exited correctly."<<none<<endl;
+					return 0;
 			}
 		}