初学c语言必看书(C学习笔记6新人必看)

C#是当前在.Net开发环境下一种十分易于使用的语言,这门语言上手容易,语法简单,那么接下来就展示一些学习这门语言中的笔记吧本合集将要要学习的内容提炼为11天,本篇是第六天的内容,主要是一个WinForm小游戏的实例,11天成为C#程序员,就是这么简单,今天小编就来聊一聊关于初学c语言必看书?接下来我们就一起去研究一下吧!

初学c语言必看书(C学习笔记6新人必看)

初学c语言必看书

C#是当前在.Net开发环境下一种十分易于使用的语言,这门语言上手容易,语法简单,那么接下来就展示一些学习这门语言中的笔记吧!本合集将要要学习的内容提炼为11天,本篇是第六天的内容,主要是一个WinForm小游戏的实例,11天成为C#程序员,就是这么简单!

6 .Net学习第六天

6.1 飞行棋

头文件以及全局变量

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test16 { class Program { #region 静态变量 //用静态变量模拟全局变量 public static int[] Maps = new int[100]; public static int PlayerAPos = 0; public static int PlayerBPos = 0; public static string PlayerAName = "A"; public static string PlayerBName = "B"; public static bool PlayerMoveFlag = true; public static bool PauseA = false; public static bool PauseB = false; #endregion

6.2 飞行棋的主方法

/// <summary> /// 主方法,用于进入整个飞行棋程序 /// </summary> /// <param name="args"></param> static void Main(string[] args) { ShowGameStart(); GetName(); Console.Clear(); Console.WriteLine("{0}的棋子用A表示", PlayerAName); Console.WriteLine("{0}的棋子用B表示", PlayerBName); InitializeMap(); DrawMap(); while (PlayerAPos < 99 && PlayerBPos < 99) { PlayGame(); } GameOver(); Console.ReadKey(true); }

6.3 飞行棋游戏头

/// <summary> /// 游戏头 /// </summary> public static void ShowGameStart() { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("***************飞行棋游戏***************"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("****************************************"); Console.ForegroundColor = ConsoleColor.White; }

6.4 飞行棋获取玩家名字

/// <summary> /// 主方法中调用的获取名字的方法 /// </summary> public static void GetName() { GetName(ref PlayerAName); GetName(ref PlayerBName); if (PlayerBName == PlayerAName) { Console.WriteLine("两位玩家名字相同,请重新输入两位玩家的名字"); PlayerAName = "A"; PlayerBName = "B"; GetName(); } }

6.5 飞行棋获取名字

/// <summary> /// 获取玩家的名字 /// </summary> /// <param name="name">用于存储名字的字符串</param> public static void GetName(ref string name) { string inName = name; do { Console.WriteLine("请输入玩家{0}的姓名", inName); name = Console.ReadLine(); } while (name == ""); }

6.6 初始化地图数组

/// <summary> /// 初始化地图数组 /// </summary> public static void InitializeMap() { //普通方块为0,无特殊事件 //幸运轮盘为1,按1选择交换位置,按2选择对方后退6格 //地雷方块为2,后退6格 //时空隧道为3,前进10格 //暂停方块为4,下一轮机会跳过 int[] luckyTurn = { 6, 23, 40, 55, 69, 83 }; int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; int[] pause = { 9, 27, 60, 93 }; int[] timeTennel = { 20, 25, 45, 63, 72, 88, 90 }; for (int i = 0; i < luckyTurn.Length; i ) { Maps[luckyTurn[i]] = 1; } for (int i = 0; i < landMine.Length; i ) { Maps[landMine[i]] = 2; } for (int i = 0; i < pause.Length; i ) { Maps[pause[i]] = 3; } for (int i = 0; i < timeTennel.Length; i ) { Maps[timeTennel[i]] = 4; } }

6.7 画地图元素的样式

/// <summary> /// 画地图元素的样式 /// </summary> /// <param name="mark">对应地图元素样式的标识数字</param> public static void DrawMark(int mark, int location) { if (PlayerAPos == location && PlayerBPos == location) { Console.ForegroundColor = ConsoleColor.White; Console.Write("<>"); } else if (PlayerAPos == location) { Console.ForegroundColor = ConsoleColor.White; Console.Write(" A"); } else if (PlayerBPos == location) { Console.ForegroundColor = ConsoleColor.White; Console.Write(" B"); } else { switch (mark) { case 0: Console.ForegroundColor = ConsoleColor.White; Console.Write("□"); break; case 1: Console.ForegroundColor = ConsoleColor.Red; Console.Write("◎"); break; case 2: Console.ForegroundColor = ConsoleColor.Green; Console.Write("☆"); break; case 3: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("△"); break; case 4: Console.ForegroundColor = ConsoleColor.White; Console.Write("卐"); break; default: break; } } }

6.8 画地图的方法

/// <summary> /// 画地图的方法 /// </summary> public static void DrawMap() { Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:△ 时空隧道:卐 "); #region 第一横行 for (int i = 0; i < 30; i ) { DrawMark(Maps[i], i); } Console.WriteLine(); #endregion #region 第一竖列 for (int i = 30; i < 35; i ) { for (int j = 0; j < 29; j ) { Console.Write(" "); } DrawMark(Maps[i], i); Console.WriteLine(); } #endregion #region 第二横行 for (int i = 64; i > 34; i--) { DrawMark(Maps[i], i); } Console.WriteLine(); #endregion #region 第二竖列 for (int i = 65; i < 70; i ) { DrawMark(Maps[i], i); Console.WriteLine(); } #endregion #region 第三横行 for (int i = 70; i < 100; i ) { DrawMark(Maps[i], i); } Console.WriteLine(); #endregion }

6.9 游戏结束判断胜利方

/// <summary> /// 游戏结束 /// </summary> public static void GameOver() { Console.Clear(); if (PlayerAPos >= 99) { Console.WriteLine("玩家{0}赢得了游戏胜利", PlayerAName); } else { Console.WriteLine("玩家{0}赢得了游戏胜利", PlayerBName); } }

6.10 判断棋子移动后的状态

/// <summary> /// 判断棋子移动后的状态 /// </summary> /// <param name="name">移动的棋子</param> public static void JudgePos(string name) { if (PlayerAPos >= 99 || PlayerBPos >= 99) { return; } if (name==PlayerAName) { if (PlayerAPos == PlayerBPos) { Console.WriteLine("玩家{0}的棋子踩到了玩家{1}的棋子,玩家{2}退6格", PlayerAName, PlayerBName, PlayerBName); PlayerBPos -= 6; Console.ReadKey(true); } else { switch (Maps[PlayerAPos]) { #region 普通方块 case 0: Console.WriteLine("玩家{0}踩到了普通方块,安全", PlayerAName); Console.ReadKey(true); break; #endregion #region 幸运轮盘 case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择 1——交换位置 2——轰炸对方", PlayerAName); while (true) { string input = Console.ReadLine(); if (input == "1") { Console.WriteLine("玩家{0}选择和玩家{1}交换位置", PlayerAName, PlayerBName); PlayerAPos = PlayerAPos - PlayerBPos; PlayerBPos = PlayerAPos PlayerBPos; PlayerAPos = PlayerBPos - PlayerAPos; Console.WriteLine("交换完成,按任意键继续"); Console.ReadKey(true); break; } else if (input == "2") { Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerAName, PlayerBName, PlayerBName); PlayerBPos -= 6; Console.WriteLine("玩家{0}退了6格,按任意键继续", PlayerBName); Console.ReadKey(true); break; } else { Console.WriteLine("只能输入1或者2 1——交换位置 2——轰炸对方"); } } break; #endregion #region 地雷 case 2: Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerAName); PlayerAPos -= 6; Console.WriteLine("后退完毕,按任意键继续"); Console.ReadKey(true); break; #endregion #region 暂停 case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合不能行动", PlayerAName); PauseA = true; Console.ReadKey(true); break; #endregion #region 时空隧道 case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerAName); PlayerAPos = 10; Console.WriteLine("前进完毕,按任意键继续"); Console.ReadKey(true); break; #endregion default: break; } } if (PlayerAPos < 0) { PlayerAPos = 0; } if (PlayerBPos < 0) { PlayerBPos = 0; } } else { if (PlayerAPos == PlayerBPos) { Console.WriteLine("玩家{0}的棋子踩到了玩家{1}的棋子,玩家{2}退6格", PlayerBName, PlayerAName, PlayerAName); PlayerAPos -= 6; Console.ReadKey(true); } else { switch (Maps[PlayerBPos]) { #region 普通方块 case 0: Console.WriteLine("玩家{0}踩到了普通方块,安全", PlayerBName); Console.ReadKey(true); break; #endregion #region 幸运轮盘 case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择 1——交换位置 2——轰炸对方", PlayerBName); while (true) { string input = Console.ReadLine(); if (input == "1") { Console.WriteLine("玩家{0}选择和玩家{1}交换位置", PlayerBName, PlayerAName); PlayerAPos = PlayerAPos - PlayerBPos; PlayerBPos = PlayerAPos PlayerBPos; PlayerAPos = PlayerBPos - PlayerAPos; Console.WriteLine("交换完成,按任意键继续"); Console.ReadKey(true); break; } else if (input == "2") { Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerBName, PlayerAName, PlayerAName); PlayerAPos -= 6; Console.WriteLine("玩家{0}退了6格,按任意键继续", PlayerAName); Console.ReadKey(true); break; } else { Console.WriteLine("只能输入1或者2 1——交换位置 2——轰炸对方"); } } break; #endregion #region 地雷 case 2: Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerBName); PlayerBPos -= 6; Console.WriteLine("后退完毕,按任意键继续"); Console.ReadKey(true); break; #endregion #region 暂停 case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合不能行动", PlayerBName); PauseB = true; Console.ReadKey(true); break; #endregion #region 时空隧道 case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerBName); PlayerBPos = 10; Console.WriteLine("前进完毕,按任意键继续"); Console.ReadKey(true); break; #endregion default: break; } } if (PlayerAPos < 0) { PlayerAPos = 0; } if (PlayerBPos < 0) { PlayerBPos = 0; } } }

6.11 移动玩家棋子的方法

/// <summary> /// 移动玩家棋子位置的方法 /// </summary> /// <param name="name">要移动棋子的玩家</param> /// <param name="pos">要移动的位置</param> public static void PlayerMove(string name, int pos) { if (name == PlayerAName) { PlayerAPos = pos; } else { PlayerBPos = pos; } }

6.12 游戏过程执行方法

/// <summary> /// 游戏过程执行方法 /// </summary> public static void PlayGame() { if (PlayerMoveFlag) { Console.WriteLine("玩家{0}按任意键开始掷骰子", PlayerAName); Console.ReadKey(true); Random r = new Random(); int rNum = r.Next(1, 7); Console.WriteLine("玩家{0}掷出了{1}", PlayerAName, rNum); PlayerMove(PlayerAName, rNum); JudgePos(PlayerAName); Console.WriteLine("玩家{0}行动完了,按任意键继续", PlayerAName); if (PauseB) { PlayerMoveFlag = !PlayerMoveFlag; PauseB = false; } PlayerMoveFlag = !PlayerMoveFlag; Console.ReadKey(true); } else { Console.WriteLine("玩家{0}按任意键开始掷骰子", PlayerBName); Console.ReadKey(true); Random r = new Random(); int rNum = r.Next(1, 7); Console.WriteLine("玩家{0}掷出了{1}", PlayerBName, rNum); PlayerMove(PlayerBName, rNum); Console.WriteLine("玩家{0}按任意键开始行动", PlayerBName); JudgePos(PlayerBName); Console.WriteLine("玩家{0}行动完了,按任意键继续", PlayerBName); if (PauseA) { PlayerMoveFlag = !PlayerMoveFlag; PauseA = false; } PlayerMoveFlag = !PlayerMoveFlag; Console.ReadKey(true); } Console.Clear(); DrawMap(); }

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页