实例介绍
【实例简介】
C#双人五子棋小游戏源代码(完整版) 仅供学习交流使用,请勿用于商业用途
【实例截图】

【核心代码】
public partial class Form1 : Form
{
string bzw = "0";
string ks = "0";
string bh = "";
DateTime sj = new DateTime();
wzqDB wzqDB = new wzqDB();
Color col = Color.FromArgb(255,185,0);
string jmyi = "";
int ylcs = 0;
int gb = 0;
int fsjm = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Color col1 = Color.FromArgb(255, 165, 0);
menuStrip1.BackColor = col1;
label5.BackColor = col1;
Bitmap bmap;Graphics gph;
GraphicsInitial(out gph, out bmap);
this.ControlBox = false;
ylcs = wzqDB.ReturnCS();
}
//鼠标点击事件,在鼠标点击区域绘制五子棋图标
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (groupBox1.Visible == true)
{
return;
}
if (ks == "0")
{
return;
}
DataTable dttxt = wzqDB.readwzqDB_jsbz();
DataTable dtstrcmd = wzqDB.datatable_select1(dttxt, "bh='" bh "' and jsbz='1'");
if (dtstrcmd.Rows.Count > 0)
{
MessageBox.Show("该回合已分胜负,不能在继续游戏!", "温馨提示");
return;
}
DataTable txtsj = wzqDB.readwzqDB_sj();
DataTable dtsj = wzqDB.datatable_select2(txtsj, "sj='" sj "'", "sj desc");
if (dtsj.Rows.Count == 0)
wzqDB.writewzqDB_sj(bh "|" sj);
Bitmap bmap; Graphics gph;
GraphicsInitialPic(out gph, out bmap);
DataTable dttxt1 = wzqDB.readwzqDB();
DataTable dt1 = wzqDB.datatable_select1(dttxt1, "bh='" bh "'");
GraphicsInitialDraw(ref gph, ref bmap, dt1);
int xzb = 0;
int yzb = 0;
wzqDB.XYvalue(e.X, e.Y, ref xzb, ref yzb);
if (xzb < 50 || xzb > 950 || yzb < 20 || yzb > 620)
{
return;
}
DataTable dt2 = wzqDB.datatable_select1(dttxt1, "hzb='" xzb "' and zzb='" yzb "' and bh='" bh "'");
if (dt2.Rows.Count > 0)
{
pictureBox1.Image = bmap;
return;
}
GraphicsInitialDrawWZQ(ref gph, ref bmap, xzb, yzb);
}
private void panduan(int xval, int yval)
{
string yj = wzqDB.panduan(xval, yval, bzw, bh);
if (yj == "1")
{}
else
{
MessageBox.Show(yj,"温馨提示");
}
}
//开始或重新开始按钮事件
private void 开始游戏ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (groupBox1.Visible == true)
return;
menuStrip1.Items[0].Text = "重新开始";
Bitmap bmap; Graphics gph;
GraphicsInitialPicTime(out gph, out bmap);
}
//游戏记录按钮事件
private void 游戏记录ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
checkedListBox1.BackColor = Color.YellowGreen;
groupBox1.BackColor = Color.YellowGreen;
checkedListBox1.Items.Clear();
DataTable txtsj = wzqDB.readwzqDB_sj();
DataTable dt1 = wzqDB.datatable_select2(txtsj, "","sj desc");
if (dt1.Rows.Count > 0)
{
for (int i = 0; i < dt1.Rows.Count; i )
{
if (dt1.Rows[i]["sj"].ToString() != "")
{
checkedListBox1.Items.Add(dt1.Rows[i]["sj"].ToString());
}
}
groupBox1.Visible = true;
}
else
{
MessageBox.Show("目前没有游戏记录", "温馨提示");
}
}
//游戏记录界面中的继续游戏按钮事件
private void label1_Click(object sender, EventArgs e)
{
if (checkedListBox1.CheckedItems.Count > 1 || checkedListBox1.CheckedItems.Count == 0)
{
MessageBox.Show("请选择一个游戏开始!", "温馨提示");
return;
}
groupBox1.Visible = false;
string sj1 = checkedListBox1.CheckedItems[0].ToString();
DateTime sj2 = Convert.ToDateTime(checkedListBox1.CheckedItems[0]);
sj = sj2;
DataTable txtsj = wzqDB.readwzqDB_sj();
DataTable dt1 = wzqDB.datatable_select1(txtsj, "sj='" sj2 "'");
if (dt1.Rows.Count > 0)
{
int re = 0;
int gree = 0;
bh = dt1.Rows[0]["bh"].ToString();
DataTable dttxtjsbz = wzqDB.readwzqDB_jsbz();
DataTable dtstrcmd = wzqDB.datatable_select1(dttxtjsbz, "bh='" bh "' and jsbz='1'");
DataTable dttxt = wzqDB.readwzqDB();
DataTable dt2 = wzqDB.datatable_select1(dttxt, "bh='" bh "'");
for (int i = 0; i < dt2.Rows.Count; i )
{
if (dt2.Rows[i]["bzw"].ToString() == "0")
{
re = re 1;
}
else
{
gree = gree 1;
}
}
if (dtstrcmd.Rows.Count == 0)
{
if (re - gree > 0)
{
bzw = "1";
MessageBox.Show("游戏开始,请黑方先下!", "温馨提示");
}
else
{
bzw = "0";
MessageBox.Show("游戏开始,请红方先下!", "温馨提示");
}
}
ks = "1";
Bitmap bmap; Graphics gph;
GraphicsInitialPic(out gph, out bmap);
GraphicsInitialDraw(ref gph, ref bmap, dt2);
pictureBox1.Image = bmap;
}
}
//游戏记录界面中的新游戏按钮事件
private void label2_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
menuStrip1.Items[0].Text = "重新开始";
Bitmap bmap; Graphics gph;
GraphicsInitialPicTime(out gph, out bmap);
}
//游戏记录界面中的返回按钮事件
private void label3_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
}
//游戏记录界面中的清空按钮事件
private void label4_Click(object sender, EventArgs e)
{
string filePath = AppDomain.CurrentDomain.BaseDirectory;
int index = filePath.IndexOf("bin");
filePath = filePath.Substring(0, index);
string filePath2 = filePath "wzq.wzq";
File.Delete(filePath2);
FileStream fs= File.Create(filePath2);
fs.Close();
string filePath1=filePath "wzqjzbz.wzq";
File.Delete(filePath1);
FileStream fs1=File.Create(filePath1);
fs1.Close();
string filePath3 = filePath "wzqsj.wzq";
File.Delete(filePath3);
FileStream fs2=File.Create(filePath3);
fs2.Close();
MessageBox.Show("游戏记录清空成功!", "温馨提示");
groupBox1.Visible = false;
Graphics gh;Bitmap bm;
GraphicsInitial(out gh, out bm);
return;
}
//退出游戏按钮事件
private void 退出游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
//隐藏游戏按钮事件
private void 隐藏游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
gb = 1;
if (ylcs == 0 && fsjm ==0)
{
richTextBox1.Text = "";
label6.Text = "第一次玩该游戏,请写下您的建议吧,谢谢!";
groupBox2.Visible = true;
}
else
this.Close();
}
//关闭按钮事件
private void label5_Click(object sender, EventArgs e)
{
gb = 1;
if (ylcs == 0 && fsjm == 0)
{
richTextBox1.Text = "";
label6.Text = "第一次玩该游戏,请写下您的建议吧,谢谢!";
groupBox2.Visible = true;
}
else
this.Close();
}
//意见反馈按钮事件
private void 意见反馈ToolStripMenuItem_Click(object sender, EventArgs e)
{
fsjm = 1;
if (gb == 1)
{
label6.Text = "第一次玩该游戏,请写下您的建议吧,谢谢!";
}
else
{
label6.Text = "请在这里写下您的建议,非常感谢!";
}
groupBox2.Visible = true;
richTextBox1.Text = "";
}
//发送意见按钮事件
private void label7_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Trim() != "")
{
jmyi = richTextBox1.Text.Trim();
}
else
{
MessageBox.Show("请您填入建议后再发送,谢谢!","温馨提示");
return;
}
wzqDB.SendJYToEmail(jmyi);
groupBox2.Visible = false;
if (gb == 1)
this.Close();
}
//绘制函数
private void GraphicsInitial(out Graphics gph, out Bitmap bmap)
{
bmap = new Bitmap(1000, 1000);//图片大小
gph = Graphics.FromImage(bmap);
gph.SmoothingMode = SmoothingMode.AntiAlias;
gph.Clear(col);
gph.SmoothingMode = SmoothingMode.AntiAlias;
pictureBox1.Image = bmap;
}
private void GraphicsInitialPic(out Graphics gph, out Bitmap bmap)
{
bmap = new Bitmap(1000, 1000);//图片大小
gph = Graphics.FromImage(bmap);
gph.SmoothingMode = SmoothingMode.AntiAlias;
gph.Clear(col);
gph.SmoothingMode = SmoothingMode.AntiAlias;
for (int i = 0; i <= 20; i )
{
gph.DrawLine(Pens.Black, 50, 20 30 * i, 950, 20 30 * i);
}
for (int j = 0; j <= 30; j )
{
gph.DrawLine(Pens.Black, 50 30 * j, 20, 50 30 * j, 620);
}
}
private void GraphicsInitialPicTime(out Graphics gph, out Bitmap bmap)
{
bmap = new Bitmap(1000, 1000);//图片大小
gph = Graphics.FromImage(bmap);
gph.SmoothingMode = SmoothingMode.AntiAlias;
gph.Clear(col);
gph.SmoothingMode = SmoothingMode.AntiAlias;
//画网格线
for (int i = 0; i <= 20; i )
{
gph.DrawLine(Pens.Black, 50, 20 30 * i, 950, 20 30 * i);
}
for (int j = 0; j <= 30; j )
{
gph.DrawLine(Pens.Black, 50 30 * j, 20, 50 30 * j, 620);
}
pictureBox1.Image = bmap;
ks = "1";
sj = DateTime.Now;
bh = DateTime.Now.Year.ToString() DateTime.Now.Month.ToString() DateTime.Now.Day.ToString() DateTime.Now.Hour.ToString() DateTime.Now.Minute.ToString() DateTime.Now.Second.ToString();
}
private void GraphicsInitialDraw(ref Graphics gph, ref Bitmap bmap, DataTable dt1)
{
if (dt1.Rows.Count > 0)
{
for (int m = 0; m < dt1.Rows.Count; m )
{
if (dt1.Rows[m]["bzw"].ToString() == "0")
{
gph.DrawEllipse(Pens.Red, Convert.ToInt16(dt1.Rows[m]["hzb"].ToString()) - 7, Convert.ToInt16(dt1.Rows[m]["zzb"].ToString()) - 7, 15, 15);
gph.FillEllipse(new SolidBrush(Color.Red), Convert.ToInt16(dt1.Rows[m]["hzb"].ToString()) - 7, Convert.ToInt16(dt1.Rows[m]["zzb"].ToString()) - 7, 15, 15);
}
else
{
gph.DrawEllipse(Pens.Black, Convert.ToInt16(dt1.Rows[m]["hzb"].ToString()) - 7, Convert.ToInt16(dt1.Rows[m]["zzb"].ToString()) - 7, 15, 15);
gph.FillEllipse(new SolidBrush(Color.Black), Convert.ToInt16(dt1.Rows[m]["hzb"].ToString()) - 7, Convert.ToInt16(dt1.Rows[m]["zzb"].ToString()) - 7, 15, 15);
}
}
}
}
private void GraphicsInitialDrawWZQ(ref Graphics gph, ref Bitmap bmap,int xzb,int yzb)
{
Pen penrb; Color colorrb;
if (bzw == "0")
{
penrb = Pens.Red;
colorrb = Color.Red;
}
else
{
penrb = Pens.Black;
colorrb = Color.Black;
}
gph.DrawEllipse(penrb, xzb - 7, yzb - 7, 15, 15);
gph.FillEllipse(new SolidBrush(colorrb), xzb - 7, yzb - 7, 15, 15);
pictureBox1.Image = bmap;
string txtstring = xzb "|" yzb "|" bzw "|" bh "|" DateTime.Now;
wzqDB.writewzqDB(txtstring);
panduan(xzb, yzb);
if (bzw == "0")
{
bzw = "1";
}
else
{
bzw = "0";
}
}
}
标签: 游戏
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论