使用C#怎么實現一個飛行棋游戲

這期內容當中小編將會給大家帶來有關使用C#怎么實現一個飛行棋游戲,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創新互聯建站是一家以網絡技術公司,為中小企業提供網站維護、成都網站建設、成都做網站、網站備案、服務器租用、域名注冊、軟件開發、成都小程序開發等企業互聯網相關業務,是一家有著豐富的互聯網運營推廣經驗的科技公司,有著多年的網站建站經驗,致力于幫助中小企業在互聯網讓打出自已的品牌和口碑,讓企業在互聯網上打開一個面向全國乃至全球的業務窗口:建站服務熱線:18982081108

源碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 飛行棋
{
 class Program
 {
  //我們用靜態字段來模擬全局變量
  static int[] Maps=new int[100];
  //聲明一個靜態字段的數組用來存儲玩家A和玩家B的坐標
  static int[] playerPos = new int[2];
  //存儲兩個玩家的姓名
  static string[] playerNames = new string[2];
  //兩個玩家的標記
  static bool[] flags = new bool[2]; //flags[0]玩家A和flags[1]玩家B默認都是false
  static void Main(string[] args)
  {
   GameShow(); //加載游戲頭
   #region 輸入玩家姓名
   Console.WriteLine("請輸入玩家A的姓名");
   playerNames[0] = Console.ReadLine();
   while (playerNames[0]=="")
   {
    Console.WriteLine("玩家A姓名不能為空,請重新輸入");
    playerNames[0] = Console.ReadLine();
   }
   Console.WriteLine("請輸入玩家B的姓名");
   playerNames[1] = Console.ReadLine();
   while (playerNames[1] == ""||playerNames[1]==playerNames[0])
   {
    if (playerNames[1] == "")
    {
     Console.WriteLine("姓名不能為空,請重新輸入");
     playerNames[1] = Console.ReadLine();
    }
    else
    {
     Console.WriteLine("玩家B姓名不能重復,請重新輸入");
     playerNames[1] = Console.ReadLine();
    }
   }
   #endregion
   //玩家姓名寫好后,進行清屏
   Console.Clear(); //清屏
   GameShow();
   Console.WriteLine("{0}的士兵用A表示", playerNames[0]);
   Console.WriteLine("{0}的士兵用B表示", playerNames[1]);
   Initailmap(); //初始化地圖
   DrowMap();  //畫出地圖---注意:畫地圖前首先應初始化地圖

   //當玩家A和玩家B沒有一個人到達終點的時候都在游戲中
   while (playerPos[0] < 99 && playerPos[1] < 99)
   {
    if (flags[0] == false)
    {
     PlayGame(0);     
    }
    else
    {
     flags[0] = false;
    } 
    if (playerPos[0] >= 99)
    {
     Console.WriteLine("玩家{0}贏了玩家{1}", playerNames[0], playerNames[1]);
     break;
    }
    if (flags[1] == false)
    {
     PlayGame(1);
    }
    else
    {
     flags[1] = false;
    }
    if (playerPos[1] >= 99)
    {
     Console.WriteLine("玩家{0}贏了玩家{1}", playerNames[1], playerNames[0]);
     break;
    }
   }
   Console.ReadKey();
  }
  /// <summary>
  /// 設置游戲頭及輸出內容的顏色
  /// </summary>
  public static void GameShow()
  {
   Console.ForegroundColor = ConsoleColor.Blue;  //設置輸出內容的前景色
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.Cyan;
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.Red;
   Console.WriteLine("******飛行棋大戰******");
   Console.ForegroundColor = ConsoleColor.Yellow;
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.DarkGray;
   Console.WriteLine("**********************");
   Console.ForegroundColor = ConsoleColor.Green;
   Console.WriteLine("**********************");
  }
  /// <summary>
  /// 初始化地圖
  /// </summary>
  public static void Initailmap()
  {
   int[] lucklyturn = { 6, 23, 40, 55, 69, 83 }; //幸運輪盤
   for (int i = 0; i < lucklyturn.Length; i++)
   {
    int index = lucklyturn[i];  
    Maps[index] = 1; //將幸運輪盤的位置處值都設為1
   }
   int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
   for (int i = 0; i < landMine.Length; i++)
   {
    int index = landMine[i];
    Maps[index] = 2;  //將地雷的位置處值都設為2
   }
   int[] pause = { 9, 27, 60, 93 }; //暫停
   for (int i = 0; i < pause.Length; i++)
   {
    int index = pause[i];
    Maps[index] = 3;  //將暫停的位置處值都設為3
   }
   int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//時空隧道
   for (int i = 0; i <timeTunnel.Length; i++)
   {
    int index=timeTunnel[i];
    Maps[index] = 4;  //將時空隧道的位置處值都設為4
   }
  }
  /// <summary>
  /// 畫出地圖
  /// </summary>
  public static void DrowMap()
  {
   Console.WriteLine("圖例:普通方塊:□ 幸運輪盤:◎ 地雷:★  暫停:▲  時空隧道:卍");
   //第一橫行0--29
   for (int i = 0; i <= 29; i++)
   {
    Console.Write(DrowStringMap(i)); //輸出返回的當前單元格的圖形
   }
   //畫完第一行后換行
   Console.WriteLine();
   //第一豎行30--34
   for (int i = 30; i <= 34; i++)
   {
    for (int j = 0; j < 29; j++)
    {
     Console.Write(" "); //兩個空格
    }
    Console.Write(DrowStringMap(i)); 
    Console.WriteLine(); 
   }
   //第二橫行35--64
   for (int i = 64; i >=35; i--)
   {
    Console.Write(DrowStringMap(i));
   }
   //第二橫行打印完后換行
   Console.WriteLine();
   //第二豎行65--69
   for (int i = 65; i <= 69; i++)
   {
    Console.Write(DrowStringMap(i));
    Console.WriteLine();
   }
   //第三橫行70--99
   for (int i = 70; i <= 99; i++)
   {
    Console.Write(DrowStringMap(i));
   }
   Console.WriteLine(); //畫完最后一行地圖后應該換行
  }
  /// <summary>
  /// 畫當前的單元格的圖案
  /// </summary>
  /// <param name="i">傳入當前單元格的索引</param>
  /// <returns>返回當前單元格的圖形</returns>
  public static string DrowStringMap(int i)
  {
   string str = " ";
    //如果玩家A和玩家B的坐標相同并且都在地圖上畫一個<>(因為剛開始和結束的時候玩家可能去地圖外)
    if (playerPos[0] == playerPos[1] && playerPos[0] == i)
    {
     Console.ForegroundColor = ConsoleColor.Blue;
     str="<>";
    }
    else if (playerPos[0] == i)
    {
     Console.ForegroundColor = ConsoleColor.Blue;
     str="A";
    }
    else if (playerPos[1] == i)
    {
     Console.ForegroundColor = ConsoleColor.Blue;
     str="B";
    }
    else
    {
     switch (Maps[i])
     {
      case 0:
       Console.ForegroundColor = ConsoleColor.DarkYellow;
       str="□";
       break;
      case 1:
       Console.ForegroundColor = ConsoleColor.Green;
       str="◎";
       break;
      case 2:
       Console.ForegroundColor = ConsoleColor.Red;
       str="★";
       break;
      case 3:
       Console.ForegroundColor = ConsoleColor.Blue;
       str="▲";
       break;
      case 4:
       Console.ForegroundColor = ConsoleColor.DarkCyan;
       str="卍";
       break;
     }
    } //else
    return str;
  }
  /// <summary>
  /// 玩游戲
  /// </summary>
  public static void PlayGame(int playerNumber)
  {
   Random r = new Random();
   int rNumber = r.Next(1, 7);
   Console.WriteLine("{0}玩家開始擲骰子", playerNames[playerNumber]);
   Console.ReadKey(true); //ReadKey是重載函數,里面參數為true表示不顯示按下的鍵。
   Console.WriteLine("{0}擲出了{1}", playerNames[playerNumber], rNumber);
   Console.ReadKey(true);
   Console.WriteLine("{0}按任意鍵開始行動", playerNames[playerNumber]);
   Console.ReadKey(true);
   playerPos[playerNumber] += rNumber;
   ChangePos();
   Console.WriteLine("玩家{0}行動完了", playerNames[playerNumber]);
   Console.ReadKey(true);
   //玩家A有可能踩到玩家B
   if (playerPos[playerNumber] == playerPos[1 - playerNumber])
   {
    Console.WriteLine("玩家{0}踩到了玩家{1},{2}退6格", playerNames[0], playerNames[1], playerNames[1]);
    playerPos[1 - playerNumber] -= 6;  //如果玩家A踩到了玩家B,玩家B退6格
    ChangePos();
    Console.ReadKey(true);
   }
   //玩家A有可能踩到方塊,時空隧道,暫停,地雷,幸運輪盤
   else
   {
    switch (Maps[playerPos[playerNumber]])
    {
     case 0:
      Console.WriteLine("玩家{0}踩到了方塊,安全", playerNames[playerNumber]);
      Console.ReadKey(true);
      break;
     case 1:
      Console.WriteLine("玩家{0}踩到了幸運輪盤,請選擇a換位置;b轟炸對方,讓對方退6格", playerNames[playerNumber]);
      string input = Console.ReadLine();
      while (true)
      {
       if (input == "a")
       {
        Console.WriteLine("玩家{0}和玩家{1}交換位置", playerNames[playerNumber], playerNames[1 - playerNumber]); //*******
        Console.ReadKey(true);
        int temp = playerPos[playerNumber];
        playerPos[playerNumber] = playerPos[1 - playerNumber];
        playerPos[1 - playerNumber] = temp;
        Console.WriteLine("玩家{0}和玩家{1}位置交換成功,按任意鍵繼續游戲。", playerNames[playerNumber], playerNames[1 - playerNumber]);
        Console.ReadKey(true);
        break;
       }
       else if (input == "b")
       {
        Console.WriteLine("玩家{0}選擇轟炸玩家{1},玩家{2}的位置退6格", playerNames[playerNumber], playerNames[1 - playerNumber], playerNames[1 - playerNumber]);
        Console.ReadKey(true);
        playerPos[1 - playerNumber] -= 6;
        ChangePos();
        Console.WriteLine("玩家{0}的位置退6格", playerNames[1 - playerNumber]);
        Console.ReadKey(true);
        break;
       }
       else
       {
        Console.WriteLine("只能輸入a或者b,輸入錯誤,請重新輸入");
        input = Console.ReadLine();
       }
      }
      break;
     case 2:
      Console.WriteLine("玩家{0}踩到了地雷,退6格", playerNames[playerNumber]);
      Console.ReadKey(true);
      playerPos[playerNumber] -= 6;
      ChangePos();
      break;
     case 3:
      Console.WriteLine("玩家{0}踩到了暫停,暫停一回合", playerNames[playerNumber]);
      flags[playerNumber] = true;
      Console.ReadKey(true);
      break;
     case 4:
      Console.WriteLine("玩家{0}踩到了時空隧道,前進10格", playerNames[playerNumber]);
      playerPos[playerNumber] += 10;
      ChangePos();
      break;
    } //switch
   } //else
   ChangePos();
   Console.Clear();
   //清屏
   DrowMap();  //重新畫
  }
  /// <summary>
  /// 當玩家坐標發生改變的時候調用
  /// </summary>
  public static void ChangePos()
  {
   if (playerPos[0] < 0)
   {
    playerPos[0] = 0;
   }
   else if (playerPos[0] >99)
   {
    playerPos[0] = 99;
   }
   else if (playerPos[1] < 0)
   {
    playerPos[1] = 0;
   }
   else if (playerPos[1] > 99)
   {
    playerPos[1] = 99;
   }
   
  }
 }
}

