Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <conio.h>
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define LUNGIME 92
- #define LATIME 20
- #define SIMBOL_MARGINE1 '-'
- #define SIMBOL_MARGINE2 '|'
- using namespace std;
- struct Monstru{
- int HP, poz, pozy;
- char FORMA[5][7], LASER;
- };
- Monstru M1, M2, M3, M4, R;
- int sh = 1;
- bool GAMEEND = false;
- char matTer[LATIME][LUNGIME];
- short int pozNava = 4;
- int Is_Clear(int m, int n)
- {
- for(int i=m-1;i<=m+5;i++)
- for(int j=n-2;j<=n+2;j++)
- if(matTer[i][j]!='\0' )
- return 0;
- return 1;
- }
- void m1 (){
- M1.pozy = 88;
- do
- M1.poz = rand() % 19;
- while (
- M1.poz < 3 ||
- Is_Clear(M1.poz,M1.pozy)==0
- );
- matTer[M1.poz][M1.pozy] = M1.FORMA[2][1];
- matTer[M1.poz - 1][M1.pozy + 2] =
- matTer[M1.poz][M1.pozy + 2] =
- matTer[M1.poz + 1][M1.pozy + 2] =
- matTer[M1.poz - 1][M1.pozy + 1] =
- matTer[M1.poz][M1.pozy + 1] =
- matTer[M1.poz + 1][M1.pozy + 1] =
- M1.FORMA[2][2];
- }
- void m2(){
- M2.pozy = 88;
- do
- M2.poz = rand() % 18;
- while (
- M2.poz < 2 ||Is_Clear(M2.poz,M2.pozy)==0);
- matTer[M2.poz][M2.pozy] =
- matTer[M2.poz][M2.pozy + 1] =
- matTer[M2.poz][M2.pozy + 2] =
- M2.FORMA[1][1];
- }
- void m3(){
- M3.pozy = 88;
- do
- M3.poz = rand() % 18;
- while (M3.poz < 3 ||
- Is_Clear(M3.poz,M3.pozy)==0);
- matTer[M3.poz][M3.pozy] =
- matTer[M3.poz][M3.pozy + 1] =
- matTer[M3.poz - 1][M3.pozy + 1] =
- matTer[M3.poz - 1][M3.pozy + 2] =
- matTer[M3.poz + 1][M3.pozy + 1] =
- matTer[M3.poz + 1][M3.pozy + 2] =
- M3.FORMA[2][1];
- }
- void m4(){
- M4.pozy = 88;
- do
- M4.poz = rand() % 16;
- while (
- M4.poz < 5 ||
- Is_Clear(M4.poz,M4.pozy)==0
- );
- matTer[M4.poz -1][M4.pozy-1]=M4.FORMA[1][2];
- matTer[M4.poz][M4.pozy-1]=matTer[M4.poz][M4.pozy+2]=M4.FORMA[2][2];
- matTer[M4.poz +1][M4.pozy-1]=M4.FORMA[4][1];
- matTer[M4.poz-1][M4.pozy-2]=matTer[M4.poz +1][M4.pozy-2]=M4.FORMA[1][1];
- matTer[M4.poz -1 ][M4.pozy]=matTer[M4.poz -1][M4.pozy+1]=matTer[M4.poz +1][M4.pozy]=matTer[M4.poz +1][M4.pozy+1]=M4.FORMA[1][3];
- matTer[M4.poz -1][M4.pozy+2]=M4.FORMA[1][6];
- matTer[M4.poz+1][M4.pozy+2]=M4.FORMA[4][6];
- matTer[M4.poz][M4.pozy]=matTer[M4.poz][M4.pozy+1]=M4.FORMA[3][3];
- }
- void r(){
- R.pozy = 86;
- do
- R.poz = rand () % 18;
- while (
- R.poz<2 ||
- matTer[R.poz][R.pozy] != '\0' ||
- matTer[R.poz - 1][R.pozy + 1] != '\0' ||
- matTer[R.poz - 1][R.pozy + 2] != '\0' ||
- matTer[R.poz - 1][R.pozy + 3] != '\0' ||
- matTer[R.poz][R.pozy + 1] != '\0' ||
- matTer[R.poz][R.pozy + 2] != '\0' ||
- matTer[R.poz][R.pozy + 3] != '\0'
- );
- matTer[R.poz][R.pozy + 4] = R.FORMA[2][4];
- matTer[R.poz - 1][R.pozy + 1] = matTer[R.poz - 1][R.pozy + 2] = matTer[R.poz - 1][R.pozy + 3] = R.FORMA[1][2];
- matTer[R.poz][R.pozy + 1] = matTer[R.poz][R.pozy + 2] = matTer[R.poz][R.pozy + 3] = R.FORMA[1][2];
- matTer[R.poz][R.pozy] = R.FORMA[2][1];
- }
- void show_cursor(bool show){ //vizibilitate curosor in consola
- HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO infCursor;
- GetConsoleCursorInfo(out, &infCursor);
- infCursor.bVisible = show;
- SetConsoleCursorInfo(out, &infCursor);
- }
- int check (char x){
- int i,j;
- for(i = 1; i <= 3; i++)
- for(j = 1; j <= 3; j++)
- if (x == M1.FORMA[i][j])
- return 1;
- if (x == M2.FORMA[1][1])
- return 2;
- for(i = 1; i <= 3; i++)
- for(j = 1;j <= 3; j++)
- if (x == M3.FORMA[i][j])
- return 3;
- for(i = 1; i <= 5; i++)
- for(j = 1; j <= 4; j++)
- if (x == M4.FORMA[i][j])
- return 4;
- for(i = 1; i <= 2; i++)
- for(j = 1; j <= 4; j++)
- if(x == R.FORMA[i][j])
- return 5;
- return 0;
- }
- void DEL_M1(){
- for(int i = M1.poz - 1; i <= M1.poz + 1; i++)
- for(int j = M1.pozy; j <= M1.pozy + 3; j++)
- matTer[i][j]='\0';
- }
- void DEL_M2(){
- for(int j = M2.pozy; j <= M2.pozy + 2; j++)
- matTer[M2.poz][j]='\0';
- }
- void DEL_M3(){
- for(int i = M3.poz - 1; i <= M3.poz + 1; i++)
- for(int j = M3.pozy; j <= M3.pozy + 2; j++)
- if(matTer[i][j] == 'O')
- matTer[i][j] = '\0';
- }
- void DEL_M4(){
- for(int i = M4.poz - 1; i <= M4.poz + 2; i++)
- for(int j = M4.pozy - 2; j <= M4.pozy + 3; j++)
- if(check(matTer[i][j]) == 4)
- matTer[i][j]='\0';
- }
- void afisare(){
- for (int i = 1; i <= LATIME - 1; i++){
- for (int j = 1; j <= LUNGIME - 1; j++)
- cout << matTer[i][j];
- cout << endl;
- }
- }
- void creare_teren(){
- int i;
- for (i = 1; i <= LATIME - 1; i++)
- matTer[i][1] = matTer[i][LUNGIME - 1] = SIMBOL_MARGINE2;
- for (i = 1; i <= LUNGIME - 1; i++)
- matTer[1][i] = matTer[LATIME - 1][i] = SIMBOL_MARGINE1;
- }
- void nava(){
- matTer[2][2] = matTer[3][2] = matTer[4][2] = matTer[3][3] = (char)219;
- }
- void wave1()
- {
- int i;
- int Wave_Size=20;
- Monstru Monster[50];
- int Monster_Type[50];
- for(i=1;i<=Wave_Size;i++)
- switch (rand()%4)
- {
- case 0: {Monster[i]=M1;
- Monster_Type[i]=1;}
- case 1: {Monster[i]=M2;
- Monster_Type[i]=2;}
- case 2: {Monster[i]=M3;
- Monster_Type[i]=3;}
- case 3: {Monster[i]=M4;
- Monster_Type[i]=4;}
- }
- }
- void miscare(){
- char newKey;
- int i,j;
- if (_kbhit())
- newKey = getch();
- if(newKey == 's' ||newKey == 'S')
- if(pozNava < 18){
- matTer[pozNava + 1][2] = matTer[pozNava][2];
- matTer[pozNava ][3] = matTer[pozNava-1][3];
- matTer[pozNava - 2][2] = matTer[pozNava-1][3] = '\0';
- pozNava++;
- }
- if(newKey == 'w' || newKey == 'W')
- if(pozNava > 4){
- matTer[pozNava - 3][2] = matTer[pozNava][2];
- matTer[pozNava - 2][3] = matTer[pozNava - 1][3];
- matTer[pozNava][2] = matTer[pozNava - 1][3] = '\0';
- pozNava--;
- }
- if(newKey == (char)32){
- if (matTer[pozNava - 1][4] != '\0')
- matTer[pozNava - 1][4] = '\0';
- matTer[pozNava - 1][4] = char(248);
- }
- //sh, "shoot" in prima faza, era o variabila cu care voiam sa controlez rata la care trag navele lor
- if(sh % 25 == 1 && M1.HP != 0)
- matTer[M1.poz][M1.pozy - 1] = M1.LASER;
- if(sh % 25 == 7 && M2.HP != 0)
- matTer[M2.poz][M2.pozy - 1] = M2.LASER;
- if(sh % 25 == 13 && M3.HP != 0)
- matTer[M3.poz][M3.pozy - 1] = M3.LASER;
- if(sh % 25 == 17 && M4.HP != 0)
- matTer[M4.poz - 1][M4.pozy - 3] = matTer[M4.poz + 1][M4.pozy - 3] = M4.LASER;
- sh++;
- for(i = 2; i <= LATIME - 2; i++)
- for(j = 2 ; j <= LUNGIME - 2; j++)
- {if(matTer[i][j] == (char)248)
- {
- if(
- matTer[i][j + 2] == M1.LASER ||
- matTer[i][j + 2] == M2.LASER ||
- matTer[i][j + 2] == M3.LASER ||
- matTer[i][j + 2] == M4.LASER ||
- matTer[i][j + 1] == M1.LASER ||
- matTer[i][j + 1] == M2.LASER ||
- matTer[i][j + 1] == M3.LASER ||
- matTer[i][j + 1] == M4.LASER
- )
- matTer[i][j + 2] = matTer[i][j + 1] = '\0';
- else
- if(matTer[i][j + 2] == '\0'){
- if(j < LUNGIME - 3){
- matTer[i][j + 2] = (char)248;
- matTer[i][j] = '\0';
- j = j + 2;
- }
- else
- matTer[i][j]='\0';
- }
- else{
- switch (check(matTer[i][j + 2]))
- { //de aici si pana la urmatorul, verific ce "monstru" a lovit
- case 1:{ // printr-un switch care verifica acel caracter care este in 2 pozitii
- M1.HP -= 1; //unde ar trebui sa se mute glontele
- if(M1.HP == 0)
- DEL_M1();
- break;
- }
- case 2:{
- M2.HP = 0; //daca distrugi nava, nici sa nu mai traga
- DEL_M2();
- break;
- }
- case 3:{
- M3.HP--;
- if(M3.HP == 0)
- DEL_M3();
- break;
- }
- case 4:{
- if(matTer[M4.poz][M4.pozy]=='\0')
- {
- Sleep(10000);}
- M4.HP--;
- if(M4.HP==0){
- DEL_M4();}
- break;
- }
- default: break;
- }
- matTer[i][j]='\0';
- } //daca s-a intrat pe acel else, se loveste de ceva, fac ce fac, si dupa sterg glontele
- }
- else
- if(matTer[i][j] == M1.LASER || matTer[i][j] == M2.LASER || matTer[i][j] == M3.LASER || matTer[i][j] == M4.LASER){
- if (matTer[i][j - 2] == char(219) || matTer[i][j - 1] == char(219))
- GAMEEND=TRUE; //daca esti lovit, se termina jocul
- else
- if(j==3){
- matTer[i][j] = '\0'; //daca glontele lui a ajuns la coloana 3, dispare
- }
- else
- if(j > 3){
- matTer[i][j-1]=matTer[i][j];
- matTer[i][j]='\0';
- } //il tot mut cu o pozitie la fiecare parcurgere
- }
- else{
- if(sh % 3 == 0){ //"sh" a devenit si rata la care se misca navele, ca nu vreau sa o faca mereu
- if(matTer[i][j] != '\0' && matTer[i][j] != char(219))
- if(check(matTer[i][j])==1){ //toate if/else verifica muta componenta unei nave
- if(M1.pozy == 3){ //si in caz ca ajunge la final, o face sa dispara
- if(matTer[i][j - 1] == char(219))
- GAMEEND=TRUE;
- else{
- DEL_M1();
- M1.HP=0;
- }
- }
- matTer[i][j - 1] = matTer[i][j];
- if(i == M1.poz && j == M1.pozy)
- M1.pozy--;
- }
- else
- if(check(matTer[i][j]) == 2){
- if(M2.pozy == 3){
- if(matTer[i][j - 1] == char(219))
- GAMEEND = TRUE;
- else{
- DEL_M2();
- M2.HP = 0;
- }
- }
- matTer[i][j - 1] = matTer[i][j];
- if(i == M2.poz && j == M2.pozy)
- M2.pozy--;
- }
- else
- if(check(matTer[i][j]) == 3){
- if(M3.pozy == 3){
- if(matTer[i][j - 1] == char(219))
- GAMEEND = TRUE;
- else{
- DEL_M3();
- M3.HP = 0;
- }
- }
- matTer[i][j - 1] = matTer[i][j];
- if(i == M3.poz && j == M3.pozy)
- M3.pozy--;
- }
- else
- if(check(matTer[i][j]) == 4){
- if(M4.pozy == 4){
- if(matTer[i][j - 1] == char(219))
- GAMEEND = TRUE;
- else{
- DEL_M4();
- M4.HP = 0;
- }
- }
- matTer[i][j - 1] = matTer[i][j];
- if(i == M4.poz && j == M4.pozy)
- M4.pozy--;
- }
- if(matTer[i][j] != char(219))
- matTer[i][j] = '\0';
- }
- }
- }}
- int main (){
- char x;
- cout << "_______________________________________________________________________" << endl;
- cout << "| |" << endl;
- cout << "| * * *** * *** ** * * *** |" << endl;
- cout << "| * * * ** * * * * * * * ** |" << endl;
- cout << "| * * *** *** *** ** * * *** |" << endl;
- cout << "| |" << endl;
- cout << "| |" << endl;
- cout << "| PRESS 1 TO START GAME |" << endl;
- cout << "| PRESS 2 TO EXIT |" << endl;
- cout << "_______________________________________________________________________" << endl;
- cin >> x;
- system("cls");
- if(x == '1'){
- creare_teren();
- show_cursor(false);
- M1.HP = 3;
- M2.HP = 1;
- M3.HP = 4;
- M4.HP = 6;
- M1.FORMA[2][1] = (char)174;
- M1.FORMA[1][2] = M1.FORMA[2][2] = M1.FORMA[3][2] = (char)254;
- M2.FORMA[1][1] = M2.FORMA[1][2] = M2.FORMA[1][3] = char(178);
- M3.FORMA[2][1] = M3.FORMA[2][2] = M3.FORMA[1][2] = M3.FORMA[1][3] = M3.FORMA[3][2] = M3.FORMA[3][3] = 'O';
- R.FORMA[2][1] = '<';
- R.FORMA[1][2] = R.FORMA[1][3] = R.FORMA[2][2] = R.FORMA[2][3] = '_';
- R.FORMA[2][4] = '|';
- M4.FORMA[1][1] = M4.FORMA[4][1] = '-';
- M4.FORMA[1][2] = char(201);
- M4.FORMA[1][6] = char(187);
- M4.FORMA[2][2] = M4.FORMA[3][2] = char(186);
- M4.FORMA[1][3] = M4.FORMA[1][4] = M4.FORMA[1][5] = M4.FORMA[4][3] = M4.FORMA[4][4] = M4.FORMA[4][5] = char(205);
- M4.FORMA[2][6] = M4.FORMA[3][6] = char(186);
- M4.FORMA[4][1] = char(200);
- M4.FORMA[4][6] = char(188);
- M4.FORMA[3][2] = M4.FORMA[3][3] = ' ';
- srand(time(NULL));
- m1();
- m2();
- m3();
- m4();
- r();
- nava();
- M1.LASER = char (223);
- M2.LASER = '=';
- M3.LASER = char(249);
- M4.LASER = '~';
- afisare();
- wave1();
- system("cls");
- while(GAMEEND==FALSE){
- miscare();
- Sleep(50);
- system("cls");
- afisare();
- if(M1.HP == 0 && M2.HP == 0 && M3.HP == 0 && M4.HP == 0){
- GAMEEND=TRUE;
- cout << "CONGRATS";
- }
- }
- }
- else
- if(x == '2')
- cout << "GAME OVER";
- exit(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement