《Try to Live》
控制台的绝地求生
#include<iostream> #include<cmath> #include<windows.h> #include<cstdlib> #include<time.h> using namespace std; char ground[16][31]; char move_player,fight_player; char player[5]; char a='A'; char house[5]; int i,j,k; int s_round; int zd_player[5]; int number_player,playing; int x_player[5],y_player[5]; int x_house[5],y_house[5]; int life_player[5]; int x1_round,y1_round,x2_round,y2_round; bool live_player[5],die_player[5]; bool AK47_player[5],scar_player[5]; bool yao_player[5]; bool AK47_house[5],scar_house[5],yao_house[5]; bool zd_house[5]; void color(int a) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a); } void first() { srand(time(0)); for(i=1;i<=4;i++) { life_player[i]=0; zd_player[i]=0; live_player[i]=false; die_player[i]=false; player[i]=a+i-1; x_player[i]=rand()%13+2; y_player[i]=rand()%28+2; house[i]='$'; AK47_player[i]=false; scar_player[i]=false; yao_player[i]=false; zd_house[i]=true; if(i%2==0) { AK47_house[i]=true; scar_house[i]=false; yao_house[i]=false; } else { scar_house[i]=true; AK47_house[i]=false; yao_house[i]=true; } } x_house[1]=5;y_house[1]=21; x_house[2]=7;y_house[2]=11; x_house[3]=9;y_house[3]=20; x_house[4]=11;y_house[4]=14; x1_round=2;y1_round=2; x2_round=14;y2_round=29; s_round=0; } void map() { system("cls"); bool ok; for(i=1;i<=number_player;i++) cout<<player[i]<<": "<<life_player[i]<<endl; for(i=1;i<=15;i++) { for(j=1;j<=30;j++) { ok=false; if(i>1&&i<15&&j>1&&j<30) { for(k=1;k<=number_player;k++) if(i==x_player[k]&&j==y_player[k]) { color(10); cout<<player[k]; color(15); ok=true; break; } for(k=1;k<=5;k++) if(i==x_house[k]&&j==y_house[k]) { color(14); cout<<house[k]; color(15); ok=true; break; } if(ok==false) { if((i==x1_round||i==x2_round)&&j>=y1_round&&j<=y2_round) ground[i][j]='-'; else if((j==y1_round||j==y2_round)&&i>x1_round&&i<x2_round) ground[i][j]='|'; else ground[i][j]=' '; cout<<ground[i][j]; ok==true; } } else { ground[i][j]='@'; color(11); cout<<ground[i][j]; color(15); ok=true; } } cout<<endl; } } void move() { bool ok=false; cin>>move_player; if(move_player=='w') { for(i=1;i<=4;i++) { if(x_player[k]-1==x_house[i]&&y_player[k]==y_house[i]) { ok=true; break; } else if(x_player[k]-1==x_player[i]&&y_player[k]==y_player[i]&&live_player[i]==true) { ok=true; break; } } if(ground[x_player[k]-1][y_player[k]]!='#'&&ok==false) x_player[k]--; } else if(move_player=='s') { for(i=1;i<=4;i++) { if(x_player[k]+1==x_house[i]&&y_player[k]==y_house[i]) { ok=true; break; } else if(x_player[k]+1==x_player[i]&&y_player[k]==y_player[i]&&live_player[i]==true) { ok=true; break; } } if(ground[x_player[k]+1][y_player[k]]!='#'&&ok==false) x_player[k]++; } else if(move_player=='a') { for(i=1;i<=4;i++) { if(x_player[k]==x_house[i]&&y_player[k]-1==y_house[i]) { ok=true; break; } else if(x_player[k]==x_player[i]&&y_player[k]-1==y_player[i]&&live_player[i]==true) { ok=true; break; } } if(ground[x_player[k]][y_player[k]-1]!='#'&&ok==false) y_player[k]--; } else if(move_player=='d') { for(i=1;i<=4;i++) { if(x_player[k]==x_house[i]&&y_player[k]+1==y_house[i]) { ok=true; break; } else if(x_player[k]==x_player[i]&&y_player[k]+1==y_player[i]&&live_player[i]==true) { ok=true; break; } } if(ground[x_player[k]][y_player[k]+1]!='#'&&ok==false) y_player[k]++; } else { cout<<"请重新输入:"; move(); } } void fight() { bool ok=false; cin>>fight_player; if(fight_player=='w') { for(i=x_player[k]-1;i>1;i--) { for(j=1;j<=number_player;j++) if(x_player[j]==i&&y_player[j]==y_player[k]&&life_player[j]>0) { if(AK47_player[k]==true) life_player[j]-=10; else if(scar_player[k]==true) life_player[j]-=8; if(life_player[j]<0) { life_player[j]=0; playing--; } zd_player[k]-=3; ok=true; break; } for(j=1;j<=5;j++) if(x_house[j]==i&&y_house[j]==y_player[k]) ok=true; if(ok==true) break; } } else if(fight_player=='s') { for(i=x_player[k]+1;i<15;i++) { for(j=1;j<=number_player;j++) if(x_player[j]==i&&y_player[j]==y_player[k]&&life_player[j]>0) { if(AK47_player[k]==true) life_player[j]-=10; else if(scar_player[k]==true) life_player[j]-=8; if(life_player[j]<0) { life_player[j]=0; playing--; } zd_player[k]-=3; ok=true; break; } for(j=1;j<=5;j++) if(x_house[j]==i&&y_house[j]==y_player[k]) ok=true; if(ok==true) break; } } else if(fight_player=='a') { for(i=y_player[k]-1;i>1;i--) { for(j=1;j<=number_player;j++) if(y_player[j]==i&&x_player[j]==x_player[k]&&life_player[j]>0) { if(AK47_player[k]==true) life_player[j]-=10; else if(scar_player[k]==true) life_player[j]-=8; if(life_player[j]<0) { life_player[j]=0; playing--; } zd_player[k]-=3; ok=true; break; } for(j=1;j<=5;j++) if(y_house[j]==i&&x_house[j]==x_player[k]) ok=true; if(ok==true) break; } } else if(fight_player=='d') { for(i=y_player[k]+1;i<30;i++) { for(j=1;j<=number_player;j++) if(y_player[j]==i&&x_player[j]==x_player[k]&&life_player[j]>0) { if(AK47_player[k]==true) life_player[j]-=10; else if(scar_player[k]==true) life_player[j]-=8; if(life_player[j]<0) { life_player[j]=0; playing--; } zd_player[k]-=3; ok=true; break; } for(j=1;j<=5;j++) if(y_house[j]==i&&x_house[j]==x_player[k]) ok=true; if(ok==true) break; } } else { cout<<"请重新输入:"; fight(); } } void get_things() { char add,choose; bool ok=false; int h,p; for(i=1;i<=4;i++) { if(abs(x_house[i]-x_player[k])<=1&&abs(y_house[i]-y_player[k])<=1) { cout<<"house"<<i<<':'; if(AK47_house[i]==true) cout<<" a.AK47"; if(scar_house[i]==true) cout<<" b.scar"; if(zd_house[i]==true) cout<<" c.子弹*30"; if(yao_house[i]==true) cout<<" d.绷带"; h=i; ok=true; cout<<endl; break; } } for(i=1;i<=4;i++) { if(abs(x_player[i]-x_player[k])<=1&&abs(y_player[i]-y_player[k])<=1&&die_player[i]==true) { cout<<player[i]<<':'; if(AK47_player[i]==true) cout<<" a.AK47"; if(scar_player[i]==true) cout<<" b.scar"; if(zd_player[i]>0) cout<<" c.子弹*"<<zd_player[i]; if(yao_player[i]==true) cout<<" d.绷带"<<endl; p=i; cout<<endl; ok=true; break; } } if(ok==false) cout<<"周围没有物品!"<<endl; else { cout<<"请输入地址(h或p):"; cin>>add; if(add=='h') { cout<<"请选择物品:"; cin>>choose; if(choose=='a'&&AK47_player[k]==false&&scar_player[k]==false&&AK47_house[h]==true) { AK47_house[h]=false; AK47_player[k]=true; } else if(choose=='b'&&scar_player[k]==false&&AK47_player[k]==false&&scar_house[h]==true) { scar_house[h]=false; scar_player[k]=true; } else if(choose=='c'&&zd_house[h]==true) { zd_house[h]=false; zd_player[k]+=30; } else if(choose=='d'&&yao_player[k]==false&&yao_house[h]==true) { yao_house[h]=false; yao_player[k]=true; } else cout<<"错误!"<<endl; } if(add=='p') { cout<<"请选择物品:"; cin>>choose; if(choose=='a'&&AK47_player[k]==false&&scar_player[k]==false&&AK47_player[p]==true) { AK47_player[p]=false; AK47_player[k]=true; } else if(choose=='b'&&scar_player[k]==false&&AK47_player[k]==false&&scar_player[p]==true) { scar_house[h]=false; scar_player[k]=true; } else if(choose=='c'&&zd_player[p]>0) { zd_player[k]+=zd_player[p]; zd_player[p]=0; } else if(choose=='d'&&yao_player[k]==false&&yao_player[p]==true) { yao_player[p]=false; yao_player[k]=true; } else cout<<"错误!"<<endl; } } } void leave_things() { int h; char choose; bool ok=false; for(i=1;i<=4;i++) if(abs(x_house[i]-x_player[k])<=1&&abs(y_house[i]-y_player[k])<=1) { h=i; ok=true; break; } if(ok==false) cout<<"无法丢弃物品!"<<endl; else { cout<<player[k]<<":"; if(AK47_player[k]==true) cout<<" a.AK47"; if(scar_player[k]==true) cout<<" b.scar"; if(yao_player[k]==true) cout<<" c.绷带"; cout<<endl; cout<<"请选择物品:"; cin>>choose; if(choose=='a'&&AK47_player[k]==true&&AK47_house[h]==false) { AK47_house[h]=true; AK47_player[k]=false; } else if(choose=='b'&&scar_player[k]==true&&scar_house[h]==false) { scar_house[h]=true; scar_player[k]=false; } else if(choose=='c'&&yao_player[k]==true&&yao_house[h]==false) { yao_house[h]=true; yao_player[k]=false; } else cout<<"错误!"<<endl; } } void dayao() { if(life_player[k]<75) { life_player[k]+=20; yao_player[k]=false; } else cout<<"生命值不小于75%不能打药。"<<endl; } void round() { if(s_round==5&&x1_round<7&&x2_round>9&&y1_round<14&&y2_round>17) { x1_round++; x2_round--; y1_round+=2; y2_round-=2; s_round=0; } for(i=1;i<=number_player;i++) { if(life_player[i]>0&&(x_player[i]<x1_round||x_player[i]>x2_round||y_player[i]<y1_round||y_player[i]>y2_round)) life_player[i]-=10; if(life_player[i]<0) life_player[i]=0; } } void die() { playing=number_player; for(i=1;i<=number_player;i++) if(life_player[i]==0) { die_player[i]=true; player[i]='#'; playing--; } } void play() { char choose; for(k=1;k<=number_player;k++) { if(die_player[k]==false) { color(10); cout<<player[k]; color(13); cout<<": a.移动"; if((AK47_player[k]==true||scar_player[k]==true)&&zd_player[k]>0) cout<<" b.攻击"; cout<<" c.捡东西"; cout<<" d.扔东西"; if(yao_player[k]==true) cout<<" e.打药"<<endl; else cout<<endl; color(15); cout<<"请选择:"; cin>>choose; if(choose=='a') { cout<<"请输入移动方向:"; move(); } else if(choose=='b'&&(AK47_player[k]==true||scar_player[k]==true)&&zd_player[k]>0) { cout<<"请输入攻击方向:"; fight(); } else if(choose=='c') get_things(); else if(choose=='d') leave_things(); else if(choose=='e'&&yao_player[k]==true) dayao(); else continue; } } s_round++; } int main() { first(); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; cout<<"$ ####### # # $"<<endl; cout<<"$ # # # $"<<endl; cout<<"$ # ### # # ### ### # # # # ### $"<<endl; cout<<"$ # # # # # # # # # # # ### $"<<endl; cout<<"$ # # ### ## ### ### # # # $"<<endl; cout<<"$ # ### $"<<endl; cout<<"$ ### $"<<endl; cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; cout<<" Made by Peter Winchester"<<endl; cout<<" Please use w, s, a, d to move or fight. To save"<<endl; cout<<"your life, you have to kill other players!"<<endl; cout<<" The $ is a house. Some things are in the houses!"<<endl; cout<<"You must kill other players with the things in the"<<endl; cout<<"houses."<<endl; cout<<" Can you save your life in the end?"<<endl; cout<<" Please input the number of players:"<<endl; cin>>number_player; if(number_player<1) number_player=1; else if(number_player>4) number_player=4; playing=number_player; if(number_player>1) { for(i=1;i<=number_player;i++) { live_player[i]=true; life_player[i]=100; } while(playing>1) { map(); play(); round(); die(); } map(); for(i=1;i<=number_player;i++) if(die_player[i]==false) cout<<player[i]<<" wins!"<<endl; } cout<<"Game over!"; return 0; }