1、 初始化地圖,在繪制時可先將地圖進行初始化,用數組來存儲關卡的位置,然后利用循環給地圖中 關卡所在處賦予代表關卡的值。
關鍵代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/// <summary> /// 初始化游戲地圖 /// </summary> static void initialmap() { for ( int i=0;i<map.length;i++) { map[i] =0; } //用于存儲關卡位置 int [] luckyturn = { 6, 23, 40, 55, 69, 83,98 }; //幸運轉盤 1 int [] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; //地雷 2 int [] pause = { 9, 27, 60, 93 }; //暫停 3 int [] timetunnel = { 20, 25, 45, 63, 72, 88, 90}; //時空隧道 4 for ( int i=0;i<luckyturn.length;i++) { int pos = luckyturn[i]; map[pos] = 1; } for ( int i=0;i<landmine.length;i++) { map[landmine[i]] = 2; } for ( int i=0;i<pause.length;i++) { int pos = pause[i]; map[pos] = 3; } for ( int i=0;i<timetunnel.length;i++) { int pos = timetunnel[i]; map[pos] =4; } } |
2、檢查坐標的值,在將地圖進行初始化之后,便可開始進行繪制地圖的操作了,地圖繪制可使用 在程序設計時所講的分布繪制,在繪制地圖時應檢驗該該坐標點的值,在根據該點的值繪制相應的圖案,在檢查時根據值 返回相應的圖案 ,在利用循環繪制出即可,檢查坐標的值代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/// <summary> /// 獲得要繪制的坐標 /// </summary> /// <param name="i"> 要繪制的坐標</param> /// <returns></returns> static string getmapstring( int i) { string result= "" ; //用于返回 給一個坐標相應的圖案 if (playerpos[0] == i && playerpos[1] == i) //判斷是否是對戰雙方所在此處 { console.foregroundcolor = consolecolor.yellow; //設置圖案的前景色為黃色 result = "<>" ; //得到兩人均在圖案 } else if (playerpos[0] == i) { console.foregroundcolor = consolecolor.yellow; result = "a" ; //得到a均在圖案 } else if (playerpos[1] == i) { console.foregroundcolor = consolecolor.yellow; result = "b" ; //得到b均在圖案 } else { switch (map[i]) { case 0: console.foregroundcolor = consolecolor.white; result = "□" ; //得到普通均在圖案 break ; case 1: console.foregroundcolor = consolecolor.red; result = "○" ; //得轉盤圖案 break ; case 2: console.foregroundcolor = consolecolor.blue; result = "☆" ; break ; case 3: console.foregroundcolor = consolecolor.green; result = "▲" ; break ; case 4: console.foregroundcolor = consolecolor.darkblue; result = "卍" ; break ; } } return result; //返回圖案 } |
3、繪制地圖,在得到 返回的圖案后,便可進行地圖的繪制,這里給出繪制第一行的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/// <summary> /// 繪制游戲地圖 /// </summary> static void drownmap() { console.writeline( "圖例:幸運轉盤 ○ 地雷 ☆ 暫停 ▲ 時空隧道 卍" ); //畫第一行 下標0-29 的地圖 for ( int i=0;i<30;i++) //循環坐標得到 第一行每個點的圖案 { console.write(getmapstring(i)); //調用函數得到每個坐標的圖案 } console.write( "\n" ); console.resetcolor(); //重置前景色 } |
以上所述是小編給大家介紹的c#繪制飛行棋地圖小程序,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/lovelei/archive/2016/09/09/5856014.html