从网上看着老师一步步的讲解,最终写出了飞行棋的代码。在过程中有了解了C#与java c++的类似
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//不断刷新,
namespace 飞行棋
{class Program{public static int[] Map = new int[100];//大部分函数都要使用的时候可以定义为全局变量//默认全都为0//玩家a与玩家b的做标public static int[] Player = new int[2];//存储两个玩家的姓名public static string[] Playernames=new string[2];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[0] == Playernames[1]){if (Playernames[1] == ""){Console.WriteLine("玩家B的姓名不能为空,请重新输入");Playernames[1] = Console.ReadLine();}else {Console.WriteLine("玩家B的姓名不能和玩家A的相同,请重新输入");Playernames[1] = Console.ReadLine();}}#endregion//首先应该清屏Console.Clear();GameShow();Console.WriteLine("{0}的士兵用A表示", Playernames[0]);Console.WriteLine("{0}的士兵用A表示", Playernames[1]);//注意都是从0开始的哈//在画地图前要是先初始化地图InitialMap();DrawMap();//当玩家都不在终点时#region 玩游戏的过程while (Player[0] < 99 && Player[1] < 99){PlayGame(0);PlayGame(1);}Console.ReadKey();#endregion}#region 初始化public static void GameShow() {//游戏头Console.BackgroundColor = ConsoleColor.Black;//设置背景颜色Console.ForegroundColor = ConsoleColor.Green;//前景颜色,枚举类型Console.WriteLine("****************");Console.WriteLine("****************");Console.WriteLine("**基础班飞行棋**");Console.WriteLine("****************");Console.WriteLine("****************");}//初始化地图,将数组里的数字变成字符串的过程public static void InitialMap() {int[] luckturn = { 6, 23, 40, 55, 69, 83 };for (int i = 0; i < luckturn.Length; i++) {int n = luckturn[i];Map[n] = 1;//直接调用静态变量//Map[lunkturn[i]]=1;}int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };for (int i = 0; i < landmine.Length; i++){int n = landmine[i];Map[n] = 2;//直接调用静态变量}int[] pause = { 9, 27, 60, 93 };for (int i = 0; i < pause.Length; i++){int n = pause[i];Map[n] = 3;//直接调用静态变量}int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };for (int i = 0; i < timeTunnel.Length; i++){int n = timeTunnel[i];Map[n] = 4;//直接调用静态变量}}#endregionpublic static void DrawMap() {//第一横行,要有限制Console.WriteLine("图例:幸运轮盘 地雷 暂停 时空隧道");#region 第一横行for (int i = 0; i < 30; i++) {Console.Write( DrawStringMap(i));}#endregionConsole.WriteLine();//第一竖行,writerline不可以满足这件事for循环的嵌套#region 第一竖行for (int i = 30; i < 35; i++) {//应该换行for (int j = 0; j <29; j++) {Console.Write(" ");}Console.Write(DrawStringMap(i));Console.WriteLine();}#endregion#region 第二横行for (int i = 64; i >= 35; i--) {Console.Write(DrawStringMap(i));}#endregion//应该换行Console.WriteLine();#region 第二竖行for (int i = 65; i < 70; i++) {Console.WriteLine(DrawStringMap(i));//}#endregionfor (int i = 70; i <= 99; i++) {Console.Write(DrawStringMap(i));}Console.WriteLine();}//选择全角不要忘记public static string DrawStringMap(int i){//需要当前地图的坐标作为参数string str = "";if (Player[0] == Player[1] && Player[1] == i)//相同并且在一个地图上,开始默认为零{//方法里尽量不要输出Console.ForegroundColor = ConsoleColor.Red;str="+";//换行}else if (Player[0] == i)//就是A在i位置上{//shift+空格Console.ForegroundColor = ConsoleColor.Yellow;str = "A";}else if (Player[1] == i){Console.ForegroundColor = ConsoleColor.DarkGray;str = "B";}else{switch (Map[i]){case 0:Console.ForegroundColor = ConsoleColor.Red;str = "@";break;case 1:Console.ForegroundColor = ConsoleColor.Green;str = "#";break;case 2:Console.ForegroundColor = ConsoleColor.Blue;str = "*";break;case 3:Console.ForegroundColor = ConsoleColor.White;str = "&";break;case 4:str = "%";break;}}return str;}//条件是两个都没有到终点public static void PlayGame(int number) {Random r = new Random();int r1 = r.Next(1, 7);Console.WriteLine("{0}按任意键开始掷骰子", Playernames[number]);Console.ReadKey(true);//怎样按任意键之后不显示?用trueConsole.WriteLine("{0}掷出了{1}", Playernames[number],r1);Player[0] += r1;ChangePose();Console.ReadKey(true);Console.WriteLine("按任意键开始行动");Console.ReadKey(true);if (Player[number] == Player[1- number]){//玩家1踩到玩家2退六格Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退六格", Playernames[number], Playernames[1- number], Playernames[1- number]);Player[1] -= 6;ChangePose();Console.ReadKey(true);}else{ //踩到关卡switch (Map[Player[number]]){ //0 1 2 3 4case 0:Console.WriteLine("玩家{0}踩到了方块,安全", Playernames[number]);Console.ReadKey(true);break;case 1:Console.WriteLine("玩家{0}踩到了幸运轮盘 1--交换位置 2--轰炸对方", Playernames[0]);string input = Console.ReadLine();while (true){if (input == "1"){Console.WriteLine("玩家{0}选择与玩家{1}交换位置", Playernames[number], Playernames[1- number]);Console.ReadKey(true);int temp = Player[number];Player[number] = Player[1- number];Player[1- number] = temp;ChangePose();Console.WriteLine("交换完成 按任意键开始继续游戏");Console.ReadKey(true);break;}else if (input == "2"){Console.WriteLine("玩家{0}选择轰炸对方", Playernames[number]);Console.ReadKey(true);Player[1] -= 6;ChangePose();Console.WriteLine("玩家{0}退了六格", Playernames[1- number]);Console.ReadKey(true);break;}else{Console.WriteLine("只能输入1或者2 1--交换位置 2--轰炸对方");input = Console.ReadLine();}}break;case 2:Console.WriteLine("玩家{0}踩到了地雷退六格", Playernames[number]);Console.ReadKey(true);Player[number] -= 6;ChangePose();break;case 3:Console.WriteLine("玩家{0}踩到了暂停", Playernames[number]);Console.ReadKey(true);break;case 4:Console.WriteLine("玩家{0}踩到了时空隧道前进十个", Playernames[number]);Player[number] += 10;ChangePose();break;}}Console.Clear();//每次都刷新一次DrawMap();}//当玩家坐标改变时public static void ChangePose() {if (Player[0] < 0)Player[0] = 0;if (Player[0] >= 99)Player[0] = 99;if (Player[1] < 0)Player[1] = 0;if (Player[1] >= 99)Player[1] = 99;}}}
本文链接:https://my.lmcjl.com/post/9371.html
展开阅读全文
4 评论