diff --git a/11. februarie 2015 RA postdoc - Copy - Copy.doc b/11. februarie 2015 RA postdoc - Copy - Copy.doc deleted file mode 100644 index ab90049..0000000 Binary files a/11. februarie 2015 RA postdoc - Copy - Copy.doc and /dev/null differ diff --git a/Map.txt b/Map.txt new file mode 100644 index 0000000..1367a0d --- /dev/null +++ b/Map.txt @@ -0,0 +1 @@ +######################################### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## W ######################################### \ No newline at end of file diff --git a/Map1.txt b/Map1.txt new file mode 100644 index 0000000..56a54da --- /dev/null +++ b/Map1.txt @@ -0,0 +1 @@ +######################################### ## Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y ## ## ## ## ## ## ## ## ## ## ## ## |---| |---| |---| ## | | | | | | ## W ######################################### \ No newline at end of file diff --git a/Map2.txt b/Map2.txt new file mode 100644 index 0000000..0a3b473 --- /dev/null +++ b/Map2.txt @@ -0,0 +1 @@ +######################################### ## Y Y Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y ## ## ## ## ## ## ## ## ## ## |---| |---| |---| ## | | | | | | ## W ######################################### \ No newline at end of file diff --git a/Map3.txt b/Map3.txt new file mode 100644 index 0000000..7ec024a --- /dev/null +++ b/Map3.txt @@ -0,0 +1 @@ +######################################### ## Y Y Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y Y ## Y Y Y Y Y Y Y ## Y Y Y Y Y Y ## ## ## ## ## ## ## |---| |---| |---| ## | | | | | | ## W ######################################### \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index a577bf3..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# ComputerProgrammingLab -Programs developed during the Computer Programming Laboratory at the Faculty of Automation, Computers and Electronics within the University Of Craiova diff --git a/Resume-var.txt b/Resume-var.txt new file mode 100644 index 0000000..e69de29 diff --git a/Resume.txt b/Resume.txt new file mode 100644 index 0000000..d4a8f95 --- /dev/null +++ b/Resume.txt @@ -0,0 +1 @@ +######################################### ^^^ ## ^^^ ## ^^^ ## ^^^ ## ^^^ ## ## ## ## ## ## Y Y ## Y ## ## ## ## |- - ## | | ## W ######################################### \ No newline at end of file diff --git a/Space Invader.cpp b/Space Invader.cpp new file mode 100644 index 0000000..9badb68 --- /dev/null +++ b/Space Invader.cpp @@ -0,0 +1,1677 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +void init_map1(); +void init_map2(); +void init_map3(); +void display_map(); +void new_game(); +void display_meniu(); +void move_player_right(); +void move_player_left(); +void move_player_up(); +void move_player_down(); +void GotoXY(int x, int y); +void delete_coord(int x, int y); +void move_enemy(); +void move_enemy_right(); +void move_enemy_left(); +void move_enemy_down(); +void player_shoot(); +void player_shoot_2(); +void player_shoot_3(); +void player_bullet(); +void enemy_shoot(); +void enemy_bullet(); +void end_war(); +void init_high_score(); +void number_enemy(); +void init_map_resume(); +void resume_save_map(); +void save_top_score(); +void display_top_score(); +void display_bonus(); +void display_malus(); + + +char map[20][40]; +int i, j; +int k_meniu=0; +bool end_game=false; +int speed_game=100; +char direction_enemy; +int lives=3; +int k_enemy=0; +int down; +int last_line_enemy; +long high_score,v_high_score[10]; +long score=0; +int resume=0; +int level; +int var_random; +bool bonus=false; +bool malus=false; +int col_bonus; +int col_malus; +int k_bonus=0,k_malus=0; +int k_shoot=1; + +HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); //help change color + +HANDLE hOut; + +struct coordonate{ // memorises player's position + int x,y; +}; + +struct coordonate poz_player; + +int main() +{ + hOut = GetStdHandle(STD_OUTPUT_HANDLE); + + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); //change color in green + + srand ( time(NULL) ); // prevent random numbers to be the same every time + + display_meniu(); + + return 0; +} +//Initialize the map for level 1 +void init_map1() +{ + FILE *map1; + map1=fopen("Map1.txt", "r"); + while ( !feof(map1) ){ + for (i = 0; i < 20; i++){ + for (j = 0; j < 40; j++){ + fscanf(map1, "%c", &map[i][j]); + } + } + } + fclose(map1); +} +//Initialize the map for level 2 +void init_map2() +{ + FILE *map2; + map2=fopen("Map2.txt", "r"); + while ( !feof(map2) ){ + for (i = 0; i < 20; i++){ + for (j = 0; j < 40; j++){ + fscanf(map2, "%c", &map[i][j]); + } + } + } + fclose(map2); +} +//Initialize the map for level 3 +void init_map3() +{ + FILE *map3; + map3=fopen("Map3.txt", "r"); + while ( !feof(map3) ){ + for (i = 0; i < 20; i++){ + for (j = 0; j < 40; j++){ + fscanf(map3, "%c", &map[i][j]); + } + } + } + fclose(map3); +} +//Initialize the map after resume +void init_map_resume() +{ + FILE *resume_map; + resume_map=fopen("Resume.txt", "r"); + while ( !feof(resume_map) ){ + for (i = 0; i < 20; i++){ + for (j = 0; j < 40; j++){ + fscanf(resume_map, "%c", &map[i][j]); + } + } + } + fclose(resume_map); + + ifstream dir("direction.txt"); //reads the direction of movement of the enemy + dir>>direction_enemy; + dir.close(); + + ifstream scor("score.txt"); //reads the score + scor>>score; + scor.close(); + + ifstream lev("level.txt"); //reads the level + lev>>level; + lev.close(); + + ifstream VR("variabila_random.txt"); // reads the random variable + VR>>var_random; + VR.close(); + + ifstream shoot("shoot.txt"); // reads the number of bullets that you shoot + shoot>>k_shoot; + shoot.close(); + + ifstream lif("life.txt"); // reads your life + lif>>lives; + lif.close(); +} +//Save the map to return to menu +void resume_save_map() +{ + ofstream fisier("Resume.txt"); + for (i = 0; i < 20; i++){ + for (j = 0; j < 40; j++){ + fisier<< map[i][j]; + } + } + fisier.close(); + + ofstream dir("direction.txt"); //Saves enemies direction: left or right + dir<> v_high_score[q]; + } + Top_Score_file.close(); + high_score=v_high_score[0]; +} + +//Saves highest 10 scores +void save_top_score() +{ + ofstream Top_Score_file("Top-Score.txt"); + for( int q=0;q<10;q++ ){ + Top_Score_file << v_high_score[q]<<" "; + } + Top_Score_file.close(); +} + +void display_top_score() +{ + + COORD l = GetLargestConsoleWindowSize(hOut); // helps to write in the middle of console + int l1 = l.X; + + if( score > high_score ){ //check if is the highest score + high_score=score; + + for( int q=9;q>=1;q-- ){ + v_high_score[q]=v_high_score[q-1]; + } + + v_high_score[0]=high_score; //Put your score on second place + save_top_score(); + + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); //change color in red + cout<<"\n\n\n\n\n\n NEW HIGH SCORE : "<=2;q-- ){ + v_high_score[q]=v_high_score[q-1]; + } + v_high_score[1]=score; //Put your score on second place + save_top_score(); + + system("cls"); + cout<<"\n\n\n\n\n\n"; + cout << setw(l1 / 4) << " " << "Top score\n\n"; + + int ok=0; + for( int q=0;q<10;q++ ){ + if( score != v_high_score[q+1] && ok==0 ){ + ok=1; + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << v_high_score[q] <<"\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else if( v_high_score[q] != 0 ){ + cout << setw(l1 / 4) << " " << v_high_score[q] <<"\n"; + } + } + Sleep(4000); + } + else{ + for( int q=1;q<10;q++ ){ // Put your score in the top scores and write it with red + if( score > v_high_score[q] ){ + for( int w=9;w>=q+1;w-- ){ + v_high_score[w]=v_high_score[w-1]; + } + v_high_score[q]=score; + save_top_score(); + + cout<<"\n\n\n\n\n\n"; + cout << setw(l1 / 4) << " " << "Top score\n\n"; + + int ok=0; + for( int q=0;q<10;q++ ){ + if( score == v_high_score[q] && ok==0 ){ + ok=1; + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << v_high_score[q] <<"\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else if( v_high_score[q] != 0 ){ + cout << setw(l1 / 4) << " " << v_high_score[q] <<"\n"; + } + } + Sleep(4000); + break; + } + else if( score == v_high_score[q] ){ + for( int w=9;w>=q+1;w-- ){ + v_high_score[w]=v_high_score[w-1]; + } + v_high_score[q]=score; + save_top_score(); + + cout<<"\n\n\n\n\n\n"; + cout << setw(l1 / 4) << " " << "Top score\n\n"; + + int ok=0; + for( int q=0;q<10;q++ ){ + if( score == v_high_score[q] && score != v_high_score[q+1] && ok==0 ){ + ok=1; + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << v_high_score[q] <<"\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else if( v_high_score[q] != 0 ){ + cout << setw(l1 / 4) << " " << v_high_score[q] <<"\n"; + } + } + Sleep(4000); + break; + } + } + } + system("cls"); + resume=0; + exit(0); +} + +void display_map() +{ + system("cls"); + for (i = 0; i < 20; i++){ + for (j = 0; j < 40; j++){ + if (map[i][j]=='W'){ // memorises player's position + poz_player.x=i; + poz_player.y=j; + } + printf("%c", map[i][j]); + } + cout<> var ; + + if(var != 0 ){ + cout << setw(l1 / 4) << " " << var <<"\n"; + } + } + Top_Score_file.close(); + + while(getch() ){ + if( GetAsyncKeyState(VK_ESCAPE) ){ //if the key pressed is ESC then go back to menu + break; + } + } + } + + if (k_meniu == 3){ // exit is selected + system("cls"); + exit(0); + } + } + + if (k_meniu == 0){ //if new game is selected then write it with red + system("cls"); + cout << "\n\n\n\n\n"; + + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << "NEW GAME\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else{ + system("cls"); + cout << "\n\n\n\n\n"; + cout << setw(l1 / 4) << " " << "NEW GAME\n"; + } + + if (k_meniu == 1){ //if resume is selected then write it with red + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << "RESUME\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else{ + cout << setw(l1 / 4) << " " << "RESUME\n"; + } + + if (k_meniu == 2){ //if top scores is selected then write it with red + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << "TOP SCORE\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else{ + cout << setw(l1 / 4) << " " << "TOP SCORE\n"; + } + + if (k_meniu == 3){ //if exit is selected then write it with red + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << setw(l1 / 4) << " " << "EXIT\n"; + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + } + else{ + cout << setw(l1 / 4) << " " << "EXIT\n"; + } + } +} + +void new_game() +{ + end_game=false; + init_high_score(); //initializes the highest score and vector top scores + + if( resume==0 ){ // means that new game is selected + + if( level==1 ){ + init_map1(); //Initialize the map for level 1 + direction_enemy='r'; //enemies moving to right the first time + var_random=25; //defines how often shoot enemies + system("cls"); + cout<<"\n\n\n\n\n\n\n\n\n\n"; + cout<<" LEVEL 1"; + GotoXY(500,500); //move the cursor into an area where is not visible + Sleep(3000); + system("cls"); + } + else if( level==2 ){ + init_map2(); //Initialize the map for level 2 + direction_enemy='r'; + var_random=15; + } + else if( level==3 ){ + init_map3(); //Initialize the map for level 3 + direction_enemy='r'; + var_random=7; + } + }else if(resume==1){ // I chose resume + init_map_resume(); + } + + display_map(); + + GotoXY(7,50); //move the cursor to the given coordinates + cout<<"Lives: "<0;i-- ){ //Search the enemy bullet + for( j=0;j<40;j++ ){ + if( map[i][j]=='*' ) + enemy_bullet(); //control the movement of enemy bullet + } + } + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + if ( GetAsyncKeyState(VK_RIGHT) ){ //if the key pressed is right arrow then + move_player_right(); + } + if ( GetAsyncKeyState(VK_LEFT) ){ //if the key pressed is left arrow then + move_player_left(); + } + if ( GetAsyncKeyState(VK_UP) ){ //if the key pressed is up arrow then + move_player_up(); + } + if ( GetAsyncKeyState(VK_DOWN) ){ //if the key pressed is down arrow then + move_player_down(); + } + + if( GetAsyncKeyState(VK_SPACE) ){ //if the key pressed is space then + + switch(k_shoot){ // number of bullets that you shoot + + case 1:{ + player_shoot(); + break; + } + case 2:{ + player_shoot_2(); + break; + } + case 3:{ + player_shoot_3(); + break; + } + } + } + + move_enemy(); // make the movement of enemies + + int ok=0; + for( i=20;i>=0;i-- ){ //identify the last line of enemies because only the last line shoots + for( j=0;j<40;j++ ){ + if( map[i][j]=='Y' ){ + last_line_enemy=i; + ok=1; + break; + } + } + if(ok==1){ + break; + } + } + + enemy_shoot(); + + if( bonus==false && malus==false ){ + display_bonus(); + } + + if( bonus==false && malus==false ){ + display_malus(); + } + + + number_enemy(); //count how many enemies are left + + GotoXY(500,500); //move the cursor into an area where is not visible + + Sleep(speed_game); + + end_war(); + }//end while + + if( end_game==false ){ //saves the map when it's pressed ESC + resume=1; + + resume_save_map(); + display_meniu(); + } +} + +void end_war() //check if the game was over +{ + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + + + COORD l = GetLargestConsoleWindowSize(hOut); + int l1 = l.X; + + + for( int p=0;p<40;p++ ){ //Check if the enemies have reached to the last line + if( map[18][p]=='Y' ){ + Sleep(2000); + end_game=true; + system("cls"); + + cout<<"\n\n\n\n The enemy was too strong \n\n"; + Sleep(3000); + cout<<" The planet was conquered!"; + Sleep(3000); + + system("cls"); + display_top_score(); + } + } + + if( lives==0 && k_enemy==0 ){ //You destroyed your enemies and you died + Sleep(3000); + end_game=true; + system("cls"); + + cout<<"\n\n\n\n You won,but you pay with your life \n\n"; + Sleep(3000); + cout<<" Your sacrifice will not be forgotten!"; + Sleep(3000); + + system("cls"); + display_top_score(); + + }else if( lives==0 ){ //you died + Sleep(3000); + end_game=true; + system("cls"); + + cout<<"\n\n\n\n You Lose! \n\n"; + Sleep(3000); + cout<<" The planet was conquered!"; + Sleep(3000); + + system("cls"); + display_top_score(); + + }else if( k_enemy==0 ){ // you destroy all your enemies + Sleep(3000); + system("cls"); + + if( level==1 ){ //go to the level 2 + level=2; + cout<<"\n\n\n\n\n\n\n\n\n\n"; + cout<<" LEVEL 2"; + GotoXY(500,500); + Sleep(3000); + system("cls"); + resume=0; + new_game(); + + }else if( level==2 ){ //go to the level 3 + level=3; + cout<<"\n\n\n\n\n\n\n\n\n\n"; + cout<<" LEVEL 3"; + GotoXY(500,500); + Sleep(3000); + system("cls"); + resume=0; + new_game(); + } + end_game=true; //game is over and you won + + cout<<"\n\n\n\n You Won! \n\n"; + Sleep(3000); + cout<<" Congratulations!"; + Sleep(3000); + + system("cls"); + display_top_score(); + } +} + +void move_player_right() +{ + switch( map[poz_player.x][poz_player.y+1] ){ + case ' ':{ //move your spaceship to the next right position + delete_coord(poz_player.x,poz_player.y); + poz_player.y++; + GotoXY(poz_player.x,poz_player.y); + cout<<'W'; + map[poz_player.x][poz_player.y]='W'; + break; + } + case 'Y':{ //move your spaceship to the right and hit the enemy + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + delete_coord(poz_player.x,poz_player.y); + poz_player.y++; + GotoXY(poz_player.x,poz_player.y); + cout<<'X'; //X - represents your spaceship explosion + map[poz_player.x][poz_player.y]='X'; + + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + score=score+100; + GotoXY(11,50); + cout<<"Score: "<14 ){ //move your spaceship to the next up position + switch( map[poz_player.x-1][poz_player.y] ){ + case ' ':{ + delete_coord(poz_player.x,poz_player.y); + poz_player.x--; + GotoXY(poz_player.x,poz_player.y); + cout<<'W'; + map[poz_player.x][poz_player.y]='W'; + break; + } + case 'Y':{ //move your spaceship to the next up position and hit the enemy + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + delete_coord(poz_player.x,poz_player.y); + poz_player.x--; + GotoXY(poz_player.x,poz_player.y); + cout<<'X'; + map[poz_player.x][poz_player.y]='X'; + + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + score=score+100; + GotoXY(11,50); + cout<<"Score: "<=0;i-- ){ + for( j=0;j<40;j++ ){ + if(map[i][j]=='Y'){ + switch( map[i+1][j] ){ + + case '^':{ // move the enemy to the next down position and hits your bullet + SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); + delete_coord(i,j); + map[i+1][j]='x'; + GotoXY(i+1,j); + cout<<'x'; + + SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + score=score+100; + GotoXY(11,50); + cout<<"Score: "<