温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何用代码实现飞行棋简单小游戏

发布时间:2021-03-15 10:32:01 来源:亿速云 阅读:223 作者:TREX 栏目:开发技术

本篇内容主要讲解“C#如何用代码实现飞行棋简单小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#如何用代码实现飞行棋简单小游戏”吧!

目标:实现飞行棋游戏基础功能

玩家在地图触发道具:

1、获得道具,可以进行一次选择 1–交换位置 2–让对方退随机格子
2、踩到炸弹,让对方暂停一回合
3、乘上了飞机,前进10格
4、进入隧道,将随机从其他隧道口出来

C#如何用代码实现飞行棋简单小游戏

C#如何用代码实现飞行棋简单小游戏

C#如何用代码实现飞行棋简单小游戏

C#如何用代码实现飞行棋简单小游戏

using System;

namespace FXQGame
{
  class Program
  {
    //储存地图数组
    static int[] mMaps = new int[120];
    //储存两个玩家的坐标
    static int[] mPlayerPos = new int[2];
    static string[] mPlayerName = { "玩家一","玩家二" };
    //下标控制当前玩家
    static int index;
    static void Main(string[] args)
    {
      //游戏头 展示信息
      GameTitle();
      Console.WriteLine(mPlayerName[0]);
      Console.WriteLine(mPlayerName[1]);
      Console.WriteLine("游戏玩法:");
      Console.WriteLine("输入任意键开始游戏");
      //按下任意键进入下一步骤
      Console.ReadKey();
      //清屏
      Console.Clear();
      //初始化地图
      InitGameMap();
      //绘制地图
      DrawMap();
      //进入游戏
      Update();
      //游戏结束
      if (mPlayerPos[0] > mPlayerPos[1])
      {
        Console.WriteLine("{0}获得胜利",mPlayerName[0]);
      }
      else
      {
        Console.WriteLine("{0}获得胜利", mPlayerName[1]);
      }

      Console.ReadKey();
    }
    //游戏逻辑
    private static void Update()
    {
      index = 0;

      do
      {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("{0}按任意键开始投掷骰子", mPlayerName[index % 2]);
        Console.ReadKey(true);
        int ran = GetRanden(1, 7);
        Console.WriteLine("{0}掷出{1}点", mPlayerName[index % 2], ran);
        mPlayerPos[index % 2] += ran;
        Console.ReadKey(true);
        Console.WriteLine("{0}开始行动", mPlayerName[index % 2]);
        //玩家A与玩家B接触
        if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] != 0)
        {
          Console.WriteLine("{0}走到了{1}的背后进行攻击,{2}后退3格", mPlayerName[index % 2], mPlayerName[(index + 1) % 2], mPlayerName[(index + 1) % 2]);
          if (mPlayerPos[(index + 1) % 2] - 3 < 0)
          {
            mPlayerPos[(index + 1) % 2] = 0;
          }
          else
          {
            mPlayerPos[(index + 1) % 2] -= 3;
          }
          Console.ReadKey(true);
        }
        else//玩家在地图触发道具
        {
          switch (mMaps[mPlayerPos[index % 2]])
          {
            case 0:
              Console.WriteLine("{0}踩到方块,安全", mPlayerName[index % 2]);
              Console.ReadKey(true);
              break;
            case 1:
              Console.WriteLine("{0}获得道具,可以进行一次选择 1--交换位置 2--让对方退随机格子", mPlayerName[index % 2]);
              string input = Console.ReadLine();
              while (true)
              {
                if (input == "1")
                {
                  Console.WriteLine("玩家{0}选择跟玩家{1}交换位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]);
                  int temp = mPlayerPos[0];
                  mPlayerPos[0] = mPlayerPos[1];
                  mPlayerPos[1] = temp;
                  Console.WriteLine("交换完毕,请按任意键进行游戏");
                  Console.ReadKey(true);
                  break;
                }
                else if (input == "2")
                {
                  Console.WriteLine("玩家{0}选择让玩家{1}退回随机位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]);
                  int r = GetRanden(1, 7);
                  if (mPlayerPos[(index + 1) % 2] - r < 0)
                  {
                    mPlayerPos[(index + 1) % 2] = 0;
                  }
                  else
                  {
                    mPlayerPos[(index + 1) % 2] -= r;
                  }

                  Console.WriteLine("玩家{0}本回合得退回{1}位置,按任意键进行游戏", mPlayerName[(index + 1) % 2], r);
                  Console.ReadKey(true);
                  break;
                }
                else
                {
                  Console.WriteLine("输入无效字符,请重新输入");
                  input = Console.ReadLine();
                }
              }
              break;
            case 2:
              Console.WriteLine("{0}踩到炸弹,让对方暂停一回合", mPlayerName[index % 2]);
              index++;
              Console.ReadKey(true);
              break;
            case 3:
              Console.WriteLine("{0}乘上了飞机,前进10格", mPlayerName[index % 2]);
              mPlayerPos[index % 2] += 10;
              Console.ReadKey(true);
              break;
            case 4:
              Console.WriteLine("{0}进入隧道,将随机从其他隧道口出来", mPlayerName[index % 2]);
              int[] tunnel = { 20, 25, 45, 63, 72, 88, 90 };
              mPlayerPos[index % 2] = tunnel[GetRanden(0, 7)];
              Console.WriteLine("{0}从其他隧道口出来", mPlayerName[index % 2], mPlayerPos[index % 2]);
              Console.ReadKey(true);
              break;
          }
        }
        index++;
        Console.Clear();
        DrawMap();
      }while (mPlayerPos[0] < 99 && mPlayerPos[1] < 99);
      //当某一位玩家到达终点时,结束游戏
    }
    //得到随机数
    private static int GetRanden(int min,int max)
    {
      Random random = new Random();
      return random.Next(min, max);
    }
    //游戏头介绍
    private static void GameTitle()
    {
      Console.ForegroundColor = ConsoleColor.Green;
      Console.WriteLine(DateTime.Now);
      Console.WriteLine("飞行棋双人小游戏");
      Console.WriteLine("************************************");
    }
    //初始化游戏地图
    private static void InitGameMap()
    {
      //玩家道具 在数组中存储为1 可以让敌人退后随机距离 也可以交换双方位置
      int[] planeItem = { 6,23,40,55,69,83 };
      for(int i = 0; i< planeItem.Length; i++)
      {
        mMaps[planeItem[i]] = 1;
      }
      //炸弹道具 在数组中存储为2 暂停一回合
      int[] bombItem = {5,13,17,33,38,50,64,80,94 };
      for (int i = 0; i < bombItem.Length; i++)
      {
        mMaps[bombItem[i]] = 2;
      }
      //飞机道具 在数组中存储为3 前进10格
      int[] propsItem = {9,27,60,93 };
      for (int i = 0; i < propsItem.Length; i++)
      {
        mMaps[propsItem[i]] = 3;
      }
      //隧道 在数组中存储为4 从其他随机隧道钻出
      int[] tunnelItem = {20,25,45,63,72,88,90 };
      for (int i = 0; i < tunnelItem.Length; i++)
      {
        mMaps[tunnelItem[i]] = 4;
      }
    }
    //绘制地图
    private static void DrawMap()
    {
      Console.WriteLine("玩家一用A表示,玩家二用B表示");
      Console.WriteLine("图例:□普通方块 ◇道具 ●炸弹 $飞机 ¤隧道");
      //第一行
      for (int i = 0; i < 30; i++)
      {
        Draw(i);
      }
      //第一竖行
      Console.WriteLine();
      for (int i = 30; i < 35; i++)
      {
        for (int j = 0; j <= 28; j++)
        {//两个空格
          Console.Write(" ");
        }
        Draw(i);
        Console.WriteLine();
      }
      //第二行
      for (int i = 64; i >= 35; i--)
      {
        Draw(i);
      }
      Console.WriteLine();
      //第二竖行
      for(int i = 65; i <= 69; i++)
      {
        Draw(i);
        Console.WriteLine();
      }
      //第三行
      for (int i = 70; i <= 99; i++)
      {
        Draw(i);
      }
      Console.WriteLine();
    }
    //判断当前点属于什么特殊属性 并绘制出来
    private static void Draw(int i)
    {
      //当两人重合显示
      if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] == i)
      {
        Console.Write("<>");
      }
      else if (mPlayerPos[0] == i)
      {//玩家一显示A
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("A");
      }
      else if (mPlayerPos[1] == i)
      {//玩家二显示B
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("B");
      }
      else
      {
        switch (mMaps[i])
        {
          case 0:
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("□");
            break;
          case 1:
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("◇");
            break;
          case 2:
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write("●");
            break;
          case 3:
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("$");
            break;
          case 4:
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("¤");
            break;
        }
      }
    }
  }
}

到此,相信大家对“C#如何用代码实现飞行棋简单小游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI