先上個(gè) 方法的小練習(xí)
static void Main(string[] args)
{
//用方法來實(shí)現(xiàn):有一個(gè)字符串?dāng)?shù)組:
//{ "馬龍", "邁克爾喬丹", "雷吉米勒", "蒂姆鄧肯", "科比布萊恩特" },
//請輸出最名字最長的
string[] names = { "馬龍", "邁克爾喬丹", "雷吉米勒", "蒂姆鄧肯", "科比布萊恩特" };
Console.WriteLine(GetLongest(names));
Console.ReadKey();
}
/// <summary>
/// 求一個(gè)字符串?dāng)?shù)組中最長的元素
/// </summary>
/// <param name="arr">字符串?dāng)?shù)組</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;
}
結(jié)果.png
飛行棋游戲
先看看大致的界面
圖片.png
整個(gè)游戲所用到的知識(shí)都是 基礎(chǔ)01-基礎(chǔ)07 的知識(shí),理解起來有困難的可以看看我以前的幾篇文章。
整個(gè)游戲可以分為以下幾個(gè)部分:
1携御、畫游戲頭
2、初始化地圖(加載地圖所需要的資源)
將整數(shù)數(shù)組中的數(shù)字編程控制臺(tái)中顯示的特殊字符串的這個(gè)過程 就是初始化地圖
3、畫地圖
4寇漫、玩游戲
游戲規(guī)則:
如果玩家A踩到了玩家B 玩家B退6格
踩到了地雷 退6格
踩到了時(shí)空隧道 進(jìn)10格
踩到了幸運(yùn)輪盤 1交換位置 2 轟炸對方 使對方退6格
踩到了暫停 暫停一回合
踩到了方塊 神馬都不干
-
畫游戲頭
游戲頭.png
實(shí)現(xiàn)代碼
/// <summary>
/// 游戲頭
/// </summary>
public static void GameShow()
{
//設(shè)置背景色
Console.BackgroundColor = ConsoleColor.Cyan;
//設(shè)置前景色(即文本顏色)
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("**********************************");
}
- 初始化地圖
地圖.png
/// <summary>
/// 初始化地圖
/// </summary>
public static void IntialMap()
{
//我用0表示普通,顯示給用戶就是 □
//....1...幸運(yùn)輪盤,顯示組用戶就◎
//....2...地雷,顯示給用戶就是 ☆
//....3...暫停,顯示給用戶就是 ▲
//....4...時(shí)空隧道,顯示組用戶就 卐
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸運(yùn)輪盤◎
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 };//時(shí)空隧道卐
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;
}
}
- 畫地圖
當(dāng)玩家位置相同時(shí)顯示為尖括號(hào).png
當(dāng)玩家位置不同時(shí)顯示各自的位置.png
地圖坐標(biāo)分析
地圖坐標(biāo)分析.png
1殉摔、畫第一橫行
#region 第一橫行
for (int i = 0; i < 30; i++)
{
//如果玩家A 和 玩家B 的坐標(biāo)相同州胳,并且都在地圖上,畫一個(gè)尖括號(hào)
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
第一橫行效果.png
2逸月、畫第一豎行
#region 畫第一豎行
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)//畫空格
{
Console.Write("**");
}
#region 畫圖
//如果玩家A 和 玩家B 的坐標(biāo)相同栓撞,并且都在地圖上,畫一個(gè)尖括號(hào)
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
這里先以** 代替 空格表示
第一豎行效果.png
3碗硬、這里先封裝下方法
/// <summary>
/// 從畫地圖的方法中抽象出來的一個(gè)方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string DrawStringMap(int i)
{
string str = "";
#region 畫圖
//如果玩家A 和 玩家B 的坐標(biāo)相同瓤湘,并且都在地圖上瓢颅,畫一個(gè)尖括號(hào)
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();
第二橫行效果.png
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
第二豎行效果.png
其實(shí) 這里應(yīng)該滑到 69行比較合適
6惜索、最后一橫行
#region 最后一橫行
for (int i = 70; i < 100; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion
最后一橫行效果.png
- 輸入姓名
#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
效果圖.png
根據(jù)游戲規(guī)則玩游戲
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02飛行棋游戲
{
class Program
{
//用靜態(tài)字段模擬全局變量
static int[] Maps = new int[100];
//存儲(chǔ)玩家A B 坐標(biāo)
static int[] PlayerPos = new int[2];
//存儲(chǔ)玩家的 姓名
static string[] PlayerNames = new string[2];
//兩個(gè)玩家的標(biāo)記
static bool[] Flags = new bool[2];//Flags[0]默認(rèn)是false Flags[1]默認(rèn)也是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
//玩家姓名輸入之后我們首先應(yīng)該清屏
Console.Clear();//清屏
GameShow();
Console.WriteLine("{0}用 A 表示", PlayerNames[0]);
Console.WriteLine("{0}用 B 表示", PlayerNames[1]);
IntialMap();
DrawMap();
//當(dāng)玩家A 和 玩家B 都沒到終點(diǎn)時(shí)隨便玩
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()
{
//設(shè)置背景色
//Console.BackgroundColor = ConsoleColor.Cyan;
//設(shè)置前景色(即文本顏色)
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...幸運(yùn)輪盤,顯示組用戶就◎
//....2...地雷,顯示給用戶就是 ☆
//....3...暫停,顯示給用戶就是 ▲
//....4...時(shí)空隧道,顯示組用戶就 卐
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸運(yùn)輪盤◎
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 };//時(shí)空隧道卐
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("圖例:幸運(yùn)轉(zhuǎn)輪:◎ 地雷:☆ 暫停:▲ 時(shí)空隧道:卐");
#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>
/// 從畫地圖的方法中抽象出來的一個(gè)方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string DrawStringMap(int i)
{
string str = "";
#region 畫圖
//如果玩家A 和 玩家B 的坐標(biāo)相同淘讥,并且都在地圖上圃伶,畫一個(gè)尖括號(hào)
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}擲出了{(lán)1}", PlayerNames[playerNumber], step);
PlayerPos[playerNumber] += step;
ChangePos();
Console.WriteLine("玩家{0}按任意鍵開始行動(dòng)", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("玩家{0}行動(dòng)完了", PlayerNames[playerNumber]);
Console.ReadKey(true);
//玩家A 可能踩到玩家B 方塊 幸運(yùn)輪盤 地雷 暫停 時(shí)空隧道
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//踩到關(guān)卡
{
//玩家的坐標(biāo)
switch (Maps[PlayerPos[playerNumber]])
{
case 0:
Console.WriteLine("玩家{0}踩到了方塊,安全", PlayerNames[playerNumber]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到了幸運(yùn)輪盤蒲列,請選擇 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("交換完成V吓蟆!蝗岖!按任意鍵繼續(xù)游戲=男伞!抵赢!");
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}踩到時(shí)空隧道,前進(jìn)10格", PlayerNames[playerNumber]);
PlayerPos[playerNumber] += 10;
ChangePos();
Console.ReadKey(true);
break;
default:
break;
}
}
Console.Clear();
DrawMap();
}
/// <summary>
/// 當(dāng)玩家坐標(biāo)發(fā)生改變時(shí) 保證坐標(biāo)在地圖上
/// </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();
}
}
}
最終運(yùn)行結(jié)果.png
往期回顧
圖文無關(guān).png