C#實現(xiàn)winform版飛行棋
本文實例為大家分享了C#實現(xiàn)winform版飛行棋的具體代碼,供大家參考,具體內(nèi)容如下
游戲界面


游戲規(guī)則:
1、兩個人輪流擲骰子紅人和綠人
2、投擲出2,4,6點出門,投擲出6點可以在出門后再次投擲行走
3、地圖長度共100步
4、地圖中除過普通地板之外,另設六種特殊功能地板
(1) 踩到香蕉皮,退6步
(2) 踩到時空,前進6步
(3) 踩到陷阱,暫停一回合
(4) 踩到星星,可以再投擲一次
(5) 踩到移魂大法,可以做出選擇與對方互換位置
(6) 踩到手槍,可以擊退對方3步
(7) 踩到大炮,可以直接將對方轟炸回家(需要重新出門)
5、如果踩到對方,則對方直接回到起點,
游戲策劃
1.地圖面積30*13
2.每個格子30像素
3.地面的代號=0,普通地板=1,香蕉皮=2,時空=3,陷阱=4,星星=5,大挪移=6,手槍=7
紅人=8,綠人=9
起點=10,終點=11
兩人在同一位置=12
程序代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 飛行棋
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Panel map = new Panel(); // 創(chuàng)建游戲區(qū)對象
int[] mapList = new int[390];//存儲地圖數(shù)的數(shù)組
PictureBox[] mapImg = new PictureBox[390];// 存儲圖片的數(shù)組
const int size = 30;// 格子大小
Label jiLu = new Label();
RichTextBox msg = new RichTextBox(); //存儲近況
Random r = new Random();
PictureBox dice = new PictureBox(); //創(chuàng)建骰子對象
Panel redplayHome = new Panel(); // 紅方飛機
Panel greenplayHome = new Panel(); //綠方飛機
private void Form1_Load(object sender, EventArgs e)
{
// 設置不可移動
this.FormBorderStyle = FormBorderStyle.FixedSingle;
// this.FormBorderStyle=FormBorderStyle.FixedToolWindow與Icon圖標不可連用
this.BackColor =Color.Plum;
this.Size = new Size(1200, 600);
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2);
// 地圖對象
map.Width = 30 * size;
map.Height = 13 * size;
map.Location = new Point(10, 150);
map.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(map);
InitialGame(); //調(diào)用初始化地圖圖片方法
// 紅方的家
redplayHome.Size = new Size(100, 100);
redplayHome.BackgroundImage = Image.FromFile("../../img/select_circle.png");
redplayHome.Location = new Point(20, map.Top - 120);
redplayHome.BackgroundImageLayout = ImageLayout.Stretch;
this.Controls.Add(redplayHome);
// 綠方的家
greenplayHome.Size = new Size(100, 100);
greenplayHome.BackgroundImage = Image.FromFile("../../img/select_circle.png");
greenplayHome.Location = new Point(map.Left+map.Width-greenplayHome.Width, map.Top - 120);
greenplayHome.BackgroundImageLayout = ImageLayout.Stretch;
this.Controls.Add(greenplayHome);
//紅飛機
PictureBox redPlayer = new PictureBox();
redPlayer.Size = new Size(80, 80);
redPlayer.Image = Image.FromFile("../../img/red.png");
redPlayer.Location = new Point(10, 10);
redPlayer.SizeMode = PictureBoxSizeMode.StretchImage;
redplayHome.Controls.Add(redPlayer);
//綠飛機
PictureBox greenPlayer = new PictureBox();
greenPlayer.Size = new Size(80, 80);
greenPlayer.Image = Image.FromFile("../../img/green.png");
greenPlayer.Location = new Point(10, 10);
greenPlayer.SizeMode = PictureBoxSizeMode.StretchImage;
greenplayHome.Controls.Add(greenPlayer);
// 記錄游戲近況框
msg.Size = new Size(260, 390);
msg.ReadOnly = true;//設置只讀
msg.Location = new Point(map.Right,map.Top);
msg.Font = new Font("微軟雅黑", 12);
msg.BackColor = Color.LightPink;
this.Controls.Add(msg);
// 游戲近況字
jiLu.Size = new Size(100, 30);
jiLu.Text = "游戲近況";
jiLu.Font = new Font("楷體", 16);
jiLu.ForeColor = Color.Maroon;
jiLu.BackColor = Color.Aquamarine;
jiLu.Location = new Point(msg.Left+msg.Width/2-jiLu.Width/2, map.Top-jiLu.Height);
this.Controls.Add(jiLu);
//骰子
dice.Size = new Size(100, 100);
dice.Image = Image.FromFile("../../img/roll.png");
dice.Location = new Point(map.Left+map.Width/2-dice.Width/2,map.Top-dice.Height);
dice.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(dice);
dice.MouseClick += Dice_MouseClick;
string startmsg = "請兩個人先輪流擲骰子,點大的先一步,紅方先手";
ResultTell(startmsg); //調(diào)用
}
// 骰子點擊事件
private void Dice_MouseClick(object sender, MouseEventArgs e)
{
PlayDice();
PlayGame();
}
// 記錄誰可以投擲 索引0代表紅方,1代表綠方
bool[] whoCan = new bool[2] { true, false };
int[] startNum = new int[2]; // 記錄雙方投擲的點數(shù)
string[] playeName = new string[2] { "紅方", "綠方" };
int[] playPostion = new int[2] { -1, -1 };// 記錄兩個人現(xiàn)在位置 沒有出門默認-1
int[] playStan = new int[2]{ -1,-1}; // 記錄上飛機上一個位置的坐標索引默認為-1
private void PlayDice() // 輪流投擲的方法 此方法為投擲為一輪
{
//紅方先投 默認紅方先投擲
if (whoCan[0]) //默認為true
{
startNum[0] = r.Next(1, 7);
ResultTell(string.Format("紅方投擲出【{0}】點", startNum[0]));
whoCan[0] = !whoCan[0]; // false賦給紅方
}
else
{
whoCan[0] = !whoCan[0]; //將false變?yōu)閠rue
}
// 綠方投擲
if (whoCan[1]) // 默認為false
{
startNum[1] = r.Next(1, 7);
ResultTell(string.Format("綠方投擲出【{0}】點", startNum[1]));
whoCan[1] = !whoCan[1]; // 將true變?yōu)閒alse
}
else
{
whoCan[1] = !whoCan[1]; //將true變?yōu)閒alse
}
}
// 控制游戲剛開始誰先行
bool start = true;
// 判斷是不是游戲剛開始,比點數(shù)確定先行
private void PlayGame()
{
// 判斷是否投擲完一輪,投擲完一輪后才能判斷點數(shù)誰先行
if (start)
{
// 雙方都投擲后,點數(shù)都相同的情況
OutDoor();
if (whoCan[0] && !whoCan[1]) // true && true
{
ResultTell("請紅方投擲");
}
else if (!whoCan[0] && whoCan[1]) // false && true
{
ResultTell("請綠方投擲");
}
}
else
{ // 判斷誰變成false,說明上次是誰投擲的
if (whoCan[0]&&!whoCan[1]) //綠方
{
PlayeReturn(1);
}
else if (!whoCan[0]&&whoCan[1])// 紅方
{
PlayeReturn(0);
}
}
}
// 是否暫停判斷
bool[] reclick = new bool[2] { false,false };
//雙方開始游戲,playIndex為索引0,1來判斷是紅方還是綠方
private void PlayeReturn(int playIndex)
{ // 判斷此方位置為-1,則沒有出發(fā)
if (playPostion[playIndex] == -1)
{
switch (startNum[playIndex]) //判斷點數(shù) 2,4,6 可以出發(fā)
{
case 2:
case 4:
ResultTell(string.Format("{0}可以起步!", playeName[playIndex]));
playPostion[playIndex] = 0; //初始到開始位置
playStan[playIndex] = 0; //存儲上一次位置
// 如果位置相同 換兩個飛機的圖片
if (playPostion[1] == playPostion[0])
{
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2];
}
else // 如果不同則位置對應的圖片
{
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
}
break;
case 6:
whoCan[playIndex] = true; //此方重新投擲
whoCan[1 - playIndex] = false;//對方不投擲
ResultTell(string.Format("{0}可以起步!", playeName[playIndex]));
playPostion[playIndex] = 0; // 初始到索引為0處
playStan[playIndex] = 0; // 數(shù)組存儲上一次位置為0
// 如果位置相同 換兩個飛機的圖片
if (playPostion[1] == playPostion[0])
{
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2];
}
else // 如果不同則位置對應的圖片
{
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
}
ResultTell(string.Format("請{0}投擲骰子", playeName[playIndex]));
break;
default:
ResultTell(string.Format("很遺憾,{0}投擲出{1}點無法起步!輪到{2}投擲", playeName[playIndex], startNum[playIndex], playeName[1 - playIndex]));
break;
}
if (playPostion[0]!=-1) //紅色出門
{
redplayHome.Controls.Clear(); // 紅色飛機的家中飛機消失
}
if (playPostion[1]!=-1) // 綠色出門
{
greenplayHome.Controls.Clear(); //綠色飛機的家中飛機消失
}
}
else // 不是-1則已經(jīng)出發(fā)
{
// 將現(xiàn)在位置賦給記錄上一次位置的數(shù)組中
playStan[playIndex] = playPostion[playIndex];
playPostion[playIndex] += startNum[playIndex]; //將點數(shù)賦給位置
ResultTell(string.Format("{0}移動{1}步", playeName[playIndex], startNum[playIndex]));
if (playPostion[playIndex]>=99)
{
ResultTell(string.Format("{0}獲勝!", playeName[playIndex]));
playPostion[playIndex] = 99;
ChangImg(playIndex);
return;
}
ChangImg(playIndex); // 改變圖片
// 判斷移動完后位置
if (playPostion[playIndex]==playPostion[1-playIndex])
{
playPostion[1 - playIndex] = 0;
ResultTell(string.Format("厲害!{0}精準踩到{1},{0}的當前位置是{2},{1}的當前位置是{3}", playeName[playIndex], playeName[1 - playIndex], playPostion[playIndex], playPostion[1 - playIndex]));
playStan[1 - playIndex] = playPostion[1 - playIndex];
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[1 - playIndex];
mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1 - playIndex];
ResultTell(string.Format("{0}開始投擲。", playeName[1 - playIndex]));
}
switch (mapList[road[playPostion[playIndex]]])
{
case 1: // 走到索引為1的地圖上時
ResultTell(string.Format("{0}安全到達!當前位置是{1}", playeName[playIndex],playPostion[playIndex]));
ResultTell(string.Format("{0}開始投擲。", playeName[1 - playIndex]));
break;
case 2: //香蕉皮
ResultTell(string.Format("很不幸,{0}踩中香蕉皮,退6步!當前位置是{1}", playeName[playIndex],playPostion[playIndex]));
playStan[playIndex] = playPostion[playIndex];
//記錄當前的位置 到上次位置數(shù)組中
playPostion[playIndex] -= 6; // 現(xiàn)在的位置后退6步
ChangImg(playIndex); // 添加對應的圖片
ResultTell(string.Format("{0}當前的位置是{1}",playeName[playIndex],playPostion[playIndex]));
ResultTell(string.Format("{0}開始投擲。", playeName[1 - playIndex]));
break;
case 3: // 時空隧道
ResultTell(string.Format("恭喜!{0}踩中時空隧道,前進6步!當前位置是{1}", playeName[playIndex],playPostion[playIndex]));
playStan[playIndex] = playPostion[playIndex];// 將現(xiàn)在坐標存儲到記錄上位置的數(shù)組中
playPostion[playIndex] += 6; // 現(xiàn)在的位置后退6步
ChangImg(playIndex); // 添加對應的圖片
ResultTell(string.Format("{0}當前位置是{1}", playeName[playIndex], playPostion[playIndex]));
ResultTell(string.Format("{0}開始投擲。", playeName[1 - playIndex]));
break;
case 4: //陷阱
ResultTell(string.Format("可惜!{0}踩中陷阱,暫停一回合!", playeName[playIndex]));
reclick[1 - playIndex] = true;
reclick[playIndex] = false;
break;
case 5: // 幸運星
ResultTell(string.Format("恭喜!踩中幸運星,再玩一回合!當前位置是{1}", playeName[playIndex],playPostion[playIndex]));
whoCan[playIndex] = true; //開啟繼續(xù)搖骰子
whoCan[1 - playIndex] = false;
ResultTell(string.Format("{0}繼續(xù)投擲。", playeName[playIndex]));
break;
case 6: // 大羅移
ResultTell(string.Format("恭喜!{0}踩中大羅移,請選擇是否移動!當前位置是{1}", playeName[playIndex],playPostion[playIndex]));
DialogResult dr= MessageBox.Show("是否選擇移動!","大羅移!",MessageBoxButtons.YesNo);
if (dr==DialogResult.Yes)
{ //雙方位置互換
int temp = playPostion[playIndex];
playPostion[playIndex] = playPostion[1 - playIndex];
playPostion[1 - playIndex] = temp;
playStan[playIndex] = playPostion[playIndex]; // 將此方現(xiàn)在位置賦給記錄上次位置的數(shù)組
playStan[1 - playIndex] = playPostion[1 - playIndex];// 將另一方現(xiàn)在的位置賦給記錄上次位置的數(shù)組
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1-playIndex];
}
ResultTell(string.Format("{0}的當前位置是{1},{2}的當前位置是{3}", playeName[playIndex], playPostion[playIndex], playeName[1 - playIndex], playPostion[1 - playIndex]));
ResultTell(string.Format("{0}開始投擲。", playeName[1 - playIndex]));
break;
case 7: // 手槍
ResultTell(string.Format("恭喜!{0}獲得手槍,可選擇擊退對方3步!當前位置是{1}", playeName[playIndex],playPostion[playIndex]));
DialogResult drr = MessageBox.Show("是否選擇擊退對方!", "手槍!", MessageBoxButtons.YesNo);
if (drr==DialogResult.Yes)
{
playStan[1 - playIndex] = playPostion[1 - playIndex]; // 記錄對方位置
playPostion[1 - playIndex] -= 3; //對方現(xiàn)在位置后移3個
mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1 - playIndex];// 添加圖片
ChangImg(1-playIndex);
}
ResultTell(string.Format("{0}被擊退對方3步!當前位置是{1}", playeName[1-playIndex], playPostion[1-playIndex]));
ResultTell(string.Format("{0}開始投擲。", playeName[1 - playIndex]));
break;
}
/*
第一輪:↓
紅色踩到后: reclick[0紅色]=reclick=false; reclick[1綠色]=reclick=true;
此時紅色搖完,綠色搖 whcan[0]=false,whcan[1]=true;
reclick[playIndex] && !reclick[1-playIndex]=true:false;
第二輪↓
reclick[1綠色]=reclick=true;reclick[0紅色]=reclick=false;
whcan[0]=true,whcan[1]=false;
reclick[playIndex==綠色]&&!reclick[1-playIndex==紅色]=true:false;
*/
if (reclick[playIndex]&&!reclick[1-playIndex])
{
whoCan[playIndex] = true;
whoCan[1 - playIndex] = false;
reclick[playIndex] = false;
reclick[playIndex] = false;
}
/*
* 第二輪結束
whcan[0]=false,whcan[1]=true;
第三輪開始
whcan[0]=false,whcan[1]=true;
//三輪結束
whcan[0]=true,whcan[1]=false;
*/
}
}
private void ChangImg(int playIndex)
{
// 如果位置相同 換成兩個飛機的圖片
if (playPostion[1] == playPostion[0])
{
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2];
}
else // 如果不同則位置對應的圖片
{
mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
}
// 雙方已經(jīng)出發(fā),兩個人在同一位置,此方圖片離開后,顯示另一方圖片
if (playStan[0]==playStan[1] &&playStan[0]!=-1&&playStan[0]!=-1) // 如果上次位置雙方相同
{
mapImg[road[playStan[playIndex]]].Image = imageList1.Images[1 - playIndex];
}
else // 上次雙方位置不同,變?yōu)橹澳J的圖片
{
switch (mapList[road[playStan[playIndex]]]) //判斷上一次記錄的圖片
{
case 0: // 地板
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/floor.png");
break;
case 1: // 路
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/water.gif");
break;
case 2: // 香蕉皮
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xj.jpg");
break;
case 3: // 時空
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/sk.jpg");
break;
case 4: // 陷阱
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xianjing.jpg");
break;
case 5: // 星星
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xx.jpg");
break;
case 6: // 交換
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/jh.jpg");
break;
case 7: // 手槍
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/sq.jpg");
break;
case 10: // 開始位置
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-out.jpg");
break;
case 11: // 結束位置
mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-over.jpg");
break;
default:
break;
}
}
}
// 判斷投擲完一輪,相同點數(shù),誰點數(shù)大 誰再投擲
private void OutDoor()
{
//投擲完一輪,并且雙方都存有點數(shù)
if (whoCan[0] && !whoCan[1] && (startNum[0] != 0 || startNum[1] != 0))
{ // 雙方點數(shù)相同
if (startNum[0].Equals(startNum[1]))
{
ResultTell("雙方點數(shù)相同,請重新投擲!");
}
else
{
start = false; //不是第一回合
if (startNum[0] > startNum[1])
{
ResultTell("紅方點數(shù)較大,先行一步,紅方投擲");
whoCan[0] = true; //紅方繼續(xù)投擲
whoCan[1] = false;
}
else
{
ResultTell("綠方點數(shù)較大,先行一步,綠方投擲");
whoCan[0] = false;
whoCan[1] = true; //綠方繼續(xù)投擲
}
}
}
}
void InitialGame() //初始化地圖
{
for (int i = 0; i < mapImg.Length; i++)
{
CreateMap();// 調(diào)用存儲數(shù)字的圖片
CreateGear(); // 調(diào)用創(chuàng)建機關的方法 必須向有路,才能調(diào)用機關的方法
PictureBox picture = new PictureBox();
picture.Size = new Size(size, size);
mapImg[i] = picture; //圖片添加到數(shù)組
switch (mapList[i]) // 判斷地圖數(shù)組中存儲的數(shù)字,添加圖片
{
case 0: // 地板
picture.Image = Image.FromFile("../../img/floor.png");
break;
case 1: // 路
picture.Image = Image.FromFile("../../img/water.gif");
break;
case 2: // 香蕉皮
picture.Image = Image.FromFile("../../img/xj.jpg");
break;
case 3: // 時空
picture.Image = Image.FromFile("../../img/sk.jpg");
break;
case 4: // 陷阱
picture.Image = Image.FromFile("../../img/xianjing.jpg");
break;
case 5: // 星星
picture.Image = Image.FromFile("../../img/xx.jpg");
break;
case 6: // 交換
picture.Image = Image.FromFile("../../img/jh.jpg");
break;
case 7: // 手槍
picture.Image = Image.FromFile("../../img/sq.jpg");
break;
case 10: // 開始位置
picture.Image = Image.FromFile("../../img/game-out.jpg");
break;
case 11: // 結束位置
picture.Image = Image.FromFile("../../img/game-over.jpg");
break;
default:
break;
}
picture.SizeMode = PictureBoxSizeMode.StretchImage; //圖片適應大小
picture.Location= new Point(i % 30 * size,i/30*size);
map.Controls.Add(picture);// 圖片添加到地圖中
}
}
void CreateMap() //創(chuàng)建地圖
{
CreateRoed();//創(chuàng)建路
for (int i = 0; i < road.Length; i++)
{
mapList[road[i]] = 1; // 路 將路對應的索引變?yōu)?
}
mapList[0] = 10; //開始位置
mapList[mapList.Length - 1] = 11; //結束位置
}
int[] road = new int[100];// 記錄所有路的索引位置
int[] back = {7,27,43,63,96}; // 記錄香蕉皮的索引位置
int[] forword = {10,25,33,65,80,89 };// 記錄時空的索引位置
int[] stop = { 3,20,35,50,60,70,90}; //記錄陷阱的索引位置
int[] star = { 5,28,45,71,85}; //記錄星星的索引位置
int[] change = { 4,55,75,98};//記錄交換的索引位置
int[] gun = {22,32,66,82 };//記錄手槍的索引位置
void CreateRoed() // 創(chuàng)建路
{
for (int i = 0; i < 30; i++) // 0-29的索引 向右
{
road[i] = i;
}
for (int i = 30; i <=35; i++) // 59,89,119,149,179 右下五格
{
road[i] = road[i - 1] + 30;
// i=30; road[30]===>29
// i=31; road[31-1]+30==>road[30]+30==>29+30==>59
// i=32;road[32-1]+30==>road[31]+30==>59+30==>89
}
for (int i = 36; i <65 ; i++) // 向左
{
road[i] = road[i - 1] - 1;
// i=36; road[36-1]-1=>road[35]-1=>179-1=>178 ...
}
for (int i = 65; i <=70; i++) // 左下五格
{
road[i] = road[i - 1] + 30;
}
for (int i = 71; i <100; i++) // 向右
{
road[i] = road[i - 1] + 1;
}
}
void CreateGear() // 創(chuàng)建機關
{
for (int i = 0; i < back.Length; i++)
{
mapList[road[back[i]]] = 2; // 香蕉皮
}
for (int i = 0; i < forword.Length; i++)
{
mapList[road[forword[i]]] = 3; // 時空
}
for (int i = 0; i < stop.Length; i++)
{
mapList[road[stop[i]]] = 4; // 陷阱
}
for (int i = 0; i < star.Length; i++)
{
mapList[road[star[i]]] = 5;// 星星
}
for (int i = 0; i < change.Length; i++)
{
mapList[road[change[i]]] = 6; // 交換
}
for (int i = 0; i < gun.Length; i++)
{
mapList[road[gun[i]]] = 7; // 手槍
}
}
void ResultTell(string str)
{
MessageBox.Show(str);
msg.AppendText(str + "\r\n"); // 將獲取的點數(shù)添加到文本框中
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#結合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺的操作方法
這篇文章主要介紹了C#結合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺,上傳視頻功能,主要要解決兩個問題,一是在服務端通過C#生成簽名和SDKID,二是在客戶端通過JavaScript上傳視頻到騰訊云點播服務器,感興趣的朋友跟隨小編一起看看吧2023-11-11

