C#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡(jiǎn)單飛行棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo):實(shí)現(xiàn)飛行棋游戲基礎(chǔ)功能
玩家在地圖觸發(fā)道具:
1、獲得道具,可以進(jìn)行一次選擇 1–交換位置 2–讓對(duì)方退隨機(jī)格子
2、踩到炸彈,讓對(duì)方暫停一回合
3、乘上了飛機(jī),前進(jìn)10格
4、進(jìn)入隧道,將隨機(jī)從其他隧道口出來




using System;
namespace FXQGame
{
class Program
{
//儲(chǔ)存地圖數(shù)組
static int[] mMaps = new int[120];
//儲(chǔ)存兩個(gè)玩家的坐標(biāo)
static int[] mPlayerPos = new int[2];
static string[] mPlayerName = { "玩家一","玩家二" };
//下標(biāo)控制當(dāng)前玩家
static int index;
static void Main(string[] args)
{
//游戲頭 展示信息
GameTitle();
Console.WriteLine(mPlayerName[0]);
Console.WriteLine(mPlayerName[1]);
Console.WriteLine("游戲玩法:");
Console.WriteLine("輸入任意鍵開始游戲");
//按下任意鍵進(jìn)入下一步驟
Console.ReadKey();
//清屏
Console.Clear();
//初始化地圖
InitGameMap();
//繪制地圖
DrawMap();
//進(jìn)入游戲
Update();
//游戲結(jié)束
if (mPlayerPos[0] > mPlayerPos[1])
{
Console.WriteLine("{0}獲得勝利",mPlayerName[0]);
}
else
{
Console.WriteLine("{0}獲得勝利", mPlayerName[1]);
}
Console.ReadKey();
}
//游戲邏輯
private static void Update()
{
index = 0;
do
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("{0}按任意鍵開始投擲骰子", mPlayerName[index % 2]);
Console.ReadKey(true);
int ran = GetRanden(1, 7);
Console.WriteLine("{0}擲出{1}點(diǎn)", mPlayerName[index % 2], ran);
mPlayerPos[index % 2] += ran;
Console.ReadKey(true);
Console.WriteLine("{0}開始行動(dòng)", mPlayerName[index % 2]);
//玩家A與玩家B接觸
if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] != 0)
{
Console.WriteLine("{0}走到了{(lán)1}的背后進(jìn)行攻擊,{2}后退3格", mPlayerName[index % 2], mPlayerName[(index + 1) % 2], mPlayerName[(index + 1) % 2]);
if (mPlayerPos[(index + 1) % 2] - 3 < 0)
{
mPlayerPos[(index + 1) % 2] = 0;
}
else
{
mPlayerPos[(index + 1) % 2] -= 3;
}
Console.ReadKey(true);
}
else//玩家在地圖觸發(fā)道具
{
switch (mMaps[mPlayerPos[index % 2]])
{
case 0:
Console.WriteLine("{0}踩到方塊,安全", mPlayerName[index % 2]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("{0}獲得道具,可以進(jìn)行一次選擇 1--交換位置 2--讓對(duì)方退隨機(jī)格子", mPlayerName[index % 2]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}選擇跟玩家{1}交換位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]);
int temp = mPlayerPos[0];
mPlayerPos[0] = mPlayerPos[1];
mPlayerPos[1] = temp;
Console.WriteLine("交換完畢,請(qǐng)按任意鍵進(jìn)行游戲");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}選擇讓玩家{1}退回隨機(jī)位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]);
int r = GetRanden(1, 7);
if (mPlayerPos[(index + 1) % 2] - r < 0)
{
mPlayerPos[(index + 1) % 2] = 0;
}
else
{
mPlayerPos[(index + 1) % 2] -= r;
}
Console.WriteLine("玩家{0}本回合得退回{1}位置,按任意鍵進(jìn)行游戲", mPlayerName[(index + 1) % 2], r);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("輸入無效字符,請(qǐng)重新輸入");
input = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("{0}踩到炸彈,讓對(duì)方暫停一回合", mPlayerName[index % 2]);
index++;
Console.ReadKey(true);
break;
case 3:
Console.WriteLine("{0}乘上了飛機(jī),前進(jìn)10格", mPlayerName[index % 2]);
mPlayerPos[index % 2] += 10;
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("{0}進(jìn)入隧道,將隨機(jī)從其他隧道口出來", mPlayerName[index % 2]);
int[] tunnel = { 20, 25, 45, 63, 72, 88, 90 };
mPlayerPos[index % 2] = tunnel[GetRanden(0, 7)];
Console.WriteLine("{0}從其他隧道口出來", mPlayerName[index % 2], mPlayerPos[index % 2]);
Console.ReadKey(true);
break;
}
}
index++;
Console.Clear();
DrawMap();
}while (mPlayerPos[0] < 99 && mPlayerPos[1] < 99);
//當(dāng)某一位玩家到達(dá)終點(diǎn)時(shí),結(jié)束游戲
}
//得到隨機(jī)數(shù)
private static int GetRanden(int min,int max)
{
Random random = new Random();
return random.Next(min, max);
}
//游戲頭介紹
private static void GameTitle()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(DateTime.Now);
Console.WriteLine("飛行棋雙人小游戲");
Console.WriteLine("************************************");
}
//初始化游戲地圖
private static void InitGameMap()
{
//玩家道具 在數(shù)組中存儲(chǔ)為1 可以讓敵人退后隨機(jī)距離 也可以交換雙方位置
int[] planeItem = { 6,23,40,55,69,83 };
for(int i = 0; i< planeItem.Length; i++)
{
mMaps[planeItem[i]] = 1;
}
//炸彈道具 在數(shù)組中存儲(chǔ)為2 暫停一回合
int[] bombItem = {5,13,17,33,38,50,64,80,94 };
for (int i = 0; i < bombItem.Length; i++)
{
mMaps[bombItem[i]] = 2;
}
//飛機(jī)道具 在數(shù)組中存儲(chǔ)為3 前進(jìn)10格
int[] propsItem = {9,27,60,93 };
for (int i = 0; i < propsItem.Length; i++)
{
mMaps[propsItem[i]] = 3;
}
//隧道 在數(shù)組中存儲(chǔ)為4 從其他隨機(jī)隧道鉆出
int[] tunnelItem = {20,25,45,63,72,88,90 };
for (int i = 0; i < tunnelItem.Length; i++)
{
mMaps[tunnelItem[i]] = 4;
}
}
//繪制地圖
private static void DrawMap()
{
Console.WriteLine("玩家一用A表示,玩家二用B表示");
Console.WriteLine("圖例:□普通方塊 ◇道具 ●炸彈 $飛機(jī) ¤隧道");
//第一行
for (int i = 0; i < 30; i++)
{
Draw(i);
}
//第一豎行
Console.WriteLine();
for (int i = 30; i < 35; i++)
{
for (int j = 0; j <= 28; j++)
{//兩個(gè)空格
Console.Write(" ");
}
Draw(i);
Console.WriteLine();
}
//第二行
for (int i = 64; i >= 35; i--)
{
Draw(i);
}
Console.WriteLine();
//第二豎行
for(int i = 65; i <= 69; i++)
{
Draw(i);
Console.WriteLine();
}
//第三行
for (int i = 70; i <= 99; i++)
{
Draw(i);
}
Console.WriteLine();
}
//判斷當(dāng)前點(diǎn)屬于什么特殊屬性 并繪制出來
private static void Draw(int i)
{
//當(dāng)兩人重合顯示
if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] == i)
{
Console.Write("<>");
}
else if (mPlayerPos[0] == i)
{//玩家一顯示A
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("A");
}
else if (mPlayerPos[1] == i)
{//玩家二顯示B
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("B");
}
else
{
switch (mMaps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("□");
break;
case 1:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("◇");
break;
case 2:
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("●");
break;
case 3:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("$");
break;
case 4:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("¤");
break;
}
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)
這篇文章主要介紹了C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)在C#中,@符號(hào)不僅可以加在字符串常量之前,使字符串不作轉(zhuǎn)義之用,還可以加在變量名之前,使變量名與關(guān)鍵字不沖突,這種用法稱為“逐字標(biāo)識(shí)符”,需要的朋友可以參考下2015-06-06
C#用正則表達(dá)式Regex.Matches 方法檢查字符串中重復(fù)出現(xiàn)的詞
使用正則表達(dá)式用Regex類的Matches方法,可以檢查字符串中重復(fù)出現(xiàn)的詞,Regex.Matches方法在輸入字符串中搜索正則表達(dá)式的所有匹配項(xiàng)并返回所有匹配,本文給大家分享C#正則表達(dá)式檢查重復(fù)出現(xiàn)的詞,感興趣的朋友一起看看吧2024-02-02
C# SQLite序列操作實(shí)現(xiàn)方法詳解
這篇文章主要介紹了C# SQLite序列操作實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了C#實(shí)現(xiàn)SQLite序列操作的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Unity利用材質(zhì)自發(fā)光實(shí)現(xiàn)物體閃爍
這篇文章主要為大家詳細(xì)介紹了Unity利用材質(zhì)自發(fā)光實(shí)現(xiàn)物體閃爍,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
WPF使用webView實(shí)現(xiàn)顯示瀏覽器網(wǎng)頁
在WPF中顯示一個(gè)可以操作的瀏覽器界面,你可以使用WebBrowser控件或WebView2控件,下面我們就來看看如何分別使用這兩個(gè)控件實(shí)現(xiàn)顯示瀏覽器網(wǎng)頁吧2025-01-01
登錄驗(yàn)證全局控制的幾種方式總結(jié)(session)
在登陸驗(yàn)證或者其他需要用到session全局變量的時(shí)候,歸結(jié)起來,主要有以下三種較方便的實(shí)現(xiàn)方式。(其中個(gè)人較喜歡使用第一種實(shí)現(xiàn)方法)2014-01-01
C#解決SQlite并發(fā)異常問題的方法(使用讀寫鎖)
這篇文章主要介紹了C#解決SQlite并發(fā)異常問題的方法,通過使用讀寫鎖達(dá)到多線程安全訪問,進(jìn)而解決SQLite并發(fā)異常的問題,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

