程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#編寫飛行棋游戲 源代碼

C#編寫飛行棋游戲 源代碼

編輯:關於C#
 

半天寫出來的。。。眼疼。。。大伙湊胡看看,不懂得裡面都有注釋。直接復制源碼放到vs上即可編譯運行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 鄧振奇遇記
{
class Program
{
public static int[] Maps = new int[100];//存放地圖
public static int[] PlayerPos = new int[2];//存放玩家位置 PlayerPos[0]表示第一個玩家 1表示第二個玩家
public static string[] PlayerName = new string[2];//存放玩家姓名
public static bool[] flag = new bool[2];
static void Main(string[] args)
{
Welcome();
GiveName();//獲取玩家姓名
Console.Clear();//GIveName後清屏
InItialMap();//初始化數組 令數組中所有的0值變為1,2,3,4
DrawMap();
while (true)
{
Main1();

C#編寫飛行棋游戲 源代碼
C#編寫飛行棋游戲 源代碼
 


}
}//GO GO GO
public static void Welcome()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("************************************************************");
Console.WriteLine("************************************************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("******************* 鄧振奇遇記 v1.0版本 ******************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("************************************************************");
Console.WriteLine("************************************************************");
}//顯示游戲界面
public static void InItialMap()
{
//0 普通 方塊□
//1 幸運轉盤◎
//2 地雷 ※
//3 暫停 ¢
//4 時空隧道¤
Random r = new Random();
int r1 = r.Next(1, 99);
int r2 = r.Next(1, 99);
int r3 = r.Next(1, 99);
int r4 = r.Next(1, 99);
int r5 = r.Next(1, 99);
int r6 = r.Next(1, 99);
int r7 = r.Next(1, 99);
int r8 = r.Next(1, 99);
int r9 = r.Next(1, 99);
int r10 = r.Next(1, 99);
int r11= r.Next(1, 99);
int r12= r.Next(1, 99);
int r13 = r.Next(1, 99);
int r14 = r.Next(1, 99);
int r15 = r.Next(1, 99);
int r16 = r.Next(1, 99);
int r17 = r.Next(1, 99);
int r18 = r.Next(1, 99);
int r19 = r.Next(1, 99);
int r20 = r.Next(1, 99);
int r21= r.Next(1, 99);
int r22= r.Next(1, 99);
int r23 = r.Next(1, 99);
int r24= r.Next(1, 99);
int r25 = r.Next(1, 99);
int r26 = r.Next(1, 99);
int r27 = r.Next(1, 99);
int r28 = r.Next(1, 99);
int r29 = r.Next(1, 99);
int r30 = r.Next(1, 99);
//隨機地圖的生成,因為有可能重復,所以多新建幾個地圖元素
int[] luckyTurn = { r1, r2, r3, r4, r5,r6 };
for (int i = 0; i < luckyTurn.Length; i++)
{
Maps[luckyTurn[i]] = 1;
}
int[] landMine = {r7, r8, r9, r10, r11, r12, r13,r14, r15,r16,r17,r18};
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
int[] pause = { r19, r20 ,r21,r22,r23,r24};
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = {r25,r26,r27,r28,r29,r30 };
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
}//初始化地圖 (實現隨機地圖生成功能)
public static void DrawMap()
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("************************************************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("開始游戲吧===>玩家A:{0} 玩家B:{1}", PlayerName[0], PlayerName[1]);
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("************************************************************");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("圖鑒如下=>普通道路:□ 逆天改命:◎ 狗屎:※ 美女:¢ 超級美女:¤");
#region 第一橫行 0-29
for (int i = 0; i <= 29; i++)
{
Console.Write(DrawString(i));
}
Console.WriteLine("");//畫完第一橫行換行,然後畫第二豎行
#endregion
#region 第一豎行 30-34
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
Console.WriteLine(DrawString(i));
}
#endregion
#region 第二橫行 64-35
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawString(i));
}
Console.WriteLine("");//畫完第二橫行換行
#endregion
#region 第二豎行 65-69
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawString(i));
}
#endregion
#region 第三橫行 70-99
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawString(i));
}
Console.WriteLine("");
#endregion
}//畫地圖
public static string DrawString(int i)
{
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
{
return "<>";//表示位置相同的時候 顯示<>,如果玩家二等於i 表示在地圖上
}
else if (PlayerPos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Magenta;
return "A"; //全角A占得位置等於兩個半角A 所以用全角
}
else if (PlayerPos[1] == i)
{
Console.ForegroundColor = ConsoleColor.Magenta;
return "B";
}
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.DarkCyan;//必須給全部的字符賦顏色值 否則會出現AB移動時改變其他字符顏色的bug
return "□";
//break;
case 1:
Console.ForegroundColor = ConsoleColor.DarkCyan;
return "◎";
case 2:
Console.ForegroundColor = ConsoleColor.DarkCyan;
return "※";
case 3:
Console.ForegroundColor = ConsoleColor.DarkCyan;
return "¢";
case 4:
Console.ForegroundColor = ConsoleColor.DarkCyan;
return "¤";
}//swith
}//else
return "";//因為前面有返回值 所以最後返回一個空字符串就行了 否則報錯【不是所有的代碼都有返回值】
}//畫道路上的特殊符號
public static void GiveName()
{
Console.WriteLine("請輸入玩家A的姓名:");
PlayerName[0] = Console.ReadLine();
while (PlayerName[0] == "")
{
Console.WriteLine("玩家A的姓名不能為空,請重新輸入:");
PlayerName[0] = Console.ReadLine();
}
Console.WriteLine("請輸入玩家B的姓名:");
PlayerName[1] = Console.ReadLine();
while (PlayerName[1] == "" || PlayerName[1] == PlayerName[0])
{
if (PlayerName[1] == "")
{
Console.WriteLine("玩家B的姓名不能為空,請重新輸入:");
PlayerName[1] = Console.ReadLine();
}
else
{
Console.WriteLine("親,玩家B的姓名不能和玩家A的姓名一樣哦,請重新輸入:");
PlayerName[1] = Console.ReadLine();
}
}

}//錄入姓名模塊
public static void PlayGame(int playerNumber)
{
Random r = new Random();
int rNumber = r.Next(1, 7);
Console.WriteLine("{0}按下任意鍵開始擲骰子", PlayerName[playerNumber]);
Console.ReadKey(true);//表示不再控制台顯示按下的是什麼鍵
Console.WriteLine("{0}擲出了{1}", PlayerName[playerNumber], rNumber);
PlayerPos[playerNumber] += rNumber;
LimitPlayerInMap();
Console.ReadKey(true);
Console.WriteLine("{0}按任意鍵行動", PlayerName[playerNumber]);
Console.Clear();
DrawMap();
if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])//如果玩家A踩住了玩家B,B退6格
{

Console.WriteLine("{0}踩中了{1},{2}退6格", PlayerName[playerNumber], PlayerName[1 - playerNumber], PlayerName[1 - playerNumber]);
Console.Clear();
DrawMap();
Console.ReadKey(true);
PlayerPos[1 - playerNumber] -= 6;
LimitPlayerInMap();
Console.WriteLine("{0}退6格", PlayerName[playerNumber]);
Console.ReadKey(true);
}
else//沒有踩到玩家 踩到其他圖鑒上
{
switch (Maps[PlayerPos[playerNumber]])
{
case 0:
Console.WriteLine("{0}踩到普通道路,游戲繼續。", PlayerName[playerNumber]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("{0}踩到了逆天改命。請選擇以下數字。1:和{1}交換位置 2:用糞球扔{2}", PlayerName[playerNumber], PlayerName[1 - playerNumber], PlayerName[1 - playerNumber]);
Console.ReadKey(true);
while (true)
{
string input = Console.ReadLine();
if (input == "1")
{
int temp;
temp = PlayerPos[playerNumber];
PlayerPos[playerNumber] = PlayerPos[1 - playerNumber];
PlayerPos[1 - playerNumber] = temp;
LimitPlayerInMap();
Console.Clear();
DrawMap();
Console.WriteLine("{0}選擇交換位置", PlayerName[playerNumber]);
Console.ReadKey(true);
break;
}
else if (input == "2")
{

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved