Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <windows.h>
- #include <conio.h>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- #if defined(_MSC_VER) || __cplusplus > 199711L
- #define aut(r,v) auto r = (v)
- #else
- #define aut(r,v) typeof(v) r = (v)
- #endif
- #define forA(V) for (aut(it,(V).begin());it!=(V).end();it++)
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define ll long long
- #define ull unsigned ll
- #define MOD 1000000007
- #define INF (1<<31)-1
- #define vi vector <int>
- #define pii pair <int,int>
- #define pll pair <ll,ll>
- #define newl printf("\n")
- #define scanvi(v,N) for (int i=1;i<=N;i++) scanf("%d",&v[i]);
- #define printvi(v,N) for (int i=1;i<=N;i++) printf("%d ",v[i]);
- #define scanmi(v,N,M) for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) scanf("%d",&v[i][j]);
- #define printmi(v,N,M) for (int i=1;i<=N;i++,newl) for (int j=1;j<=M;j++) printf("%d ",v[i][j]);
- char mat[1005][1005];
- int x,y,h=20,w=40,Score,nrE,Highscore,nrSuperTurns,cooldown;
- struct el
- {
- int x,y;
- }Enemies[1005];
- bool PlayerAlive=true;
- void sp(int choosecolor)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), choosecolor); //FUNCTION OF COLOR
- }
- int abs(int a){if (a<0) return -a; else return a;}
- void CreateRoom()
- {
- for (int i=1;i<=w;i++) mat[1][i]='*';
- for (int i=2;i<h;i++)
- {
- mat[i][1]=mat[i][w]='*';
- for (int j=2;j<w;j++) mat[i][j]='-';
- }
- for (int i=1;i<=w;i++) mat[h][i]='*';
- }
- void Print()
- {
- sp(7);
- for (int i=1;i<=h;i++) puts(mat[i]+1);
- printf("Score: %d\n",Score);
- printf("Nr. of SPACE uses: %d\n",nrSuperTurns);
- if (cooldown>0) printf("Cooldown: %d",cooldown);
- }
- bool Move()
- {
- if (cooldown==0 && nrSuperTurns==0) nrSuperTurns=5;
- int key=getch();
- if (key==27) return false; // ESC / ABORT
- else if (key==32)
- {
- if (cooldown==0 && nrSuperTurns==0) nrSuperTurns=5;
- if (nrSuperTurns>0)
- {
- mat[x][y]='C';
- nrSuperTurns--;
- if (nrSuperTurns==0) cooldown=50;
- }
- else Move();
- return true;
- }
- else if (key==0 || key==0xE0)
- {
- key=getch();
- if (key==75 && y>2) // LEFT
- {
- if (cooldown>0) cooldown--;
- mat[x][y]='-';
- y--;
- if (mat[x][y]=='E')
- {
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==x && Enemies[i].y==y) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- Score++;
- }
- mat[x][y]='M';
- return true;
- }
- else if (key==77 && y<w-1) // RIGHT
- {
- if (cooldown>0) cooldown--;
- mat[x][y]='-';
- y++;
- if (mat[x][y]=='E')
- {
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==x && Enemies[i].y==y) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- Score++;
- }
- mat[x][y]='M';
- return true;
- }
- else if (key==72 && x>2) // UP
- {
- if (cooldown>0) cooldown--;
- mat[x][y]='-';
- x--;
- if (mat[x][y]=='E')
- {
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==x && Enemies[i].y==y) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- Score++;
- }
- mat[x][y]='M';
- return true;
- }
- else if (key==80 && x<h-1) // DOWN
- {
- if (cooldown>0) cooldown--;
- mat[x][y]='-';
- x++;
- if (mat[x][y]=='E')
- {
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==x && Enemies[i].y==y) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- Score++;
- }
- mat[x][y]='M';
- return true;
- }
- else Move();
- }
- else Move();
- }
- void createEnemy()
- {
- int xe=rand()%h+1,ye=rand()%w+1;
- bool ok=true;
- while (ok)
- {
- ok=false;
- if (abs(xe-x)<5 || abs(ye-y)<5) xe=rand()%h+1,ye=rand()%w+1,ok=true;
- if (mat[xe][ye]!='-') xe=rand()%h+1,ye=rand()%w+1,ok=true;
- }
- nrE++;
- Enemies[nrE].x=xe;
- Enemies[nrE].y=ye;
- mat[xe][ye]='E';
- }
- void createEnemies()
- {
- while (nrE<max(1,h*w/100)) createEnemy();
- }
- void moveEnemy(int &xe,int &ye)
- {
- if (mat[xe+1][ye]=='E' && mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E' && mat[xe][ye-1]=='E') return;
- int cmd=-1;
- if (mat[xe+1][ye]=='E' && mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E') cmd=0;
- else if (mat[xe+1][ye]=='E' && mat[xe-1][ye]=='E' && mat[xe][ye-1]=='E') cmd=1;
- else if (mat[xe+1][ye]=='E' && mat[xe][ye+1]=='E' && mat[xe][ye-1]=='E') cmd=2;
- else if (mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E' && mat[xe][ye-1]=='E') cmd=3;
- // cmd==0 LEFT
- // cmd==1 RIGHT
- // cmd==2 UP
- // cmd==3 DOWN
- if (xe<=x && ye<=y && cmd==-1)
- {
- if (mat[xe+1][ye]=='E' && mat[xe][ye+1]=='E')
- while (cmd!=2 && cmd!=0) cmd=rand()%4;
- else if (xe==x) cmd=1;
- else if (ye==y) cmd=3;
- else
- while (cmd!=1 && cmd!=3) cmd=rand()%4;
- }
- else if (xe<=x && ye>=y && cmd==-1)
- {
- if (mat[xe+1][ye]=='E' && mat[xe][ye-1]=='E')
- while (cmd!=2 && cmd!=1) cmd=rand()%4;
- else if (xe==x) cmd=0;
- else if (ye==y) cmd=3;
- else
- while (cmd!=0 && cmd!=3) cmd=rand()%4;
- }
- else if (xe>=x && ye<=y && cmd==-1)
- {
- if (mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E')
- while (cmd!=2 && cmd!=3) cmd=rand()%4;
- else if (xe==x) cmd=1;
- else if (ye==y) cmd=2;
- else
- while (cmd!=1 && cmd!=2) cmd=rand()%4;
- }
- else if (xe>=x && ye>=y && cmd==-1)
- {
- if (mat[xe-1][ye]=='E' && mat[xe][ye-1]=='E')
- while (cmd!=1 && cmd!=3) cmd=rand()%4;
- else if (xe==x) cmd=0;
- else if (ye==y) cmd=2;
- else
- while (cmd!=0 && cmd!=2) cmd=rand()%4;
- }
- if (cmd==0 && (mat[xe][ye-1]=='*' || mat[xe][ye-1]=='E')) return;
- else if (cmd==1 && (mat[xe][ye+1]=='*' || mat[xe][ye+1]=='E')) return;
- else if (cmd==2 && (mat[xe-1][ye]=='*' || mat[xe-1][ye]=='E')) return;
- else if (cmd==3 && (mat[xe+1][ye]=='*' || mat[xe+1][ye]=='E')) return;
- else if (cmd==0)
- {
- mat[xe][ye]='-';
- ye--;
- if (mat[xe][ye]=='C')
- {
- Score++;
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==xe && Enemies[i].y==ye) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- }
- else if (mat[xe][ye]=='M')
- {
- mat[xe][ye]='E';
- PlayerAlive=false;
- return;
- }
- else mat[xe][ye]='E';
- }
- else if (cmd==1)
- {
- mat[xe][ye]='-';
- ye++;
- if (mat[xe][ye]=='C')
- {
- Score++;
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==xe && Enemies[i].y==ye) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- }
- else if (mat[xe][ye]=='M')
- {
- mat[xe][ye]='E';
- PlayerAlive=false;
- return;
- }
- else mat[xe][ye]='E';
- }
- else if (cmd==2)
- {
- mat[xe][ye]='-';
- xe--;
- if (mat[xe][ye]=='C')
- {
- Score++;
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==xe && Enemies[i].y==ye) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- }
- else if (mat[xe][ye]=='M')
- {
- mat[xe][ye]='E';
- PlayerAlive=false;
- return;
- }
- else mat[xe][ye]='E';
- }
- else if (cmd==3)
- {
- mat[xe][ye]='-';
- xe++;
- if (mat[xe][ye]=='C')
- {
- Score++;
- int i;
- for (i=1;i<=nrE;i++)
- if (Enemies[i].x==xe && Enemies[i].y==ye) break;
- for (;i<nrE;i++)
- Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
- nrE--;
- }
- else if (mat[xe][ye]=='M')
- {
- mat[xe][ye]='E';
- PlayerAlive=false;
- return;
- }
- else mat[xe][ye]='E';
- }
- }
- bool game()
- {
- if (nrE==0) createEnemies();
- if (!Move()) return false; // ESC / ABORT
- else
- {
- if (nrE>0)
- for (int i=1;i<=nrE;i++) moveEnemy(Enemies[i].x,Enemies[i].y);
- if (PlayerAlive==false) return false;
- system("cls");
- Print();
- game();
- }
- }
- bool start()
- {
- srand(time(NULL));
- PlayerAlive=true;
- nrSuperTurns=5;
- cooldown=0;
- Score=0;
- x=2,y=2;
- nrE=0;
- CreateRoom();
- mat[x][y]='M';
- Print();
- if (!game()) return false;
- }
- void Menu()
- {
- system("cls");
- printf("1.Play"); newl;
- printf("2.Help"); newl;
- printf("3.Highscore"); newl;
- printf("4.Quit"); newl;
- int cmd=getch();
- while (cmd!='1' && cmd!='2' && cmd!='3' && cmd!='4') cmd=getch();
- if (cmd=='1')
- {
- system("cls");
- while (start());
- system("cls");
- printf("You have been killed or have quit the game.\n");
- printf("Your final score is: %d\n",Score);
- Sleep(2000);
- if (Score>Highscore)
- {
- Highscore=Score;
- printf("Congratulations! New highscore!\n");
- }
- printf("Press ESC to go back to menu.");
- char cmd=getch();
- while (cmd!=27) cmd=getch();
- Menu();
- }
- else if (cmd=='2')
- {
- system("cls");
- Sleep(1000);
- printf("You are Michael (M) and you start in the top-left corner.\n");
- Sleep(3000);
- printf("Your goal is to touch as many Enemies (E) as possible before they touch you.\n");
- Sleep(3000);
- printf("You can move using the arrow keys.\n");
- Sleep(3000);
- printf("Space will transform you into C which makes enemies who touch you die.\n");
- Sleep(3000);
- printf("Space can be used 5 turns with a cooldown of 50 turns after using the 5 turns.\n");
- Sleep(3000);
- printf("Enemies will respawn at random locations when their number reaches zero.\n");
- Sleep(3000);
- printf("Press any button to go back to menu.\n");
- Sleep(1000);
- getch();
- Menu();
- }
- else if (cmd=='3')
- {
- system("cls");
- printf("Your highscore is %d\n",Highscore);
- printf("Press anything to go back to menu.");
- getch();
- Menu();
- }
- else if (cmd=='4') return;
- }
- int main()
- {
- Menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement