找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3895|回复: 1
打印 上一主题 下一主题
收起左侧

c#下开发的五子棋,贪吃蛇,华容道,俄罗斯方块小游戏源码

[复制链接]
跳转到指定楼层
楼主
c#下开发的小游戏,欢迎交流

源码包(含五子棋,贪吃蛇,华容道,俄罗斯方块这几个游戏):


下面是其中一个项目的源程序如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. using System.Net;
  10. using System.Collections;
  11. using System.IO;

  12. namespace 五子棋
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }

  20.         public static int[,] note = new int[15, 15];//记录棋子的摆放位置,0为白棋,1为黑棋
  21.         FrmClass frmclass = new FrmClass();
  22.         private static MessClass temMsg = new MessClass();
  23.         public static float Mouse_X = 0;//记录鼠标的X坐标
  24.         public static float Mouse_Y = 0;//记录鼠标的Y坐标
  25.         //下面是初始化必需设置的变量
  26.         public static bool ChessStyle = true;//黑方还是白方,true为黑方
  27.         public static bool CGrow = true;//当前棋子的类型
  28.         public static bool DownChess = true;//对方是否下完棋
  29.         public static int CKind = -1;//设录取胜的棋种,0为白棋

  30.         private void udpSocket1_DataArrival(byte[] Data, System.Net.IPAddress Ip, int Port)
  31.         {
  32.             DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataArrival);
  33.             this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port }); //设置托管
  34.         }

  35.         private delegate void DataArrivaldelegate(byte[] Data, System.Net.IPAddress Ip, int Port);
  36.         private void DataArrival(byte[] Data, System.Net.IPAddress Ip, int Port) //当有数据到达后的处理进程
  37.         {
  38.             //将接收的消息转换成自定义集合MessClass
  39.             MessClass msg = new ClassSerializers().DeSerializeBinary((new System.IO.MemoryStream(Data))) as MessClass;
  40.             switch (msg.sendKind)//获取发送的类型
  41.             {
  42.                 case SendKind.SendConn://连接
  43.                     {
  44.                         if (msg.ChessStyle)//判断当前棋子的类型
  45.                             ChessStyle = true;//黑棋
  46.                         else
  47.                             ChessStyle = false;//白棋
  48.                         CGrow = ChessStyle;//记录当前棋子的类型
  49.                         CKind = -1;//记录取胜的棋子种类
  50.                         temMsg.sendKind = SendKind.SendConnHit;//设置消息发送类型为连接成功
  51.                         temMsg.ChessStyle = ChessStyle;//在发送消息中设置当前棋子的类型
  52.                         //向远程计算机发送消息
  53.                         udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
  54.                         break;
  55.                     }
  56.                 case SendKind.SendConnHit://连接成功
  57.                     {
  58.                         MessageBox.Show("连接成功");//显示连接成功
  59.                         button1.Tag = 1;//设置标识
  60.                         button1.Text = "重新开始";
  61.                         if (msg.ChessStyle)//如果是黑棋
  62.                         {
  63.                             ChessStyle = true;//设置本地的棋子类型为黑棋
  64.                             DownChess = true;//本地先下
  65.                             label2.Text = "黑棋";//显示本地为黑棋
  66.                         }
  67.                         else
  68.                         {
  69.                             ChessStyle = false;//设置本地的棋子类型为白棋
  70.                             DownChess = false;//本地后下
  71.                             label2.Text = "白棋";//显示本地为白棋
  72.                         }
  73.                         CGrow = ChessStyle;//记录本地的棋子类型
  74.                         panel2.Visible = false;//隐藏最后落子的标记
  75.                         break;
  76.                     }
  77.                 case SendKind.SendAfresh://重新下棋
  78.                     {
  79.                         //清空棋盘中各棋子的位置
  80.                         for (int i = 0; i < 15; i++)
  81.                             for (int j = 0; j < 15; j++)
  82.                                 note[i, j] = -1;
  83.                         Graphics g = panel1.CreateGraphics();//创健panel1控件的Graphics类
  84.                         g.DrawImage(Properties.Resources.棋盘, 0, 0, panel1.Width, panel1.Height);//清空棋盘
  85.                         if (msg.ChessStyle)//如果是黑棋
  86.                         {
  87.                             ChessStyle = true;//设置本地的棋子类型为黑棋
  88.                             DownChess = true;//设置本地的棋子类型为黑棋
  89.                             label2.Text = "黑棋";//显示本地为黑棋
  90.                         }
  91.                         else
  92.                         {
  93.                             ChessStyle = false;//设置本地的棋子类型为白棋
  94.                             DownChess = false;//本地后下
  95.                             label2.Text = "白棋";//显示本地为白棋
  96.                         }
  97.                         CGrow = ChessStyle;//记录本地的棋子类型
  98.                         CKind = -1;//记录取胜的棋子种类
  99.                         panel2.Visible = false;//隐藏最后落子的标记
  100.                         break;
  101.                     }
  102.                 case SendKind.SendChessman://接收发送的棋子
  103.                     {
  104.                         int tem_CS = -1;
  105.                         Image tem_Image;//实例化Image类
  106.                         if (msg.Grow)//如果为黑棋
  107.                         {
  108.                             tem_CS = 1;//记录棋子类型为黑棋
  109.                             CGrow = true;//记录当前为黑棋
  110.                             tem_Image = Properties.Resources.黑棋子;//存储黑棋的图片
  111.                         }
  112.                         else
  113.                         {
  114.                             tem_CS = 0;//记录棋子类型为白棋
  115.                             CGrow = false;//记录当前为黑棋
  116.                             tem_Image = Properties.Resources.白棋子;//存储白棋的图片
  117.                         }
  118.                         note[msg.ChessX, msg.ChessY] = tem_CS;//在数组中记录当前棋子的位置
  119.                         Graphics g = panel1.CreateGraphics();
  120.                         g.DrawImage(tem_Image, msg.ChessX * 35 + 7, msg.ChessY * 35 + 7, 35, 35);//在棋盘中显示对方下的棋子
  121.                         panel2.Visible = true;//显示最后落子的标记
  122.                         panel2.Location = new System.Drawing.Point(msg.ChessX * 35 + 20, msg.ChessY * 35 + 20);//将标记显示在棋子上
  123.                         DownChess = msg.Walk;//记录对方是否下完棋
  124.                         CGrow = !msg.Grow;//记录本地的棋子类型
  125.                         Arithmetic(tem_CS, msg.ChessX, msg.ChessY);//计算对方是否获胜
  126.                         DownChess = true;//对方已下完棋
  127.                         break;
  128.                     }
  129.                 case SendKind.SendCut://断开连接
  130.                     {
  131.                         temMsg.sendKind = SendKind.SendCutHit;//设置发送的类型为断开连接
  132.                         //向远程计算机发送断开消息
  133.                         udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
  134.                         button1.Text = "连接";//显当前可重新连接
  135.                         button1.Tag = 0;//设置连接标识
  136.                         break;
  137.                     }
  138.                 case SendKind.SendCutHit://断开成功
  139.                     {
  140.                         udpSocket1.Active = false;//关闭UDP的连接
  141.                         Application.Exit();//关闭当前工程
  142.                         break;
  143.                     }
  144.             }
  145.         }

  146.         private void button1_Click(object sender, EventArgs e)
  147.         {
  148.             //清空记录各棋子位置的数组
  149.             for (int i = 0; i < 15; i++)
  150.                 for (int j = 0; j < 15; j++)
  151.                     note[i, j] = -1;
  152.             CKind = -1;//记录棋子种类
  153.             if (Convert.ToInt32(((Button)sender).Tag.ToString()) == 0)//如当前为开始连接
  154.             {
  155.                 if (textBox1.Text.Trim() == "")//如果IP地址为空
  156.                 {
  157.                     MessageBox.Show("请输入IP地址。");
  158.                     return;
  159.                 }
  160.                 FrmClass.ServerIP = textBox1.Text;//记录远程计算机的IP地址
  161.                 udpSocket1.Active = false;//关闭UDP的连接
  162.                 udpSocket1.LocalPort = 11001;//设置端口号
  163.                 udpSocket1.Active = true;//打开UDP的连接
  164.                 temMsg.sendKind = SendKind.SendConn;//设置发送类型为连接
  165.                 temMsg.ChessStyle = !ChessStyle;//设置对方的棋子类型
  166.             }
  167.             if (Convert.ToInt32(((Button)sender).Tag.ToString()) == 1)//如当前为重新开始
  168.             {
  169.                 ChessStyle = !ChessStyle;//改变本地的棋子类型
  170.                 temMsg.sendKind = SendKind.SendAfresh;//设置消息类型为重新开始
  171.                 temMsg.ChessStyle = !ChessStyle;//设置远程计算机的棋子类型
  172.                 Graphics g = panel1.CreateGraphics();
  173.                 g.DrawImage(Properties.Resources.棋盘, 0, 0, panel1.Width, panel1.Height);//清空棋盘
  174.             }
  175.             if (ChessStyle)//如果本地的棋子类型为黑棋
  176.             {
  177.                 label2.Text = "黑棋";//显示本地为黑棋
  178.                 DownChess = true;
  179.             }
  180.             else
  181.             {
  182.                 label2.Text = "白棋";//显示本地为白棋
  183.                 DownChess = false;
  184.             }
  185.             CGrow = ChessStyle;//记录当前的棋子类型
  186.             panel2.Visible = false;//隐藏最后落子的标记
  187.             //将消息发送给远程计算机
  188.             udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
  189.         }

  190.         private void Form1_Load(object sender, EventArgs e)
  191.         {
  192.             //初始化记录棋子的数组
  193.             for (int i = 0; i < 15; i++)
  194.                 for (int j = 0; j < 15; j++)
  195.                     note[i, j] = -1;
  196.             FrmClass.ClientIP = frmclass.MyHostIP();//获取本地的IP地址
  197.             FrmClass.ClientPort = "11001";//获取端口号
  198.         }

  199.         private void Form1_FontChanged(object sender, EventArgs e)
  200.         {

  201.         }

  202.         private void button2_Click(object sender, EventArgs e)
  203.         {
  204.             temMsg.sendKind = SendKind.SendCut;//设置发送的消息为断开
  205.             if (FrmClass.ServerIP != "")//如果与远程计算机连接
  206.                 //将消息发送给远程计算机
  207.                 udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
  208.             if (Convert.ToInt32(button1.Tag.ToString()) == 0)//如果没有与远程计算机连接
  209.                 Application.Exit();//退出当前工程
  210.         }

  211.         private void panel1_Paint(object sender, PaintEventArgs e)
  212.         {
  213.             Image tem_Image = Properties.Resources.白棋子;//实例化Image类
  214.             e.Graphics.DrawImage(Properties.Resources.棋盘, 0, 0, panel1.Width, panel1.Height);//清空棋盘
  215.             //遍历已下完的棋子
  216.             for (int i = 0; i < 15; i++)
  217.                 for (int j = 0; j < 15; j++)
  218.                 {
  219.                     if (note[i, j] > -1)
  220.                     {
  221.                         if (note[i, j] == 0)//如果当前是白棋
  222.                             tem_Image = Properties.Resources.白棋子;//在指定的位置绘制白棋
  223.                         if (note[i, j] == 1)//如果当前是黑棋
  224.                             tem_Image = Properties.Resources.黑棋子;//在指定的位置绘制黑棋
  225.                         e.Graphics.DrawImage(tem_Image, i * 35 + 7, j * 35 + 7, 35, 35);//绘制已下完的棋子
  226.                     }
  227.                 }
  228.         }

  229.         private void panel1_Click(object sender, EventArgs e)
  230.         {
  231.             if (udpSocket1.Active == false)
  232.             {
  233.                 MessageBox.Show("没有与远程计算机建立连接!");
  234.                 return;
  235.             }
  236.             if (DownChess == false)//如果对方没有下完棋
  237.             {
  238.                 MessageBox.Show("对方没有下完棋!");
  239.                 return;
  240.             }
  241.             if (CKind > -1)//如果当前有棋子获胜
  242.             {
  243.                 if (CKind == 0)//如果白棋获胜
  244.                     Bwin();//显示白棋获胜的提示框
  245.                 if (CKind == 1)//如果黑棋获胜
  246.                     Wwin();//显示黑棋获胜的提示框
  247.                 return;
  248.             }
  249.             int Column = Convert.ToInt32(Math.Round((Mouse_X - 30) / 35));//获取当前落子的所在列
  250.             int Row = Convert.ToInt32(Math.Round((Mouse_Y - 30) / 35));//获取当前落子的所在行
  251.             if (note[Column, Row] == -1)//如果当前位置无棋子
  252.             {
  253.                 int tem_CS = -1;
  254.                 Image tem_Image;//实例化Image类
  255.                 if (CGrow)//如果当前落子为黑棋
  256.                 {
  257.                     tem_CS = 1;
  258.                     CGrow = true;//记录落子类型
  259.                     tem_Image = Properties.Resources.黑棋子;//存储黑棋图片
  260.                 }
  261.                 else
  262.                 {
  263.                     tem_CS = 0;
  264.                     CGrow = false;//记录落子类型
  265.                     tem_Image = Properties.Resources.白棋子;//存储白棋图片
  266.                 }
  267.                 note[Column, Row] = tem_CS;//记录当前位置已有棋子
  268.                 Graphics g = panel1.CreateGraphics();
  269.                 g.DrawImage(tem_Image, Column * 35 + 7, Row * 35 + 7, 35, 35);//在棋盘中显示当前下的棋子
  270.                 panel2.Visible = true;//显示最后落子的标记
  271.                 panel2.Location = new System.Drawing.Point(Column * 35 + 20, Row * 35 + 20);//在棋子上显示标记
  272.                 DownChess = false;//对方没有下棋

  273.                 temMsg.sendKind = SendKind.SendChessman;//设置发送的类型为发送棋子
  274.                 temMsg.ChessX = Column;//记录棋子所在行
  275.                 temMsg.ChessY = Row;//记录棋子所在列
  276.                 temMsg.Grow = CGrow;//记录前棋子的类型
  277.                 temMsg.Walk = true;//记录本地已下完棋
  278.                 //向远程计算机发送消息
  279.                 udpSocket1.Send(IPAddress.Parse(FrmClass.ServerIP), Convert.ToInt32(FrmClass.ClientPort), new ClassSerializers().SerializeBinary(temMsg).ToArray());
  280.                 Arithmetic(tem_CS, Column, Row);//计算本地是否获胜
  281.             }
  282.         }

  283.         private void panel1_MouseDown(object sender, MouseEventArgs e)
  284.         {
  285.             //记录鼠标的单击位置
  286.             Mouse_X = e.X;
  287.             Mouse_Y = e.Y;
  288.         }

  289.         /// <summary>
  290.         /// 算法
  291.         /// </summary>
  292.         public void Arithmetic(int n, int Arow, int Acolumn)//算法
  293.         {
  294.             int BCount = 1;
  295.             CKind = -1;//记录棋子种类
  296.             //横向查找
  297.             bool Lbol = true;
  298.             bool Rbol = true;
  299.             int jlsf = 0;
  300.             BCount = 1;
  301.             for (int i = 1; i <= 5; i++)
  302.             {
  303.                 if ((Acolumn + i) > 14)
  304.                     Rbol = false;
  305.                 if ((Acolumn - i) < 0)
  306.                     Lbol = false;
  307.                 if (Rbol == true)
  308.                 {
  309.                     if (note[Arow, Acolumn + i] == n)
  310.                         ++BCount;
  311.                     else
  312.                         Rbol = false;
  313.                 }
  314.                 if (Lbol == true)
  315.                 {
  316.                     if (note[Arow, Acolumn - i] == n)
  317.                         ++BCount;
  318.                     else
  319.                         Lbol = false;
  320.                 }
  321.                 if (BCount >= 5)
  322.                 {
  323.                     if (n == 0)
  324.                         Bwin();
  325.                     if (n == 1)
  326.                         Wwin();
  327.                     jlsf = n;
  328.                     break;
  329.                 }
  330.             }

  331.             //纵向查找
  332.             bool Ubol = true;
  333.             bool Dbol = true;
  334.             BCount = 1;
  335.             for (int i = 1; i <= 5; i++)
  336.             {
  337.                 if ((Arow + i) > 14)
  338.                     Dbol = false;
  339.                 if ((Arow - i) < 0)
  340.                     Ubol = false;
  341.                 if (Dbol == true)
  342.                 {
  343.                     if (note[Arow + i, Acolumn] == n)
  344.                         ++BCount;
  345.                     else
  346.                         Dbol = false;
  347.                 }
  348.                 if (Ubol == true)
  349.                 {
  350.                     if (note[Arow - i, Acolumn] == n)
  351.                         ++BCount;
  352.                     else
  353.                         Ubol = false;
  354.                 }
  355.                 if (BCount >= 5)
  356.                 {
  357.                     if (n == 0)
  358.                         Bwin();
  359.                     if (n == 1)
  360.                         Wwin();
  361.                     jlsf = n;
  362.                     break;
  363.                 }
  364.             }

  365.             //正斜查找
  366.             bool LUbol = true;
  367.             bool RDbol = true;
  368.             BCount = 1;
  369.             for (int i = 1; i <= 5; i++)
  370.             {
  371.                 if ((Arow - i) < 0 || (Acolumn - i < 0))
  372.                     LUbol = false;
  373.                 if ((Arow + i) > 14 || (Acolumn + i > 14))
  374.                     RDbol = false;
  375.                 if (LUbol == true)
  376.                 {
  377.                     if (note[Arow - i, Acolumn - i] == n)
  378.                         ++BCount;
  379.                     else
  380.                         LUbol = false;
  381.                 }
  382.                 if (RDbol == true)
  383.                 {
  384.                     if (note[Arow + i, Acolumn + i] == n)
  385.                         ++BCount;
  386.                     else
  387.                         RDbol = false;
  388.                 }
  389.                 if (BCount >= 5)
  390.                 {
  391.                     if (n == 0)
  392.                         Bwin();
  393.                     if (n == 1)
  394.                         Wwin();
  395.                     jlsf = n;
  396.                     break;
  397.                 }
  398.             }
  399.             //反斜查找
  400.             bool RUbol = true;
  401.             bool LDbol = true;
  402.             BCount = 1;
  403.             for (int i = 1; i <= 5; i++)
  404.             {
  405.                 if ((Arow - i) < 0 || (Acolumn + i > 14))
  406.                     RUbol = false;
  407.                 if ((Arow + i) > 14 || (Acolumn - i < 0))
  408.                     LDbol = false;
  409.                 if (RUbol == true)
  410.                 {
  411.                     if (note[Arow - i, Acolumn + i] == n)
  412.                         ++BCount;
  413.                     else
  414.                         RUbol = false;
  415.                 }
  416.                 if (LDbol == true)
  417.                 {
  418.                     if (note[Arow + i, Acolumn - i] == n)
  419.                         ++BCount;
  420.                     else
  421.                         LDbol = false;
  422.                 }
  423.                 if (BCount >= 5)
  424.                 {
  425.                     if (n == 0)
  426.                         Bwin();
  427.                     if (n == 1)
  428.                         Wwin();
  429.                     jlsf = n;
  430.                     break;
  431.                 }
  432.             }
  433.         }

  434.         /// <summary>
  435.         /// 显示白棋获胜
  436.         /// </summary>
  437.         public void Bwin()
  438.         {
  439.             CKind = 0;
  440.             MessageBox.Show("白棋获胜!");
  441.             if (ChessStyle == false)//如果当前为白棋
  442.                 label6.Text = Convert.ToString(Convert.ToInt32(label6.Text) + 1);//白棋得分
  443.             else
  444.                 label7.Text = Convert.ToString(Convert.ToInt32(label7.Text) + 1);//黑棋得分
  445.         }

  446.         /// <summary>
  447.         /// 显示黑棋获胜
  448.         /// </summary>
  449.         public void Wwin()
  450.         {
  451.             CKind = 1;
  452.             MessageBox.Show("黑棋获胜!");
  453.             if (ChessStyle == true)
  454.                 label6.Text = Convert.ToString(Convert.ToInt32(label6.Text) + 1);//黑棋得分
  455.             else
  456.                 label7.Text = Convert.ToString(Convert.ToInt32(label7.Text) + 1);//白棋得分
  457.         }
  458.     }
  459. }
复制代码

所有资料51hei提供下载:
12-游戏设计.rar (5.46 MB, 下载次数: 51)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:380389 发表于 2018-8-1 19:46 来自手机 | 只看该作者
谢谢分享好玩的东西  收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表