【原创】【程序小游戏】Wall-war 墙战

来源:互联网 发布:做淘宝售前客服辛苦吗 编辑:程序博客网 时间:2024/06/01 09:40

事先声明:如有雷同纯属意外。


花了一个多小时,写了这个游戏。大概内容就是:

X在左上角,O在右下角。它们要把对手推到墙里卡死!

X是玩家1,用WASD移动,按XCZ释放技能。O是玩家2,用8456(建议用小键盘)移动,按012释放技能。

每个人有四个技能,每局可以选三个。

技能X(0),在自己前面放一个墙,可以把对手击退。CD 0.75秒。

这个是必须选的。

技能一,清除自己周围一圈的墙,可以把对手扔出去。CD 2秒。

技能二,往前冲5格,遇到墙或者对手停下来。CD 5秒。

技能三,清除前两格的墙,遇到对手把对手击退五格。CD 4秒。


直接上代码:


#include<cstdio>      #include<cstring>      #include<ctime>      #include<conio.h>     #include<cstdlib>      #include<windows.h>        char side_updown[50];      char side_leftright[50];      char map[50][50],c;     int Fight=0;double cd[50][3];      double allcd[50]={0,0.75,2.5,5,4};      int size,wall;      int Skill[3][3];struct Player      {      int x,y;      int face;      char o;      }p[3];      void please()      {      puts("Press any key to start.");char c=getch();      Fight=(c=='9');puts("\nWall-war 's Rule:");      puts("If you want to win , then put your enemy into the wall.");     puts("");Sleep(500);  puts("Player 1: use WASD to move , X C Z to use skills.");      puts("Player 2: use 8456 to move , 0 1 2 to use skills.");     puts("");Sleep(500);  puts("Skill X(0) means putting a wall in front of you , if your enemy is there , he shall be repeled. Its interval is 0.75s.");      puts("Skill 1 means clearing all the walls around you , but not the boundary. You can repel your enemy , too. Its interval is 2.5s.");      puts("Skill 2 means rushing for 5 unit lengths , until you can't move on . Its interval is 5s.");      puts("Skill 3 means clearing the two walls in front of you, whatever what kind of the wall is, but not the boundary, if it's your enemy, you can repel him, too. Its interval is 4s.");puts("");Sleep(1000);puts("");int a;puts("Player One : Please choose your skills: ");Sleep(500);puts("\nSkill one, use C to use :"); scanf("%d",&a); Skill[1][1]=a;puts("\nSkill two, use Z to use :"); scanf("%d",&a);  Skill[1][2]=a;puts("\nPlayer Two : Please choose your skills: ");Sleep(500);puts("\nSkill one, use 1 to use :"); scanf("%d",&a); Skill[2][1]=a;puts("\nSkill two, use 2 to use :"); scanf("%d",&a);  Skill[2][2]=a;}void init_wall()      {      wall=rand()%14+2;     if(Fight) wall=20;for(int i=0;i<=wall;i++) side_updown[i]='#';      for(int i=0;i<=wall;i++)       {      if(i==0 or i==wall) side_leftright[i]='#';      else side_leftright[i]=' ';      }      }      void Set(int t,int x,int y)      {      map[p[t].x][p[t].y]=' ';      p[t].x=x;      p[t].y=y;      map[x][y]=p[t].o;      }      void init_map()      {      for(int i=1;i<=size;i++)      {      if(i==1 or i==size) strcpy(map[i],side_updown);      else strcpy(map[i],side_leftright);      }      p[1].x=p[1].y=p[2].x=p[2].y=0;      p[1].o='X'; p[2].o='O';      Set(1,2,1);      Set(2,size-1,wall-1);      }      int choose()      {      if(map[p[1].x][p[1].y]=='#' or map[p[1].x][p[1].y]=='*') return 1;      if(map[p[2].x][p[2].y]=='#' or map[p[2].x][p[2].y]=='*') return 2;      return 0;      }      void GameEnd(int t)      {      puts("GAME OVER");      Sleep(500);    printf("Player %d is out of the wall. You lose!!!!!\n",t);      printf("Player %d wins !!!!!!!!!!!\n",t==1 ? 2 : 1);      Sleep(1500);    }        void put_map()      {      system("cls");      int k=choose();      for(int i=1;i<=size;i++)      puts(map[i]);      puts("");printf("Player One: (%d,%d)\n",p[1].x,p[1].y);printf("Player Two: (%d,%d)\n",p[2].x,p[2].y);if(k==1) GameEnd(1);      else if(k==2) GameEnd(2);      }      bool check(int x,int y)      {      return map[x][y]!='#' and map[x][y]!='X' and map[x][y]!='O' and map[x][y]!='*';      }      void set_wall(){int k=rand()%(wall*size*3/7);if(Fight) k=0;while(k)while(1){int x=rand()%(size-1)+1;int y=rand()%(wall-2)+1;if(check(x,y)){map[x][y]='#',k--;break;}}}int dx[10]={0,-1,0,1,0,1,1,-1,-1};      int dy[10]={0,0,-1,0,1,1,-1,1,-1};      void move(int t,int go)      {      int px=p[t].x+dx[go],py=p[t].y+dy[go];      p[t].face=go;      if(check(px,py)) Set(t,px,py);      }      bool D(int t,int n)      {      if(cd[t][n]==0)       {      cd[t][n]=clock();      return true;      }      if((clock()-cd[t][n])/1000>=allcd[n])       {      cd[t][n]=0;      return true;      }      return false;      }        void A(int t)      {      int go=p[t].face;      int px=p[t].x+dx[go],py=p[t].y+dy[go];      if(p[t].face and map[px][py]!='#' and map[px][py]!='*')       {      if(map[px][py]=='X') move(1,p[t].face);      if(map[px][py]=='O') move(2,p[t].face);      map[px][py]='*';      put_map();      }      }        void B(int t)      {      for(int i=1;i<=8;i++)      {      int px=p[t].x+dx[i],py=p[t].y+dy[i];      if(map[px][py]=='X') move(1,(p[1].face+2)%4+1);      if(map[px][py]=='O') move(2,(p[2].face+2)%4+1);      if(map[px][py]=='*' and px!=1 and px!=size and py!=0 and py!=wall)       map[px][py]=' ';      put_map();      }      }      void C(int t)      {      int go=p[t].face;      for(int i=1;i<=5;i++)      {      move(t,go);      put_map();      }      }      void E(int t){int go=p[t].face;int px=p[t].x+dx[go],py=p[t].y+dy[go];if(px!=1 and px!=size and py!=0 and py!=wall){int f=0;if(map[px][py]=='X')p[1].face=go,move(1,go),f=1;if(map[px][py]=='O')p[2].face=go,move(2,go),f=2;if(map[px][py]!='X' and map[px][py]!='O') map[px][py]='@';if(f) C(f);put_map();put_map();put_map();put_map();put_map();if(map[px][py]=='@') map[px][py]=' ';put_map();}int tx=px+dx[go],ty=py+dy[go];if(tx!=1 and tx!=size and ty!=0 and ty!=wall){int f=0;if(map[tx][ty]=='X')p[1].face=go,move(1,go),f=1;if(map[tx][ty]=='O')p[2].face=go,move(2,go),f=2;if(map[tx][ty]!='X' and map[tx][ty]!='O') map[tx][ty]='@';if(f) C(f);put_map();put_map();put_map();put_map();put_map();if(map[tx][ty]=='@') map[tx][ty]=' ';put_map();}}void S(int t,int a){int k=Skill[t][a];if(D(t,k)){if(k==1) B(t);if(k==2) C(t);if(k==3) E(t);}}int main()      {      srand((unsigned)time(NULL));      please();  init_wall();size=(rand()%10+3)+2;   if(Fight) size=3;memset(map,0,sizeof map);      init_map();     set_wall();put_map();      while(c=getch())      {      if(choose()) return 0;      if(c=='W' or c=='w') move(1,1);      if(c=='A' or c=='a') move(1,2);      if(c=='S' or c=='s') move(1,3);      if(c=='D' or c=='d') move(1,4);      if(c=='X' or c=='x') if(D(1,1)) A(1);      if(c=='C' or c=='c') S(1,1);      if(c=='Z' or c=='z') S(1,2);    if(c=='8') move(2,1);      if(c=='4') move(2,2);      if(c=='5') move(2,3);      if(c=='6') move(2,4);      if(c=='0') if(D(2,1)) A(2);      if(c=='1') S(2,1);     if(c=='2') S(2,2);   put_map();      }      }     


0 0