上述就是小編為大家分享的使用C#怎么實現一個飛行棋游戲了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。

本文標題:使用C#怎么實現一個飛行棋游戲
URL鏈接:http://m.kartarina.com/article46/jeddeg.html

成都網站建設公司_創新互聯,為您提供商城網站網站維護網站收錄外貿建站網站制作

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司
主站蜘蛛池模板: 无码一区二区波多野结衣播放搜索| 国产乱子伦精品无码码专区| 久久无码AV一区二区三区| 国99精品无码一区二区三区| 中文字幕人成无码免费视频| 免费无码又爽又刺激一高潮| yy111111少妇影院里无码| 亚洲av无码国产精品夜色午夜| 国产精品无码专区AV在线播放| 亚洲不卡中文字幕无码| 无码的免费不卡毛片视频| 亚洲av极品无码专区在线观看| 一区二区三区无码高清视频| 亚洲欧洲AV无码专区| 无码人妻熟妇AV又粗又大| 精品无码久久久久久久动漫| 亚洲精品无码成人| 91精品久久久久久无码| 亚洲AV无码日韩AV无码导航| 五十路熟妇高熟无码视频| 亚洲精品无码aⅴ中文字幕蜜桃| 无码人妻精品一区二区三区东京热 | 久久亚洲日韩看片无码| 久久亚洲国产成人精品无码区| 亚洲AV永久无码天堂影院| 无码137片内射在线影院| 中文字幕日韩精品无码内射| 乱人伦人妻中文字幕无码久久网| 2020无码专区人妻系列日韩| 亚洲成a∨人片在无码2023| 亚洲精品无码久久久久久久| 无码人妻精品一区二区三区久久 | 国精品无码一区二区三区在线蜜臀| 亚洲精品无码av中文字幕| 精品国产一区二区三区无码| 午夜无码伦费影视在线观看| 精品无码一区在线观看| AV无码免费永久在线观看| 777爽死你无码免费看一二区| 毛片无码免费无码播放| 激情无码人妻又粗又大中国人|