溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用C語(yǔ)言實(shí)現(xiàn)騎士飛行棋小游戲

發(fā)布時(shí)間:2021-08-11 18:44:23 來源:億速云 閱讀:164 作者:chen 欄目:編程語(yǔ)言

這篇文章主要介紹“用C語(yǔ)言實(shí)現(xiàn)騎士飛行棋小游戲”,在日常操作中,相信很多人在用C語(yǔ)言實(shí)現(xiàn)騎士飛行棋小游戲問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”用C語(yǔ)言實(shí)現(xiàn)騎士飛行棋小游戲”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

需求分析

游戲規(guī)則和傳統(tǒng)的飛行棋一樣,支持兩人對(duì)戰(zhàn)

采用100格小型游戲棋盤

游戲規(guī)則:對(duì)戰(zhàn)雙方輪流擲骰子控制自己的騎兵前進(jìn)或后退,在游戲棋盤上設(shè)置有關(guān)卡

普通

地雷

暫停

時(shí)空隧道

幸運(yùn)輪盤(提供兩種運(yùn)氣:交換位置和轟炸)

棋盤上的關(guān)卡只在騎兵第一次移動(dòng)遇到時(shí)有效

#include<stdio.h>#include<windows.h> //顏色#include<string.h>#include<conio.h> //通過控制臺(tái)進(jìn)行數(shù)據(jù)輸入和數(shù)據(jù)輸出的函數(shù)#include<stdlib.h>#include<time.h>  //定義時(shí)間函數(shù)struct node //定義四個(gè)人物名{  char name[20];} people[4];int map[100]={  0,0,0,0,0,2,1,0,0,3,0,0,0,2,0,0,0,2,0,0,4,0,0,1,0,4,0,3,0,0,  0,0,0,2,0,  0,0,0,2,0,1,0,0,0,0,4,0,0,0,0,2,0,0,0,0,1,0,0,0,0,3,0,0,4,2,  0,0,0,0,1,  0,0,4,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,4,0,4,0,0,3,2,0,0,0,0,0}; //地圖的數(shù)字代碼int number1,number2; //玩家1的選擇人物序號(hào)int numstep1,numstep2; //玩家1的選擇人物序號(hào)int rand(void);  //偽隨機(jī)數(shù)生成函數(shù)void srand(unsigned int n); //種子函數(shù)void showmap(int map[])  //打印游戲地圖{  int i,j,k;  printf("\t\t\t騎 士 飛 行 棋 \n\n");  printf("'∷'是平地,'¤'是幸運(yùn)羅盤,'★'是地雷,'■'是暫停,'〓'是時(shí)空隧道\n\n");  for(i=0; i<31; i++)  {    if(map[i]==0)      printf("∷");    else if(map[i]==1)      printf("¤");    else if(map[i]==2)      printf("★");    else if(map[i]==3)      printf("■");    else if(map[i]==4)      printf("〓");    else if(map[i]==10)      printf("A");    else if(map[i]==20)      printf("B");    else if(map[i]==30)      printf("@@");  }  printf("\n");  for(k=0; k<4; k++)  {    for(j=0; j<30; j++)    {      printf(" ");    }    if(map[i]==0)      printf("∷");    else if(map[i]==1)      printf("¤");    else if(map[i]==2)      printf("★");    else if(map[i]==3)      printf("■");    else if(map[i]==4)      printf("〓");    else if(map[i]==10)      printf("A");    else if(map[i]==20)      printf("B");    else if(map[i]==30)      printf("@@");    i++;    printf("\n");  }  for(i=65; i>=35; i--)  {    if(map[i]==0)      printf("∷");    else if(map[i]==1)      printf("¤");    else if(map[i]==2)      printf("★");    else if(map[i]==3)      printf("■");    else if(map[i]==4)      printf("〓");    else if(map[i]==10)      printf("A");    else if(map[i]==20)      printf("B");    else if(map[i]==30)      printf("@@");  }  printf("\n");  i=66;  for(j=0; j<3; j++)  {    if(map[i]==0)      printf("∷");    else if(map[i]==1)      printf("¤");    else if(map[i]==2)      printf("★");    else if(map[i]==3)      printf("■");    else if(map[i]==4)      printf("〓");    else if(map[i]==10)      printf("A");    else if(map[i]==20)      printf("B");    else if(map[i]==30)      printf("@@");    i++;    printf("\n");  }  for(i=69; i<100; i++)  {    if(map[i]==0)      printf("∷");    else if(map[i]==1)      printf("¤");    else if(map[i]==2)      printf("★");    else if(map[i]==3)      printf("■");    else if(map[i]==4)      printf("〓");    else if(map[i]==10)      printf("A");    else if(map[i]==20)      printf("B");    else if(map[i]==30)      printf("@@");  }  printf("\n");}void cleana(int map[]) //清除地圖上的標(biāo)記A,并還原地圖{  int i;  for(i=0; i<100; i++)  {    if(map[i]==10)    {      if(i==6||i==23||i==40||i==55||i==69||i==83) //‘¤'所對(duì)應(yīng)地圖上的位置        map[i]=1;      else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) //‘★'所對(duì)應(yīng)地圖上的位置        map[i]=2;      else if(i==9||i==27||i==60||i==93)  //‘■'所對(duì)應(yīng)地圖上的位置        map[i]=3;      else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90)  //‘〓'所對(duì)應(yīng)地圖上的位置        map[i]=4;      else        map[i]=0;    }  }}void cleanb(int map[])  //清除地圖上的標(biāo)記B,并還原地圖{  int i;  for(i=0; i<100; i++)  {    if(map[i]==20)    {      if(i==6||i==23||i==40||i==55||i==69||i==83)  //‘¤'所對(duì)應(yīng)地圖上的位置        map[i]=1;      else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) //‘★'所對(duì)應(yīng)地圖上的位置        map[i]=2;      else if(i==9||i==27||i==60||i==93)  //‘■'所對(duì)應(yīng)地圖上的位置        map[i]=3;      else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90)  //‘〓'所對(duì)應(yīng)地圖上的位置        map[i]=4;      else        map[i]=0;    }  }}void showprocess(int map[]) //游戲進(jìn)行的過程{  int flag1=2,flag2=2; //控制游戲暫停的標(biāo)記變量  numstep1=0;  //玩家1的初始位置  numstep2=0;  //玩家2的初始位置  int numtou;  //每回投擲的骰子數(shù)  int t;    //作為幸運(yùn)羅盤交換位置時(shí)的中間變量  int number;  //作為輸入幸運(yùn)羅盤選擇時(shí)的變量  system("cls"); //清屏  showmap(map);  //調(diào)用函數(shù)showmap(map)用來輸出地圖  printf("\n");  while(numstep1<100&&numstep2<100) //游戲開始  {    system("pause"); //運(yùn)行時(shí)會(huì)出現(xiàn)“請(qǐng)按任意鍵繼續(xù) . .    printf("\n");    cleana(map);   //清除地圖上的標(biāo)記A    cleanb(map);   //清除地圖上的標(biāo)記B    if(flag1==2)   //判斷是否為暫停    {      if(flag2==0||flag2==1)      {        flag2++;      }      srand(time(NULL));  //是設(shè)置隨機(jī)數(shù)的種子,以當(dāng)前時(shí)間作為隨機(jī)數(shù)的種子      numtou=rand()%6+1;  //產(chǎn)生隨機(jī)數(shù)      numstep1+=numtou;      Sleep(500);     //暫停0.5秒      printf("\n玩家1擲出的點(diǎn)數(shù)為 %d\n\n",numtou);      if(numstep1>=100)  //步數(shù)大于100時(shí)跳出循環(huán),游戲結(jié)束      {        map[99]=10;   //使地圖上的最后一個(gè)位置為A        Sleep(1000);   //暫停1秒        system("cls");  //清屏        showmap(map);  //調(diào)用函數(shù)showmap(map)用來輸出地圖        printf("\n\n");        printf("游戲結(jié)束!\n");        break;      }      else      {        printf("玩家1%s,你當(dāng)前的位置為 %d\n",people[number1-1].name,numstep1);        Sleep(800);        if(map[numstep1-1]==0)   //地圖位置上為'∷'        {          map[numstep1-1]=10;          if(numstep1==numstep2) //判斷玩家1,玩家2的位置是否相同          {            printf("\n玩家2%s被炸飛了!\n",people[number2-1].name);            numstep2=0;            printf("玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);          }          if(numstep2!=0)            map[numstep2-1]=20;          Sleep(1000);   //暫停1秒          system("cls");  //清屏          showmap(map);  //打印地圖          printf("\n");        }        else if(map[numstep1-1]==1)  //地圖位置上為'¤',幸運(yùn)輪盤        {          printf("\n玩家1%s,恭喜你來到幸運(yùn)羅盤!\n",people[number1-1].name);          printf("請(qǐng)做出選擇:\n");          printf("1.和對(duì)方交換位置\n2.轟炸對(duì)方(炸退六步)\n");          scanf("%d",&number); //輸入選擇數(shù)          if(number==1)     //交換玩家位置          {            t=numstep1;            numstep1=numstep2;            numstep2=t;            printf("玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);            printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);            if(numstep1==0)            {              map[numstep2-1]=20;            }            else            {              map[numstep1-1]=10;              map[numstep2-1]=20;            }            if(numstep1==numstep2&&numstep1!=0)            {              map[numstep1-1]=30;            }            Sleep(1800);  //暫停1.8秒            system("cls"); //清屏            showmap(map);  //打印地圖            printf("\n");          }          else if(number==2)  //對(duì)方退六步          {            map[numstep1-1]=10;            if(numstep2>=6)            {              numstep2-=6;            }            else numstep2=0;            printf("玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);            if(numstep2!=0)            {              map[numstep2-1]=20;            }            if(numstep1==numstep2&&numstep1!=0)            {              map[numstep1-1]=30;            }            Sleep(1800);   //暫停1.8秒            system("cls");  //清屏            showmap(map);   //打印地圖            printf("\n");          }        }        else if(map[numstep1-1]==2)  //地圖位置上為'★',地雷        {          printf("\nSORRY , 你踩到地雷了 要后退6步●﹏●\n");          if(numstep1>=6)            numstep1-=6;          else numstep1=0;          printf("\n玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);          if(numstep1==0&&numstep2!=0)          {            map[numstep2-1]=20;          }          else if(numstep1!=0&&numstep2==0)          {            map[numstep1-1]=10;          }          else if(numstep1!=0&&numstep2!=0)          {            map[numstep1-1]=10;            map[numstep2-1]=20;          }          if(numstep1==numstep2&&numstep1!=0)          {            map[numstep1-1]=30;          }          Sleep(1800);   //暫停1.8秒          system("cls");  //清屏          showmap(map);  //打印地圖          printf("\n");        }        else if(map[numstep1-1]==3)   //地圖位置上為'■',暫停一次        {          flag1=0;          printf("\n~~>_<~~ 要停戰(zhàn)一局了!\n");          map[numstep1-1]=10;          if(numstep2!=0)          {            map[numstep2-1]=20;          }          if(numstep1==numstep2&&numstep1!=0)          {            map[numstep1-1]=30;          }          Sleep(1800);   //暫停1.8秒          system("cls");  //清屏          showmap(map);   //打印地圖          printf("\n");        }        else if(map[numstep1-1]==4)   //地圖位置上為'〓',時(shí)空隧道        {          printf("\nOh My God ,是時(shí)空隧道!! 沖啊^_^\n");          numstep1+=10;          if(numstep1>=100)          {            map[99]=10;            Sleep(1000);            system("cls");            showmap(map);            printf("\n\n");            printf("游戲結(jié)束!\n");            break;          }          printf("\n玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);          map[numstep1-1]=10;          if(numstep2!=0)          {            map[numstep2-1]=20;          }          if(numstep1==numstep2&&numstep1!=0)          {            map[numstep1-1]=30;          }          Sleep(1800);   //暫停1.8秒          system("cls");  //清屏          showmap(map);   //打印地圖          printf("\n");        }      }    }    else if(flag1!=2)  //當(dāng)玩家1為暫停狀態(tài)    {      flag1++;    }    system("pause");    //顯示"請(qǐng)按任意鍵繼續(xù)....."    printf("\n");    cleana(map);      //清除地圖上的標(biāo)記A    cleanb(map);      //清除地圖上的標(biāo)記B    if(flag2==2)    //判斷玩家2是否為暫停狀態(tài)    {      if(flag1==0||flag1==1)      {        flag1++;      }      srand(time(NULL));     //是設(shè)置隨機(jī)數(shù)的種子,以當(dāng)前時(shí)間作為隨機(jī)數(shù)的種子      numtou=rand()%6+1;     //產(chǎn)生隨機(jī)數(shù)      numstep2+=numtou;      Sleep(500);         //暫停0.5秒      printf("\n玩家2擲出的點(diǎn)數(shù)為%d\n\n",numtou);      if(numstep2>=100)      //步數(shù)大于100時(shí)跳出循環(huán),游戲結(jié)束      {        map[99]=20;       //使地圖上最后一個(gè)位置為B        Sleep(1000);      //暫停1秒        system("cls");     //清屏        showmap(map);      //打印地圖        printf("\n\n");        printf("游戲結(jié)束!\n");        break;      }      else      {        printf("玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);        Sleep(1000);       //暫停1秒        if(map[numstep2-1]==0)  //地圖位置上為'∷'        {          map[numstep2-1]=20;          if(numstep1==numstep2)          {            printf("\n玩家1%s被炸飛了!\n",people[number1-1].name);            numstep1=0;            printf("玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);          }          if(numstep1!=0)            map[numstep1-1]=10;          Sleep(1000);          system("cls");          showmap(map);          printf("\n");        }        else if(map[numstep2-1]==1)  //地圖上位置為'¤',幸運(yùn)輪盤        {          printf("\n玩家2%s,恭喜你來到幸運(yùn)羅盤!\n",people[number2-1].name);          printf("請(qǐng)做出選擇:\n");          printf("1.和對(duì)方交換位置\n2.轟炸對(duì)方(炸退六步)\n");          scanf("%d",&number);          if(number==1) //玩家雙方交換位置          {            t=numstep1;            numstep1=numstep2;            numstep2=t;            printf("\n玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);            printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);            if(numstep2==0)            {              map[numstep1-1]=10;            }            else            {              map[numstep1-1]=10;              map[numstep2-1]=20;            }            if(numstep1==numstep2&&numstep1!=0)            {              map[numstep1-1]=30;            }            Sleep(1800);            system("cls");            showmap(map);            printf("\n");          }          else if(number==2)  //對(duì)方退六步          {            map[numstep2-1]=20;            if(numstep1>=6)            {              numstep1-=6;            }            else numstep1=0;            printf("玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);            if(numstep1!=0)            {              map[numstep1-1]=10;            }            if(numstep1==numstep2&&numstep1!=0)            {              map[numstep1-1]=30;            }            Sleep(1800);            system("cls");            showmap(map);            printf("\n");          }        }        else if(map[numstep2-1]==2)  //地圖上位置為'★',地雷        {          printf("\nSORRY , 你踩到地雷了 要后退6步●﹏●\n");          if(numstep2>=6)            numstep2-=6;          else numstep2=0;          printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);          if(numstep2==0&&numstep1!=0)          {            map[numstep1-1]=10;          }          else if(numstep2!=0&&numstep1==0)          {            map[numstep2-1]=20;          }          else if(numstep1!=0&&numstep2!=0)          {            map[numstep1-1]=10;            map[numstep2-1]=20;          }          if(numstep1==numstep2&&numstep1!=0)          {            map[numstep1-1]=30;          }          Sleep(1800);          system("cls");          showmap(map);          printf("\n");        }        else if(map[numstep2-1]==3)  //地圖位置上為'■',暫停一次        {          flag2=0;          printf("\n~~>_<~~ 要停戰(zhàn)一局了\n");          map[numstep2-1]=20;          if(numstep1!=0)          {            map[numstep1-1]=10;          }          if(numstep1==numstep2&&numstep1!=0)          {            map[numstep1-1]=30;          }          Sleep(1800);          system("cls");          showmap(map);          printf("\n");        }        else if(map[numstep2-1]==4)     //地圖位置上為'〓',時(shí)空隧道        {          printf("\nOh My God ,是時(shí)空隧道??! 沖啊^_^\n");          numstep2+=10;          if(numstep1>=100)  //步數(shù)大于100,跳出循環(huán)          {            map[99]=10;            Sleep(1000);            system("cls");            showmap(map);            printf("\n\n");            printf("游戲結(jié)束!\n");            break;          }          printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);          map[numstep2-1]=20;          if(numstep1!=0)          {            map[numstep1-1]=10;          }          if(numstep1==numstep2&&numstep1!=0)          {            map[numstep1-1]=30;          }          Sleep(1800);          system("cls");          showmap(map);          printf("\n");        }      }    }    else if(flag2!=0)    {      flag2++;    }  }  if(numstep1>numstep2) //判斷玩家的輸贏    printf("\n恭喜玩家1%s,你贏了!!!!\n",people[number1-1].name);  else printf("\n恭喜玩家2%s,你贏了!!!!\n",people[number2-1].name);}void showready(){  int i;  printf("地圖載入中——");  for(i=0; i<15; i++)  {    printf(".");    Sleep(100);  }  system("cls");  showmap(map);  printf("\n\n");  printf("玩家1%s,你當(dāng)前的位置為 0\n",people[number1-1].name);  printf("玩家2%s,你當(dāng)前的位置為 0\n\n",people[number2-1].name);  system("pause");  printf("\n游戲開始!\n請(qǐng)玩家1先開始擲骰子\n");  Sleep(1000);  showprocess(map);}void showstart()  //展示游戲開始界面{  int i;  int choose;  system("color 71");  printf("**************************************************\n");  printf("//                       //\n");  printf("//                       //\n");  printf("//        騎 士 飛 行 棋         //\n");  printf("//                       //\n");  printf("//                       //\n");  printf("**************************************************\n");  for(i=0; i<5; i++)  {    printf("\n");  }  printf("~~~~~~~~~~~兩 人 對(duì) 戰(zhàn)~~~~~~~~~~~\n\n");  printf("請(qǐng)選擇角色:");  strcpy(people[0].name,"戴高樂");  strcpy(people[1].name,"艾森豪威爾");  strcpy(people[2].name,"麥克阿瑟");  strcpy(people[3].name,"巴頓");  for(i=1; i<=4; i++)  {    printf("%d.%s ",i,people[i-1].name);  }  printf("\n");  printf("請(qǐng)玩家1選擇角色(選擇序號(hào)):");  scanf("%d",&number1);  printf("請(qǐng)玩家2選擇角色(選擇序號(hào)):");  scanf("%d",&number2);  printf("\n\n");  printf("1.直接進(jìn)行游戲  2.閱讀游戲規(guī)則\n");  scanf("%d",&choose);  if(choose==1)  {    showready();  }  else if(choose==2)  //展示游戲規(guī)則  {    system("cls");    printf("\n~~~~~~~~~~~~~~~~~~~游戲規(guī)則如下~~~~~~~~~~~~~~~~~~~~\n\n");    printf("1.兩個(gè)玩家輪流擲骰子,如果上輪走到暫停關(guān)卡,停擲一次\n\n");    printf("2.若玩家走到幸運(yùn)輪盤,則和對(duì)方交換位置或者對(duì)方后退6步\n\n");    printf("3.若玩家走到某格,而對(duì)方也在此格,則對(duì)方退回原點(diǎn)\n\n");    printf("4.若遇到地雷后退6步\n\n");    printf("5.若遇到暫停則此玩家下一回合停止擲骰子\n\n");    printf("6.若遇到時(shí)空隧道再前進(jìn)10步\n\n");    printf("注意:棋盤上的關(guān)卡只在騎兵第一次移動(dòng)遇到時(shí)有效\n\n");    Sleep(1800);    system("pause");    printf("\n");    showready();  }}int main(){  char str[10];  showstart();  printf("\n是否再來一局?請(qǐng)選擇: (Yes/No)\n");  //判斷是否再來一局  scanf("%s",str);  if(strcmp(str,"Yes")==0)  {    system("cls");    cleana(map);    cleanb(map);    showstart();  }  if(strcmp(str,"No")==0)    return 0;}

到此,關(guān)于“用C語(yǔ)言實(shí)現(xiàn)騎士飛行棋小游戲”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI