C#编程基础综合应用_骑士飞行棋

来源:互联网 发布:车间布局仿真软件 编辑:程序博客网 时间:2024/06/05 22:35


游戏说明:

玩家一用A表示,玩家二用B表示,玩家一与玩家二在同一位置用¤表示

地图图例:□正常        ◎幸运轮盘        ●地雷        ◆暂停       〓时空隧道

两天时间,第一天看视频里老师写代码的过程自己去理解,并把程序要点简记在本子上,第二天开始敲代码,完成整个程序。通过这个小游戏的编写,对C#语言的语法有了更好的掌握,加深了记忆。等跑完流程打算把地图的形状改一下,考虑二次碰到道具的情况,优化一下代码。

规则说明:

地图由正常道路与道具随机组成,共一百格,玩家掷骰子然后按照掷出的点数前行,先到达终点的玩家取胜。

若走到幸运轮盘,有两种运气供玩家选择,第一,可以与对方交换位置,第二,可以轰炸对方(即对方后退五格)

若走到地雷,玩家将后退三格。若走到暂停,玩家将停掷骰子一次。若走到时空隧道,玩家将前进十格。

以第一次走到的道具为准,忽略第二次走到的道具。


字段说明:

static int[] map——地图,用于存储地图的坐标

static int[] playerPos—— 玩家位置,用于存储玩家一、二的坐标

static string[] name——玩家姓名,用于存储玩家一、二的姓名

static bool[] isStop——暂停标记,用于设置玩家一、二暂停


方法说明:

public static void SetName()——按照玩家的输入给玩家一、二命名

public static void ShowUI()——屏幕输出游戏名称

public static void InitialMap()——设置正常道路与道具的位置

public static string DrawMap(int pos, int j)——按照设定的地图,在对应位置设置图标

public static void Map()——依次在屏幕上输出设置好的图标,画出整个地图

public static void CheckPos()——检查玩家坐标是否越界(小于零或大于九十九)

public static int UserSelect()——检测用户的输入是否规范

public static void ThrowDice(int playerNum)——掷骰子,并按掷出点数以及走到的道具前行


特别功能:

if (input.Key == ConsoleKey.Z && input.Modifiers == (ConsoleModifiers.Control | ConsoleModifiers.Alt))
{
        playerPos[playerNum] = 99;
}
else

{……}

作弊小暗门——玩家同时按Ctrl+Alt+Z可以瞬时到达终点


