|
c#下開發(fā)的小游戲,歡迎交流
源碼包(含五子棋,貪吃蛇,華容道,俄羅斯方塊這幾個游戲):
0.png (44.6 KB, 下載次數(shù): 51)
下載附件
2018-7-25 15:58 上傳
0.png (360.78 KB, 下載次數(shù): 45)
下載附件
2018-7-25 15:58 上傳
下面是其中一個項目的源程序如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Collections;
- using System.IO;
- namespace 五子棋
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public static int[,] note = new int[15, 15];//記錄棋子的擺放位置,0為白棋,1為黑棋
- FrmClass frmclass = new FrmClass();
- private static MessClass temMsg = new MessClass();
- public static float Mouse_X = 0;//記錄鼠標(biāo)的X坐標(biāo)
- public static float Mouse_Y = 0;//記錄鼠標(biāo)的Y坐標(biāo)
- //下面是初始化必需設(shè)置的變量
- public static bool ChessStyle = true;//黑方還是白方,true為黑方
- public static bool CGrow = true;//當(dāng)前棋子的類型
- public static bool DownChess = true;//對方是否下完棋
- public static int CKind = -1;//設(shè)錄取勝的棋種,0為白棋
- private void udpSocket1_DataArrival(byte[] Data, System.Net.IPAddress Ip, int Port)
- {
- DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataArrival);
- this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port }); //設(shè)置托管
- }
- private delegate void DataArrivaldelegate(byte[] Data, System.Net.IPAddress Ip, int Port);
- private void DataArrival(byte[] Data, System.Net.IPAddress Ip, int Port) //當(dāng)有數(shù)據(jù)到達(dá)后的處理進(jìn)程
- {
- //將接收的消息轉(zhuǎn)換成自定義集合MessClass
- MessClass msg = new ClassSerializers().DeSerializeBinary((new System.IO.MemoryStream(Data))) as MessClass;
- switch (msg.sendKind)//獲取發(fā)送的類型
- {
- case SendKind.SendConn://連接
- {
- if (msg.ChessStyle)//判斷當(dāng)前棋子的類型
- ChessStyle = true;//黑棋
- else
- ChessStyle = false;//白棋
- CGrow = ChessStyle;//記錄當(dāng)前棋子的類型
- CKind = -1;//記錄取勝的棋子種類
- temMsg.sendKind = SendKind.SendConnHit;//設(shè)置消息發(fā)送類型為連接成功
- temMsg.ChessStyle = ChessStyle;//在發(fā)送消息中設(shè)置當(dāng)前棋子的類型
- //向遠(yuǎn)程計算機(jī)發(fā)送消息
- udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
- break;
- }
- case SendKind.SendConnHit://連接成功
- {
- MessageBox.Show("連接成功");//顯示連接成功
- button1.Tag = 1;//設(shè)置標(biāo)識
- button1.Text = "重新開始";
- if (msg.ChessStyle)//如果是黑棋
- {
- ChessStyle = true;//設(shè)置本地的棋子類型為黑棋
- DownChess = true;//本地先下
- label2.Text = "黑棋";//顯示本地為黑棋
- }
- else
- {
- ChessStyle = false;//設(shè)置本地的棋子類型為白棋
- DownChess = false;//本地后下
- label2.Text = "白棋";//顯示本地為白棋
- }
- CGrow = ChessStyle;//記錄本地的棋子類型
- panel2.Visible = false;//隱藏最后落子的標(biāo)記
- break;
- }
- case SendKind.SendAfresh://重新下棋
- {
- //清空棋盤中各棋子的位置
- for (int i = 0; i < 15; i++)
- for (int j = 0; j < 15; j++)
- note[i, j] = -1;
- Graphics g = panel1.CreateGraphics();//創(chuàng)健panel1控件的Graphics類
- g.DrawImage(Properties.Resources.棋盤, 0, 0, panel1.Width, panel1.Height);//清空棋盤
- if (msg.ChessStyle)//如果是黑棋
- {
- ChessStyle = true;//設(shè)置本地的棋子類型為黑棋
- DownChess = true;//設(shè)置本地的棋子類型為黑棋
- label2.Text = "黑棋";//顯示本地為黑棋
- }
- else
- {
- ChessStyle = false;//設(shè)置本地的棋子類型為白棋
- DownChess = false;//本地后下
- label2.Text = "白棋";//顯示本地為白棋
- }
- CGrow = ChessStyle;//記錄本地的棋子類型
- CKind = -1;//記錄取勝的棋子種類
- panel2.Visible = false;//隱藏最后落子的標(biāo)記
- break;
- }
- case SendKind.SendChessman://接收發(fā)送的棋子
- {
- int tem_CS = -1;
- Image tem_Image;//實例化Image類
- if (msg.Grow)//如果為黑棋
- {
- tem_CS = 1;//記錄棋子類型為黑棋
- CGrow = true;//記錄當(dāng)前為黑棋
- tem_Image = Properties.Resources.黑棋子;//存儲黑棋的圖片
- }
- else
- {
- tem_CS = 0;//記錄棋子類型為白棋
- CGrow = false;//記錄當(dāng)前為黑棋
- tem_Image = Properties.Resources.白棋子;//存儲白棋的圖片
- }
- note[msg.ChessX, msg.ChessY] = tem_CS;//在數(shù)組中記錄當(dāng)前棋子的位置
- Graphics g = panel1.CreateGraphics();
- g.DrawImage(tem_Image, msg.ChessX * 35 + 7, msg.ChessY * 35 + 7, 35, 35);//在棋盤中顯示對方下的棋子
- panel2.Visible = true;//顯示最后落子的標(biāo)記
- panel2.Location = new System.Drawing.Point(msg.ChessX * 35 + 20, msg.ChessY * 35 + 20);//將標(biāo)記顯示在棋子上
- DownChess = msg.Walk;//記錄對方是否下完棋
- CGrow = !msg.Grow;//記錄本地的棋子類型
- Arithmetic(tem_CS, msg.ChessX, msg.ChessY);//計算對方是否獲勝
- DownChess = true;//對方已下完棋
- break;
- }
- case SendKind.SendCut://斷開連接
- {
- temMsg.sendKind = SendKind.SendCutHit;//設(shè)置發(fā)送的類型為斷開連接
- //向遠(yuǎn)程計算機(jī)發(fā)送斷開消息
- udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
- button1.Text = "連接";//顯當(dāng)前可重新連接
- button1.Tag = 0;//設(shè)置連接標(biāo)識
- break;
- }
- case SendKind.SendCutHit://斷開成功
- {
- udpSocket1.Active = false;//關(guān)閉UDP的連接
- Application.Exit();//關(guān)閉當(dāng)前工程
- break;
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //清空記錄各棋子位置的數(shù)組
- for (int i = 0; i < 15; i++)
- for (int j = 0; j < 15; j++)
- note[i, j] = -1;
- CKind = -1;//記錄棋子種類
- if (Convert.ToInt32(((Button)sender).Tag.ToString()) == 0)//如當(dāng)前為開始連接
- {
- if (textBox1.Text.Trim() == "")//如果IP地址為空
- {
- MessageBox.Show("請輸入IP地址。");
- return;
- }
- FrmClass.ServerIP = textBox1.Text;//記錄遠(yuǎn)程計算機(jī)的IP地址
- udpSocket1.Active = false;//關(guān)閉UDP的連接
- udpSocket1.LocalPort = 11001;//設(shè)置端口號
- udpSocket1.Active = true;//打開UDP的連接
- temMsg.sendKind = SendKind.SendConn;//設(shè)置發(fā)送類型為連接
- temMsg.ChessStyle = !ChessStyle;//設(shè)置對方的棋子類型
- }
- if (Convert.ToInt32(((Button)sender).Tag.ToString()) == 1)//如當(dāng)前為重新開始
- {
- ChessStyle = !ChessStyle;//改變本地的棋子類型
- temMsg.sendKind = SendKind.SendAfresh;//設(shè)置消息類型為重新開始
- temMsg.ChessStyle = !ChessStyle;//設(shè)置遠(yuǎn)程計算機(jī)的棋子類型
- Graphics g = panel1.CreateGraphics();
- g.DrawImage(Properties.Resources.棋盤, 0, 0, panel1.Width, panel1.Height);//清空棋盤
- }
- if (ChessStyle)//如果本地的棋子類型為黑棋
- {
- label2.Text = "黑棋";//顯示本地為黑棋
- DownChess = true;
- }
- else
- {
- label2.Text = "白棋";//顯示本地為白棋
- DownChess = false;
- }
- CGrow = ChessStyle;//記錄當(dāng)前的棋子類型
- panel2.Visible = false;//隱藏最后落子的標(biāo)記
- //將消息發(fā)送給遠(yuǎn)程計算機(jī)
- udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //初始化記錄棋子的數(shù)組
- for (int i = 0; i < 15; i++)
- for (int j = 0; j < 15; j++)
- note[i, j] = -1;
- FrmClass.ClientIP = frmclass.MyHostIP();//獲取本地的IP地址
- FrmClass.ClientPort = "11001";//獲取端口號
- }
- private void Form1_FontChanged(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- temMsg.sendKind = SendKind.SendCut;//設(shè)置發(fā)送的消息為斷開
- if (FrmClass.ServerIP != "")//如果與遠(yuǎn)程計算機(jī)連接
- //將消息發(fā)送給遠(yuǎn)程計算機(jī)
- udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
- if (Convert.ToInt32(button1.Tag.ToString()) == 0)//如果沒有與遠(yuǎn)程計算機(jī)連接
- Application.Exit();//退出當(dāng)前工程
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- Image tem_Image = Properties.Resources.白棋子;//實例化Image類
- e.Graphics.DrawImage(Properties.Resources.棋盤, 0, 0, panel1.Width, panel1.Height);//清空棋盤
- //遍歷已下完的棋子
- for (int i = 0; i < 15; i++)
- for (int j = 0; j < 15; j++)
- {
- if (note[i, j] > -1)
- {
- if (note[i, j] == 0)//如果當(dāng)前是白棋
- tem_Image = Properties.Resources.白棋子;//在指定的位置繪制白棋
- if (note[i, j] == 1)//如果當(dāng)前是黑棋
- tem_Image = Properties.Resources.黑棋子;//在指定的位置繪制黑棋
- e.Graphics.DrawImage(tem_Image, i * 35 + 7, j * 35 + 7, 35, 35);//繪制已下完的棋子
- }
- }
- }
- private void panel1_Click(object sender, EventArgs e)
- {
- if (udpSocket1.Active == false)
- {
- MessageBox.Show("沒有與遠(yuǎn)程計算機(jī)建立連接!");
- return;
- }
- if (DownChess == false)//如果對方?jīng)]有下完棋
- {
- MessageBox.Show("對方?jīng)]有下完棋!");
- return;
- }
- if (CKind > -1)//如果當(dāng)前有棋子獲勝
- {
- if (CKind == 0)//如果白棋獲勝
- Bwin();//顯示白棋獲勝的提示框
- if (CKind == 1)//如果黑棋獲勝
- Wwin();//顯示黑棋獲勝的提示框
- return;
- }
- int Column = Convert.ToInt32(Math.Round((Mouse_X - 30) / 35));//獲取當(dāng)前落子的所在列
- int Row = Convert.ToInt32(Math.Round((Mouse_Y - 30) / 35));//獲取當(dāng)前落子的所在行
- if (note[Column, Row] == -1)//如果當(dāng)前位置無棋子
- {
- int tem_CS = -1;
- Image tem_Image;//實例化Image類
- if (CGrow)//如果當(dāng)前落子為黑棋
- {
- tem_CS = 1;
- CGrow = true;//記錄落子類型
- tem_Image = Properties.Resources.黑棋子;//存儲黑棋圖片
- }
- else
- {
- tem_CS = 0;
- CGrow = false;//記錄落子類型
- tem_Image = Properties.Resources.白棋子;//存儲白棋圖片
- }
- note[Column, Row] = tem_CS;//記錄當(dāng)前位置已有棋子
- Graphics g = panel1.CreateGraphics();
- g.DrawImage(tem_Image, Column * 35 + 7, Row * 35 + 7, 35, 35);//在棋盤中顯示當(dāng)前下的棋子
- panel2.Visible = true;//顯示最后落子的標(biāo)記
- panel2.Location = new System.Drawing.Point(Column * 35 + 20, Row * 35 + 20);//在棋子上顯示標(biāo)記
- DownChess = false;//對方?jīng)]有下棋
- temMsg.sendKind = SendKind.SendChessman;//設(shè)置發(fā)送的類型為發(fā)送棋子
- temMsg.ChessX = Column;//記錄棋子所在行
- temMsg.ChessY = Row;//記錄棋子所在列
- temMsg.Grow = CGrow;//記錄前棋子的類型
- temMsg.Walk = true;//記錄本地已下完棋
- //向遠(yuǎn)程計算機(jī)發(fā)送消息
- udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
- Arithmetic(tem_CS, Column, Row);//計算本地是否獲勝
- }
- }
- private void panel1_MouseDown(object sender, MouseEventArgs e)
- {
- //記錄鼠標(biāo)的單擊位置
- Mouse_X = e.X;
- Mouse_Y = e.Y;
- }
- /// <summary>
- /// 算法
- /// </summary>
- public void Arithmetic(int n, int Arow, int Acolumn)//算法
- {
- int BCount = 1;
- CKind = -1;//記錄棋子種類
- //橫向查找
- bool Lbol = true;
- bool Rbol = true;
- int jlsf = 0;
- BCount = 1;
- for (int i = 1; i <= 5; i++)
- {
- if ((Acolumn + i) > 14)
- Rbol = false;
- if ((Acolumn - i) < 0)
- Lbol = false;
- if (Rbol == true)
- {
- if (note[Arow, Acolumn + i] == n)
- ++BCount;
- else
- Rbol = false;
- }
- if (Lbol == true)
- {
- if (note[Arow, Acolumn - i] == n)
- ++BCount;
- else
- Lbol = false;
- }
- if (BCount >= 5)
- {
- if (n == 0)
- Bwin();
- if (n == 1)
- Wwin();
- jlsf = n;
- break;
- }
- }
- //縱向查找
- bool Ubol = true;
- bool Dbol = true;
- BCount = 1;
- for (int i = 1; i <= 5; i++)
- {
- if ((Arow + i) > 14)
- Dbol = false;
- if ((Arow - i) < 0)
- Ubol = false;
- if (Dbol == true)
- {
- if (note[Arow + i, Acolumn] == n)
- ++BCount;
- else
- Dbol = false;
- }
- if (Ubol == true)
- {
- if (note[Arow - i, Acolumn] == n)
- ++BCount;
- else
- Ubol = false;
- }
- if (BCount >= 5)
- {
- if (n == 0)
- Bwin();
- if (n == 1)
- Wwin();
- jlsf = n;
- break;
- }
- }
- //正斜查找
- bool LUbol = true;
- bool RDbol = true;
- BCount = 1;
- for (int i = 1; i <= 5; i++)
- {
- if ((Arow - i) < 0 || (Acolumn - i < 0))
- LUbol = false;
- if ((Arow + i) > 14 || (Acolumn + i > 14))
- RDbol = false;
- if (LUbol == true)
- {
- if (note[Arow - i, Acolumn - i] == n)
- ++BCount;
- else
- LUbol = false;
- }
- if (RDbol == true)
- {
- if (note[Arow + i, Acolumn + i] == n)
- ++BCount;
- else
- RDbol = false;
- }
- if (BCount >= 5)
- {
- if (n == 0)
- Bwin();
- if (n == 1)
- Wwin();
- jlsf = n;
- break;
- }
- }
- //反斜查找
- bool RUbol = true;
- bool LDbol = true;
- BCount = 1;
- for (int i = 1; i <= 5; i++)
- {
- if ((Arow - i) < 0 || (Acolumn + i > 14))
- RUbol = false;
- if ((Arow + i) > 14 || (Acolumn - i < 0))
- LDbol = false;
- if (RUbol == true)
- {
- if (note[Arow - i, Acolumn + i] == n)
- ++BCount;
- else
- RUbol = false;
- }
- if (LDbol == true)
- {
- if (note[Arow + i, Acolumn - i] == n)
- ++BCount;
- else
- LDbol = false;
- }
- if (BCount >= 5)
- {
- if (n == 0)
- Bwin();
- if (n == 1)
- Wwin();
- jlsf = n;
- break;
- }
- }
- }
- /// <summary>
- /// 顯示白棋獲勝
- /// </summary>
- public void Bwin()
- {
- CKind = 0;
- MessageBox.Show("白棋獲勝!");
- if (ChessStyle == false)//如果當(dāng)前為白棋
- label6.Text = Convert.ToString(Convert.ToInt32(label6.Text) + 1);//白棋得分
- else
- label7.Text = Convert.ToString(Convert.ToInt32(label7.Text) + 1);//黑棋得分
- }
- /// <summary>
- /// 顯示黑棋獲勝
- /// </summary>
- public void Wwin()
- {
- CKind = 1;
- MessageBox.Show("黑棋獲勝!");
- if (ChessStyle == true)
- label6.Text = Convert.ToString(Convert.ToInt32(label6.Text) + 1);//黑棋得分
- else
- label7.Text = Convert.ToString(Convert.ToInt32(label7.Text) + 1);//白棋得分
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
12-游戲設(shè)計.rar
(5.46 MB, 下載次數(shù): 52)
2018-7-25 10:29 上傳
點擊文件名下載附件
c#小開發(fā) 下載積分: 黑幣 -5
|
|