先上个 方法的小练习
static void Main(string[] args)
{
//用方法来实现:有一个字符串数组:
//{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },
//请输出最名字最长的
string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
Console.WriteLine(GetLongest(names));
Console.ReadKey();
}
/// <summary>
/// 求一个字符串数组中最长的元素
/// </summary>
/// <param name="arr">字符串数组</param>
/// <returns>最长的元素</returns>
public static string GetLongest(string[] arr)
{
string max = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (max.Length < arr[i].Length)
{
max = arr[i];
}
}
return max;
}
飞行棋游戏
先看看大致的界面
整个游戏所用到的知识都是 基础01-基础07 的知识,理解起来有困难的可以看看我以前的几篇文章。
整个游戏可以分为以下几个部分:
1、画游戏头
2、初始化地图(加载地图所需要的资源)
将整数数组中的数字编程控制台中显示的特殊字符串的这个过程 就是初始化地图
3、画地图
4、玩游戏
游戏规则:
如果玩家A踩到了玩家B 玩家B退6格
踩到了地雷 退6格
踩到了时空隧道 进10格
踩到了幸运轮盘 1交换位置 2 轰炸对方 使对方退6格
踩到了暂停 暂停一回合
踩到了方块 神马都不干
-
画游戏头
实现代码
/// <summary>
/// 游戏头
/// </summary>
public static void GameShow()
{
//设置背景色
Console.BackgroundColor = ConsoleColor.Cyan;
//设置前景色(即文本颜色)
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("************飞行棋游戏************");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("**********************************");
}
- 初始化地图
/// <summary>
/// 初始化地图
/// </summary>
public static void IntialMap()
{
//我用0表示普通,显示给用户就是 □
//....1...幸运轮盘,显示组用户就◎
//....2...地雷,显示给用户就是 ☆
//....3...暂停,显示给用户就是 ▲
//....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[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
for (int j = 0; j < luckyturn.Length; j++)
{
int index = luckyturn[j];
Maps[index] = 1;
}
for (int k = 0; k < landMine.Length; k++)
{
int index = landMine[k];
Maps[index] = 2;
}
for (int l = 0; l < pause.Length; l++)
{
int index = pause[l];
Maps[index] = 3;
}
for (int m = 0; m < timeTunnel.Length; m++)
{
int index = timeTunnel[m];
Maps[index] = 4;
}
}
- 画地图
地图坐标分析
1、画第一横行
#region 第一横行
for (int i = 0; i < 30; i++)
{
//如果玩家A 和 玩家B 的坐标相同,并且都在地图上,画一个尖括号
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
{
Console.Write("<>");
}
else if (PlayerPos[0] == i)
{
Console.Write("A");
}
else if (PlayerPos[1] == i)
{
Console.Write("B");
}
else {
switch (Maps[i])
{
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.Magenta;
Console.Write("卐");
break;
default:
break;
}
}
}
#endregion
2、画第一竖行
#region 画第一竖行
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)//画空格
{
Console.Write("**");
}
#region 画图
//如果玩家A 和 玩家B 的坐标相同,并且都在地图上,画一个尖括号
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
{
Console.Write("<>");
}
else if (PlayerPos[0] == i)
{
Console.Write("A");
}
else if (PlayerPos[1] == i)
{
Console.Write("B");
}
else
{
switch (Maps[i])
{
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.Magenta;
Console.Write("卐");
break;
default:
break;
}
}
#endregion
//画完换行
Console.WriteLine();
}
#endregion
这里先以** 代替 空格表示
3、这里先封装下方法
/// <summary>
/// 从画地图的方法中抽象出来的一个方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string DrawStringMap(int i)
{
string str = "";
#region 画图
//如果玩家A 和 玩家B 的坐标相同,并且都在地图上,画一个尖括号
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
{
str = "<>";
}
else if (PlayerPos[0] == i)
{
str = "A";
}
else if (PlayerPos[1] == i)
{
str = "B";
}
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
str = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
str = "卐";
break;
default:
break;
}
}
return str;
#endregion
}
4、画第二横行
#region 第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
#endregion
Console.WriteLine();
5、画第二竖行
#region 第二竖行
for (int i = 65; i < 71; i++)
{
Console.Write(DrawStringMap(i));
for (int j = 1; j <= 29; j++)
{
Console.Write("**");
}
Console.WriteLine();
}
#endregion
其实 这里应该滑到 69行比较合适
6、最后一横行
#region 最后一横行
for (int i = 70; i < 100; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion
- 输入姓名
#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姓名不能为空,请重新输入");
}
else
{
Console.WriteLine("玩家A 和玩家B 不能同名,请重新输入");
}
Console.WriteLine("请输入玩家B的姓名");
PlayerNames[1] = Console.ReadLine();
}
#endregion
根据游戏规则玩游戏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02飞行棋游戏
{
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]默认是false Flags[1]默认也是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[0] == PlayerNames[1])
{
if (PlayerNames[1] == "")
{
Console.WriteLine("玩家B姓名不能为空,请重新输入");
}
else
{
Console.WriteLine("玩家A 和玩家B 不能同名,请重新输入");
}
Console.WriteLine("请输入玩家B的姓名");
PlayerNames[1] = Console.ReadLine();
}
#endregion
//玩家姓名输入之后我们首先应该清屏
Console.Clear();//清屏
GameShow();
Console.WriteLine("{0}用 A 表示", PlayerNames[0]);
Console.WriteLine("{0}用 B 表示", PlayerNames[1]);
IntialMap();
DrawMap();
//当玩家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;
}
}
Win();
Console.ReadKey();
Console.ReadKey();
}
/// <summary>
/// 游戏头
/// </summary>
public static void GameShow()
{
//设置背景色
//Console.BackgroundColor = ConsoleColor.Cyan;
//设置前景色(即文本颜色)
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("************飞行棋游戏************");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("**********************************");
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// 初始化地图
/// </summary>
public static void IntialMap()
{
//我用0表示普通,显示给用户就是 □
//....1...幸运轮盘,显示组用户就◎
//....2...地雷,显示给用户就是 ☆
//....3...暂停,显示给用户就是 ▲
//....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[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
for (int j = 0; j < luckyturn.Length; j++)
{
int index = luckyturn[j];
Maps[index] = 1;
}
for (int k = 0; k < landMine.Length; k++)
{
int index = landMine[k];
Maps[index] = 2;
}
for (int l = 0; l < pause.Length; l++)
{
int index = pause[l];
Maps[index] = 3;
}
for (int m = 0; m < timeTunnel.Length; m++)
{
int index = timeTunnel[m];
Maps[index] = 4;
}
}
/// <summary>
/// 画地图
/// </summary>
public static void DrawMap()
{
Console.WriteLine("图例:幸运转轮:◎ 地雷:☆ 暂停:▲ 时空隧道:卐");
#region 第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(DrawStringMap(i));
}
#endregion
//画完换行
Console.WriteLine();
#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.Write(DrawStringMap(i));
for (int j = 1; j <= 29; j++)
{
Console.Write(" ");
}
Console.WriteLine();
}
#endregion
#region 最后一横行
for (int i = 70; i < 100; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion
}
/// <summary>
/// 从画地图的方法中抽象出来的一个方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string DrawStringMap(int i)
{
string str = "";
#region 画图
//如果玩家A 和 玩家B 的坐标相同,并且都在地图上,画一个尖括号
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
{
str = "<>";
}
else if (PlayerPos[0] == i)
{
str = "A";
}
else if (PlayerPos[1] == i)
{
str = "B";
}
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
str = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
str = "卐";
break;
default:
break;
}
}
return str;
#endregion
}
/// <summary>
/// 玩游戏
/// </summary>
public static void PlayGame(int playerNumber)
{
Random r = new Random();
int step = r.Next(1, 7);
Console.WriteLine("玩家{0}按任意键开始投骰子", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}掷出了{1}", PlayerNames[playerNumber], step);
PlayerPos[playerNumber] += step;
ChangePos();
Console.WriteLine("玩家{0}按任意键开始行动", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("玩家{0}行动完了", PlayerNames[playerNumber]);
Console.ReadKey(true);
//玩家A 可能踩到玩家B 方块 幸运轮盘 地雷 暂停 时空隧道
if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
{
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]);
PlayerPos[1 - playerNumber] -= 6;
ChangePos();
Console.ReadKey(true);
}
else//踩到关卡
{
//玩家的坐标
switch (Maps[PlayerPos[playerNumber]])
{
case 0:
Console.WriteLine("玩家{0}踩到了方块,安全", PlayerNames[playerNumber]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择 1--交换位置 2--轰炸对方", PlayerNames[playerNumber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0} 选择和 玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
int temp = PlayerPos[playerNumber];
PlayerPos[playerNumber] = PlayerPos[1 - playerNumber];
PlayerPos[1 - playerNumber] = temp;
Console.WriteLine("交换完成!!!按任意键继续游戏!!!");
Console.ReadKey(true);
}
else if (input == "2")
{
Console.WriteLine("玩家{0} 选择轰炸 玩家{1}, 玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]);
Console.ReadKey(true);
PlayerPos[1 - playerNumber] -= 6;
ChangePos();
Console.WriteLine("玩家{1}退6格", PlayerNames[1 - playerNumber]);
Console.ReadKey(true);
}
else
{
Console.WriteLine("输入有误,请重新输入 只能输入 1--交换位置 2--轰炸对方");
Console.ReadKey(true);
input = Console.ReadLine();
}
break;
}
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();
Console.ReadKey(true);
break;
default:
break;
}
}
Console.Clear();
DrawMap();
}
/// <summary>
/// 当玩家坐标发生改变时 保证坐标在地图上
/// </summary>
public static void ChangePos()
{
if (PlayerPos[0] < 0)
{
PlayerPos[0] = 0;
}
if (PlayerPos[0] > 99)
{
PlayerPos[0] = 99;
}
if (PlayerPos[1] < 0)
{
PlayerPos[1] = 0;
}
if (PlayerPos[1] > 99)
{
PlayerPos[1] = 99;
}
}
/// <summary>
/// 胜利
/// </summary>
public static void Win()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" ◆ ");
Console.WriteLine(" ■ ◆ ■ ■");
Console.WriteLine(" ■■■■ ■ ■ ◆■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ◆ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■■■■■■ ■■■■■■■ ■ ■ ■");
Console.WriteLine(" ■■■■ ■ ■ ●■● ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ● ■ ● ■ ■ ■");
Console.WriteLine(" ■ ■ ■■■■■■ ● ■ ● ■ ■ ■");
Console.WriteLine(" ■■■■ ■ ● ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■ ■ ");
Console.WriteLine(" ■ ■ ■ ■ ● ■ ");
Console.WriteLine(" ■ ■■ ■■■■■■ ■ ● ●");
Console.ResetColor();
}
}
}
往期回顾