附上完整代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 飞行棋{    class Program    {        static int[] map = new int[100];        static int[] playerPos = new int[] { 0, 0 };        static string[] name = new string[2];        static bool[] isStop = { false, false };        static void Main(string[] args)        {            ShowUI();            SetName();            Console.Clear();            ShowUI();            Console.WriteLine("Are you ready?(按任意键开始)");            Console.ReadKey();            Console.WriteLine("###########################对战开始#########################");            Console.WriteLine("{0}用A表示      {1}用B表示    {0}与{1}在同一位置用¤表示", name[0], name[1]);            Console.ReadKey();            Console.WriteLine("图例: ◎幸运轮盘      ●地雷      ◆暂停      〓时空隧道");            InitialMap();            Map();            Console.ResetColor();            do            {                ThrowDice(0);                if (playerPos[0] >= 99 || playerPos[1] >= 99)                    break;                ThrowDice(1);                if (playerPos[0] >= 99 || playerPos[1] >= 99)                    break;            } while (true);            if (playerPos[0] == 99)                Console.WriteLine("{0}胜利啦!!!", name[0]);            else                Console.WriteLine("{0}胜利啦!!!", name[1]);            Console.ReadKey();        }        public static void SetName()        {            Console.WriteLine("请输入玩家一的姓名");            do            {                name[0] = Console.ReadLine();                if (name[0] != "")                    break;                Console.WriteLine("姓名不能为空,请重新输入");            } while (true);            Console.WriteLine("请输入玩家二的姓名");            do            {                name[1] = Console.ReadLine();                if (name[1] != "" && name[0] != name[1])                    break;                else if (name[0] == name[1])                    Console.WriteLine("玩家二姓名不能与玩家一姓名相同");                else                    Console.WriteLine("姓名不能为空");            } while (true);        }        public static void ShowUI()        {            Console.WriteLine("############################################################");            Console.WriteLine("###                                                      ###");            Console.WriteLine("###                   骑  士  飞  行  棋                 ###");            Console.WriteLine("###                                                      ###");            Console.WriteLine("############################################################");        }        public static void InitialMap()        {            int[] luckyTurn = { 23, 36, 45, 56, 75, 78, 85 };            int[] landMine = { 9, 29, 33, 41, 67, 77, 83, 92, 99 };            int[] pause = { 2, 14, 22, 37, 43, 51, 63, 72, 88, 91, 97 };            int[] timeTunnel = { 7, 18, 26, 59, 90 };            for (int i = 0; i < luckyTurn.Length; i++)            {                map[luckyTurn[i]] = 1;            }            for (int i = 0; i < landMine.Length; i++)            {                map[landMine[i]] = 2;            }            for (int i = 0; i < pause.Length; i++)            {                map[pause[i]] = 3;            }            for (int i = 0; i < timeTunnel.Length; i++)            {                map[timeTunnel[i]] = 4;            }        }        public static string DrawMap(int pos, int j)        {            string result = "";            if (playerPos[0] == j && playerPos[1] == j)            {                Console.ForegroundColor = ConsoleColor.DarkYellow;                result = "¤";            }            else if (playerPos[0] == j)            {                Console.ForegroundColor = ConsoleColor.Gray;                result = "A";            }            else if (playerPos[1] == j)            {                Console.ForegroundColor = ConsoleColor.Gray;                result = "B";            }            else            {                switch (pos)                {                    case 0:                        Console.ForegroundColor = ConsoleColor.White;                        result = "□";                        break;                    case 1:                        Console.ForegroundColor = ConsoleColor.Red;                        result = "◎";                        break;                    case 2:                        Console.ForegroundColor = ConsoleColor.Blue;                        result = "●";                        break;                    case 3:                        Console.ForegroundColor = ConsoleColor.Yellow;                        result = "◆";                        break;                    case 4:                        Console.ForegroundColor = ConsoleColor.Green;                        result = "〓";                        break;                }            }            return result;        }        public static void Map()        {            for (int i = 0; i < 30; i++)            {                Console.Write(DrawMap(map[i], i));            }            Console.WriteLine();            for (int i = 30; i < 35; i++)            {                for (int j = 0; j < 29; j++)                {                    Console.Write("  ");                }                Console.WriteLine(DrawMap(map[i], i));            }            for (int i = 64; i > 34; i--)            {                Console.Write(DrawMap(map[i], i));            }            Console.WriteLine();            for (int i = 65; i < 70; i++)            {                Console.WriteLine(DrawMap(map[i], i));            }            for (int i = 70; i < 100; i++)            {                Console.Write(DrawMap(map[i], i));            }            Console.WriteLine();        }        public static void CheckPos()        {            for (int i = 0; i < 2; i++)            {                if (playerPos[i] > 99)                    playerPos[i] = 99;                if (playerPos[i] < 0)                    playerPos[i] = 0;            }        }        public static int UserSelect()        {            int sel;            do            {                string input = Console.ReadLine();                try                {                    sel = int.Parse(input);                    if (sel != 1 && sel != 2)                        Console.WriteLine("您的输入有误,请重新输入");                    break;                }                catch                {                    Console.WriteLine("您的输入有误,请重新输入");                    continue;                }            } while (true);            return sel;        }        public static void ThrowDice(int playerNum)        {            if (isStop[playerNum] == false)            {                Console.WriteLine("{0}按任意键掷骰子", name[playerNum]);                ConsoleKeyInfo input = Console.ReadKey(true);                if (input.Key == ConsoleKey.Z && input.Modifiers == (ConsoleModifiers.Control | ConsoleModifiers.Alt))                {                    playerPos[playerNum] = 99;                }                else                {                    Random num = new Random();                    int number = num.Next(1, 7);                    Console.WriteLine("{0}前进{1}步", name[playerNum], number);                    playerPos[playerNum] += number;                    CheckPos();                    if (playerPos[playerNum] == playerPos[1 - playerNum])                    {                        Console.WriteLine("{0}好悲催啊,被{1}踩到了,只能从头再来", name[1 - playerNum], name[playerNum]);                        playerPos[1 - playerNum] = 0;                    }                    else                    {                        switch (map[playerPos[playerNum]])                        {                            case 1:                                Console.WriteLine("你好幸运啊,碰到幸运轮盘了");                                Console.ReadKey(true);                                Console.WriteLine("1----与对方交换位置,2----轰炸对方");                                int select = UserSelect();                                if (select == 1)                                {                                    Console.WriteLine("{0}选择与{1}交换位置", name[playerNum], name[1 - playerNum]);                                    int temp = playerPos[playerNum];                                    playerPos[playerNum] = playerPos[1 - playerNum];                                    playerPos[1 - playerNum] = temp;                                }                                else                                {                                    Console.WriteLine("{0}选择轰炸{1}", name[playerNum], name[1 - playerNum]);                                    playerPos[1 - playerNum] -= 5;                                    CheckPos();                                }                                break;                            case 2:                                Console.WriteLine("悲催了,{0}踩到地雷了,后退3步", name[playerNum]);                                playerPos[playerNum] -= 3;                                CheckPos();                                break;                            case 3:                                Console.WriteLine("{0}碰到暂停,休息一把再来吧", name[playerNum]);                                isStop[playerNum] = true;                                break;                            case 4:                                Console.WriteLine("{0}到达时空隧道,爽死了,前进十步", name[playerNum]);                                playerPos[playerNum] += 10;                                CheckPos();                                break;                        }                    }                    Console.ReadKey(true);                }                Console.WriteLine("开始行动吧(按任意键)");                Console.ReadKey(true);                Console.Clear();                Console.WriteLine("图例: ◎幸运轮盘      ●地雷      ◆暂停      〓时空隧道");                Map();                Console.ResetColor();                Console.WriteLine("{0}当前位置为:{1}", name[playerNum], playerPos[playerNum]);                Console.WriteLine("{0}当前位置为:{1}", name[1 - playerNum], playerPos[1 - playerNum]);                Console.ReadKey(true);            }            else            {                isStop[playerNum] = false;            }        }    }}
0 0
原创粉丝点击