这应该是几个月前,闲的手痒,敲了一上午代码搞出来的,随之就把它丢弃了,当时让别人玩过,提过几条更改建议,但是时至今日,我也没有进行过优化和更改(本人只会作案,不会收场,嘎嘎),下面的建议要给代码爱好的童鞋完成了。

更改建议:

a.当数字超过四位数时,显示的时候有部分被它的容器TextBox遮挡了,能不能把显示的数值变小点?答案是可以的。代码里有一段通过矩阵数据填充TextBox值的操作,可以在填充时,判断下数值长度,然后修改TextBox的文字大小。

b.玩游戏的时候,使用方向键移动时,焦点可能没有锁定在自定义控件的画布上,能不能使用方向键移动时,锁定画布?答案是可以的,不敢说出来,因为很简单,说出来就不值得提出这个问题让Reader来做了(哈哈)。

c.还有几条,真的记不住了,唉,老亦。

  其实不难,就是多了点,杂了点,喜欢的可以拉走做毕业设计用,如果要说这里面有什么值得看的地方,我觉得应该没有吧,毕竟做的仓促,也没好好的修改。

1.应用程序的主入口点:文件名Program.cs,这个简单,可以略过

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO; namespace _2048
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
} static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "Exception.log", DateTime.Now.ToString() + "\t" + e.Exception.Message + Environment.NewLine);
} static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "Exception.log", DateTime.Now.ToString() + "\t" + e.ExceptionObject.ToString() + Environment.NewLine);
}
}
}

2.用到的数据模型:文件名Unit.cs,这个简单,可以略过

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace _2048
{
public class Unit
{
public Unit()
{ } public Unit(int num, bool bMerge)
{
this._num = num;
this._bMerge = bMerge;
} private int _num = ; public int Num
{
get { return _num; }
set { _num = value; }
} private bool _bMerge = false; public bool BMerge
{
get { return _bMerge; }
set { _bMerge = value; }
} //private int _x = 0; //public int X
//{
// get { return _x; }
// set { _x = value; }
//} //private int _y = 0; //public int Y
//{
// get { return _y; }
// set { _y = value; }
//}
}
}

3.自定义控件和算法(设计器和后台文件已合并):文件名GridControl.cs,整个程序的核心内容都在这个文件里,包括2048游戏算法、自定义控件的伸缩、方向键移动控制等等吧,后半部分不用看了,设计器自动生成的,不可或缺,没什么嚼劲。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace _2048
{
public partial class GridControl : UserControl
{
/*
* 判断结束的必要条件:
* 无论如何移动(上、下、左、右),矩阵中没有空着的位置时,结束
*
*
*1. 初始化参数,初始化数量3
*2. 每次移动,相同数字合并加分
*3. 每次移动,在移动后的随机空格位置处产生一个新数
*/
private int _x = ; public int X
{
get { return _x; }
set
{
_x = value;
CreateTextBox();
}
} private int _y = ; public int Y
{
get { return _y; }
set
{
_y = value;
CreateTextBox();
}
} private Unit[][] _matrix = null;
private Random rand = new Random(DateTime.Now.Millisecond);
private int _score = ; public int Score
{
get { return _score; }
set { _score = value; }
} public GridControl()
{
InitializeComponent();
} public void InitControl()
{
/*初始化矩阵图,以下三个方法的顺序不能颠倒*/
this.InitMatrix();
this.InitParamter();
this.InputData();
} #region MyRegion private void CreateTextBox()
{
this.flowLayoutPanel1.Controls.Clear();
this.Size = new System.Drawing.Size( * _x + , * _y + ); for (int j = ; j < _y; j++)
{
for (int i = ; i < _x; i++)
{
TextBox textBox = new TextBox();
textBox.BackColor = System.Drawing.Color.White;
textBox.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
textBox.Location = new System.Drawing.Point(, );
textBox.Name = "textBox" + i.ToString() + j.ToString();
textBox.ReadOnly = true;
textBox.Size = new System.Drawing.Size(, );
textBox.TabIndex = ;
textBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.flowLayoutPanel1.Controls.Add(textBox);
}
}
} /// <summary>
/// 初始化矩阵
/// </summary>
private void InitMatrix()
{
_matrix = new Unit[_x][]; for (int i = ; i < _x; i++)
{
_matrix[i] = new Unit[_y]; for (int j = ; j < _y; j++)
{
_matrix[i][j] = new Unit();
}
} this._score = ;
} /// <summary>
/// 初始化参数
/// </summary>
private void InitParamter()
{
//创建三个初始参数
for (int i = ; i < ; i++)
{
CreateOneNum();
}
} private int CountEmpty()
{
int iReturn = ; for (int i = ; i < _x; i++)
{
for (int j = ; j < _y; j++)
{
if (_matrix[i][j].Num == )
iReturn++;
}
} return iReturn;
} /// <summary>
/// 根据矩阵表填充显示数据
/// </summary>
private void InputData()
{
for (int i = ; i < _x; i++)
{
for (int j = ; j < _y; j++)
{
string name = "textBox" + i.ToString() + j.ToString();
Control[] ctls = this.flowLayoutPanel1.Controls.Find(name, false);
if (ctls != null)
{
switch (_matrix[i][j].Num)
{
default:
case :
{
ctls[].Text = string.Empty;
ctls[].BackColor = Color.White;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.BurlyWood;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.DeepPink;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.BlueViolet;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.Chartreuse;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.Crimson;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.DarkGray;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.DarkOliveGreen;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.DarkSalmon;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.DarkSlateGray;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.GreenYellow;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.DodgerBlue;
} break; case :
{
ctls[].Text = _matrix[i][j].Num.ToString();
ctls[].BackColor = Color.Black;
} break;
}
}
}
} } /// <summary>
/// 游戏是否不能进行
/// </summary>
/// <returns>是否能进行</returns>
private bool CheckGameOver()
{
int countEmpty = CountEmpty();
if (countEmpty == )
{
for (int i = ; i < _x; i++)
{
Unit[] arr = new Unit[_y]; for (int j = ; j < _y; j++)
arr[j] = new Unit(_matrix[i][j].Num, false); if (Result(ref arr, true))
return true;
} for (int i = ; i < _x; i++)
{
Unit[] arr = new Unit[_y]; for (int j = _y - ; j >= ; j--)
arr[_y - - j] = new Unit(_matrix[i][j].Num, false); if (Result(ref arr, true))
return true;
} for (int i = ; i < _y; i++)
{
Unit[] arr = new Unit[_x]; for (int j = ; j < _x; j++)
arr[j] = new Unit(_matrix[j][i].Num, false); if (Result(ref arr, true))
return true;
} for (int i = ; i < _y; i++)
{
Unit[] arr = new Unit[_x]; for (int j = _x - ; j >= ; j--)
arr[_x - - j] = new Unit(_matrix[j][i].Num, false); if (Result(ref arr, true))
return true;
}
return false;
} return true;
} private void CreateOneNum()
{
int countEmpty = CountEmpty();
int pos = rand.Next(countEmpty);
int count = ; for (int i = ; i < _x; i++)
{
for (int j = ; j < _y; j++)
{
if (_matrix[i][j].Num == )
{
if (count == pos)
{
int newNum = ;
if (rand.Next() < )
newNum = ;
else
newNum = ;
_matrix[i][j].Num = newNum; return;
}
count++;
}
}
}
} /// <summary>
/// 核心算法
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
private bool Result(ref Unit[] arr, bool istest)
{
bool bReturn = false; while (true)
{
//是否发生移动
bool bMove = false; //前一个单元
Unit unit = arr[]; for (int i = ; i < arr.Length; i++)
{
//如果前面位置为空,当前位置不为空,把当前位置移入前面位置
if (unit.Num == && arr[i].Num > )
{
//移动到前一个单元
unit.Num = arr[i].Num;
unit.BMerge = arr[i].BMerge; //当前单元清空
arr[i].Num = ;
arr[i].BMerge = false; //移动了才能进入循环
if (!bMove)
{
bMove = true;
if (!bReturn)
bReturn = true;
}
} //如果前面不为空,当前不为空,前面和当前的数字相同并且都没有进行过合并
else if (unit.Num > && arr[i].Num >
&& !unit.BMerge && !arr[i].BMerge
&& unit.Num == arr[i].Num)
{
//前一个单元合并操作
unit.Num = unit.Num * ;
unit.BMerge = true; if (!istest)
_score += unit.Num; //当前单元清空
arr[i].Num = ;
arr[i].BMerge = false; //移动了才能进入循环
if (!bMove)
{
bMove = true;
if (!bReturn)
bReturn = true;
}
} unit = arr[i];
}
if (!bMove)
break;
} return bReturn;
} #endregion internal void MoveUp()
{
bool bMove = false; for (int i = ; i < _x; i++)
{
Unit[] arr = new Unit[_y]; for (int j = ; j < _y; j++)
{
arr[j] = _matrix[i][j];
arr[j].BMerge = false;
}
if (Result(ref arr, false) && !bMove)
bMove = true;
}
if (bMove)
{ //创建一个新数
CreateOneNum();
} //显示
InputData(); if (!CheckGameOver())
MessageBox.Show(" Score: " + _score + "\r\n Game Over!!!");
} internal void MoveDown()
{
bool bMove = false; for (int i = ; i < _x; i++)
{
Unit[] arr = new Unit[_y]; for (int j = _y - ; j >= ; j--)
{
arr[_y - - j] = _matrix[i][j];
arr[_y - - j].BMerge = false;
}
if (Result(ref arr, false) && !bMove)
bMove = true;
} if (bMove)
{ //创建一个新数
CreateOneNum();
}
//显示
InputData(); if (!CheckGameOver())
MessageBox.Show(" Score: " + _score + "\r\n Game Over!!!");
} internal void MoveLeft()
{
bool bMove = false; for (int i = ; i < _y; i++)
{
Unit[] arr = new Unit[_x]; for (int j = ; j < _x; j++)
{
arr[j] = _matrix[j][i];
arr[j].BMerge = false;
}
if (Result(ref arr, false) && !bMove)
bMove = true;
} if (bMove)
{ //创建一个新数
CreateOneNum();
}
//显示
InputData(); if (!CheckGameOver())
MessageBox.Show(" Score: " + _score + "\r\n Game Over!!!");
} internal void MoveRight()
{
bool bMove = false; for (int i = ; i < _y; i++)
{
Unit[] arr = new Unit[_x]; for (int j = _x - ; j >= ; j--)
{
arr[_x - - j] = _matrix[j][i];
arr[_x - - j].BMerge = false;
}
if (Result(ref arr, false) && !bMove)
bMove = true;
} if (bMove)
{ //创建一个新数
CreateOneNum();
}
//显示
InputData(); if (!CheckGameOver())
MessageBox.Show(" Score: " + _score + "\r\n Game Over!!!");
} internal void Back()
{ } private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} private void InitializeComponent()
{
this.textBox00 = new System.Windows.Forms.TextBox();
this.textBox10 = new System.Windows.Forms.TextBox();
this.textBox20 = new System.Windows.Forms.TextBox();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.textBox30 = new System.Windows.Forms.TextBox();
this.textBox01 = new System.Windows.Forms.TextBox();
this.textBox11 = new System.Windows.Forms.TextBox();
this.textBox21 = new System.Windows.Forms.TextBox();
this.textBox31 = new System.Windows.Forms.TextBox();
this.textBox02 = new System.Windows.Forms.TextBox();
this.textBox12 = new System.Windows.Forms.TextBox();
this.textBox22 = new System.Windows.Forms.TextBox();
this.textBox32 = new System.Windows.Forms.TextBox();
this.textBox03 = new System.Windows.Forms.TextBox();
this.textBox13 = new System.Windows.Forms.TextBox();
this.textBox23 = new System.Windows.Forms.TextBox();
this.textBox33 = new System.Windows.Forms.TextBox();
this.flowLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// textBox00
//
this.textBox00.BackColor = System.Drawing.Color.White;
this.textBox00.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox00.Location = new System.Drawing.Point(, );
this.textBox00.Name = "textBox00";
this.textBox00.ReadOnly = true;
this.textBox00.Size = new System.Drawing.Size(, );
this.textBox00.TabIndex = ;
this.textBox00.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox10
//
this.textBox10.BackColor = System.Drawing.Color.White;
this.textBox10.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox10.Location = new System.Drawing.Point(, );
this.textBox10.Name = "textBox10";
this.textBox10.ReadOnly = true;
this.textBox10.Size = new System.Drawing.Size(, );
this.textBox10.TabIndex = ;
this.textBox10.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox20
//
this.textBox20.BackColor = System.Drawing.Color.White;
this.textBox20.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox20.Location = new System.Drawing.Point(, );
this.textBox20.Name = "textBox20";
this.textBox20.ReadOnly = true;
this.textBox20.Size = new System.Drawing.Size(, );
this.textBox20.TabIndex = ;
this.textBox20.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Controls.Add(this.textBox00);
this.flowLayoutPanel1.Controls.Add(this.textBox10);
this.flowLayoutPanel1.Controls.Add(this.textBox20);
this.flowLayoutPanel1.Controls.Add(this.textBox30);
this.flowLayoutPanel1.Controls.Add(this.textBox01);
this.flowLayoutPanel1.Controls.Add(this.textBox11);
this.flowLayoutPanel1.Controls.Add(this.textBox21);
this.flowLayoutPanel1.Controls.Add(this.textBox31);
this.flowLayoutPanel1.Controls.Add(this.textBox02);
this.flowLayoutPanel1.Controls.Add(this.textBox12);
this.flowLayoutPanel1.Controls.Add(this.textBox22);
this.flowLayoutPanel1.Controls.Add(this.textBox32);
this.flowLayoutPanel1.Controls.Add(this.textBox03);
this.flowLayoutPanel1.Controls.Add(this.textBox13);
this.flowLayoutPanel1.Controls.Add(this.textBox23);
this.flowLayoutPanel1.Controls.Add(this.textBox33);
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(, );
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Padding = new System.Windows.Forms.Padding();
this.flowLayoutPanel1.Size = new System.Drawing.Size(, );
this.flowLayoutPanel1.TabIndex = ;
//
// textBox30
//
this.textBox30.BackColor = System.Drawing.Color.White;
this.textBox30.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox30.Location = new System.Drawing.Point(, );
this.textBox30.Name = "textBox30";
this.textBox30.ReadOnly = true;
this.textBox30.Size = new System.Drawing.Size(, );
this.textBox30.TabIndex = ;
this.textBox30.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox01
//
this.textBox01.BackColor = System.Drawing.Color.White;
this.textBox01.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox01.Location = new System.Drawing.Point(, );
this.textBox01.Name = "textBox01";
this.textBox01.ReadOnly = true;
this.textBox01.Size = new System.Drawing.Size(, );
this.textBox01.TabIndex = ;
this.textBox01.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox11
//
this.textBox11.BackColor = System.Drawing.Color.White;
this.textBox11.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox11.Location = new System.Drawing.Point(, );
this.textBox11.Name = "textBox11";
this.textBox11.ReadOnly = true;
this.textBox11.Size = new System.Drawing.Size(, );
this.textBox11.TabIndex = ;
this.textBox11.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox21
//
this.textBox21.BackColor = System.Drawing.Color.White;
this.textBox21.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox21.Location = new System.Drawing.Point(, );
this.textBox21.Name = "textBox21";
this.textBox21.ReadOnly = true;
this.textBox21.Size = new System.Drawing.Size(, );
this.textBox21.TabIndex = ;
this.textBox21.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox31
//
this.textBox31.BackColor = System.Drawing.Color.White;
this.textBox31.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox31.Location = new System.Drawing.Point(, );
this.textBox31.Name = "textBox31";
this.textBox31.ReadOnly = true;
this.textBox31.Size = new System.Drawing.Size(, );
this.textBox31.TabIndex = ;
this.textBox31.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox02
//
this.textBox02.BackColor = System.Drawing.Color.White;
this.textBox02.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox02.Location = new System.Drawing.Point(, );
this.textBox02.Name = "textBox02";
this.textBox02.ReadOnly = true;
this.textBox02.Size = new System.Drawing.Size(, );
this.textBox02.TabIndex = ;
this.textBox02.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox12
//
this.textBox12.BackColor = System.Drawing.Color.White;
this.textBox12.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox12.Location = new System.Drawing.Point(, );
this.textBox12.Name = "textBox12";
this.textBox12.ReadOnly = true;
this.textBox12.Size = new System.Drawing.Size(, );
this.textBox12.TabIndex = ;
this.textBox12.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox22
//
this.textBox22.BackColor = System.Drawing.Color.White;
this.textBox22.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox22.Location = new System.Drawing.Point(, );
this.textBox22.Name = "textBox22";
this.textBox22.ReadOnly = true;
this.textBox22.Size = new System.Drawing.Size(, );
this.textBox22.TabIndex = ;
this.textBox22.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox32
//
this.textBox32.BackColor = System.Drawing.Color.White;
this.textBox32.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox32.Location = new System.Drawing.Point(, );
this.textBox32.Name = "textBox32";
this.textBox32.ReadOnly = true;
this.textBox32.Size = new System.Drawing.Size(, );
this.textBox32.TabIndex = ;
this.textBox32.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox03
//
this.textBox03.BackColor = System.Drawing.Color.White;
this.textBox03.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox03.Location = new System.Drawing.Point(, );
this.textBox03.Name = "textBox03";
this.textBox03.ReadOnly = true;
this.textBox03.Size = new System.Drawing.Size(, );
this.textBox03.TabIndex = ;
this.textBox03.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox13
//
this.textBox13.BackColor = System.Drawing.Color.White;
this.textBox13.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox13.Location = new System.Drawing.Point(, );
this.textBox13.Name = "textBox13";
this.textBox13.ReadOnly = true;
this.textBox13.Size = new System.Drawing.Size(, );
this.textBox13.TabIndex = ;
this.textBox13.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox23
//
this.textBox23.BackColor = System.Drawing.Color.White;
this.textBox23.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox23.Location = new System.Drawing.Point(, );
this.textBox23.Name = "textBox23";
this.textBox23.ReadOnly = true;
this.textBox23.Size = new System.Drawing.Size(, );
this.textBox23.TabIndex = ;
this.textBox23.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// textBox33
//
this.textBox33.BackColor = System.Drawing.Color.White;
this.textBox33.Font = new System.Drawing.Font("宋体", 42F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.textBox33.Location = new System.Drawing.Point(, );
this.textBox33.Name = "textBox33";
this.textBox33.ReadOnly = true;
this.textBox33.Size = new System.Drawing.Size(, );
this.textBox33.TabIndex = ;
this.textBox33.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// GridControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "GridControl";
this.Size = new System.Drawing.Size(, );
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
this.ResumeLayout(false); }
private System.Windows.Forms.TextBox textBox20;
private System.Windows.Forms.TextBox textBox10;
private System.Windows.Forms.TextBox textBox00;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.TextBox textBox30;
private System.Windows.Forms.TextBox textBox01;
private System.Windows.Forms.TextBox textBox11;
private System.Windows.Forms.TextBox textBox21;
private System.Windows.Forms.TextBox textBox31;
private System.Windows.Forms.TextBox textBox02;
private System.Windows.Forms.TextBox textBox12;
private System.Windows.Forms.TextBox textBox22;
private System.Windows.Forms.TextBox textBox32;
private System.Windows.Forms.TextBox textBox03;
private System.Windows.Forms.TextBox textBox13;
private System.Windows.Forms.TextBox textBox23;
private System.Windows.Forms.TextBox textBox33; }
}

4.Form(设计器和后台文件已合并):事件绑定(可以瞅瞅)、布局大小选择、事件传递到内部等等。

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; namespace _2048
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); this.comboBox1.SelectedIndex = ; this.gridControl1.InitControl();
this.txtScore.Text = gridControl1.Score.ToString(); BindKeyDown(this);
} private void BindKeyDown(Control control)
{
control.KeyDown += new KeyEventHandler(ControlKeyDown); foreach (Control ctl in control.Controls)
{
BindKeyDown(ctl);
}
} private void ControlKeyDown(object sender, KeyEventArgs e)
{
lock (this)
{
switch (e.KeyCode)
{
case Keys.Up:
gridControl1.MoveUp();
this.txtScore.Text = gridControl1.Score.ToString();
break; case Keys.Down:
gridControl1.MoveDown();
this.txtScore.Text = gridControl1.Score.ToString();
break; case Keys.Left:
gridControl1.MoveLeft();
this.txtScore.Text = gridControl1.Score.ToString();
break; case Keys.Right:
gridControl1.MoveRight();
this.txtScore.Text = gridControl1.Score.ToString();
break; case Keys.Back:
gridControl1.Back();
this.txtScore.Text = gridControl1.Score.ToString();
break;
}
}
} private void button1_Click(object sender, EventArgs e)
{
this.gridControl1.InitControl();
this.txtScore.Text = gridControl1.Score.ToString();
} private void PaintControl(int x, int y)
{
this.gridControl1.X = x;
this.gridControl1.Y = y;
this.gridControl1.InitControl();
this.txtScore.Text = gridControl1.Score.ToString();
this.Size = new Size( * x + , * y + ); foreach (Control ctl in gridControl1.Controls)
BindKeyDown(ctl);
} private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "自定义")
{
int x = ;
int y = ; if (!int.TryParse(textBoxX.Text, out x) || x < )
x = ; if (!int.TryParse(textBoxY.Text, out y) || y < )
y = ; PaintControl(x, y);
}
else
{
switch (comboBox1.Text)
{
default:
case "4 * 4":
PaintControl(, );
break; case "5 * 5":
PaintControl(, );
break; case "4 * 5":
PaintControl(, );
break; case "5 * 4":
PaintControl(, );
break; case "6 * 6":
PaintControl(, );
break;
}
}
} private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.txtScore = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.gridControl1 = new _2048.GridControl();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBoxX = new System.Windows.Forms.TextBox();
this.textBoxY = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "New Game";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtScore
//
this.txtScore.BackColor = System.Drawing.Color.White;
this.txtScore.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.txtScore.Location = new System.Drawing.Point(, );
this.txtScore.Name = "txtScore";
this.txtScore.ReadOnly = true;
this.txtScore.Size = new System.Drawing.Size(, );
this.txtScore.TabIndex = ;
this.txtScore.Text = "";
this.txtScore.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// label9
//
this.label9.AutoSize = true;
this.label9.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.label9.Location = new System.Drawing.Point(, );
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(, );
this.label9.TabIndex = ;
this.label9.Text = "Score:";
//
// gridControl1
//
this.gridControl1.Location = new System.Drawing.Point(, );
this.gridControl1.Name = "gridControl1";
this.gridControl1.Score = ;
this.gridControl1.Size = new System.Drawing.Size(, );
this.gridControl1.TabIndex = ;
this.gridControl1.X = ;
this.gridControl1.Y = ;
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"4 * 4",
"5 * 5",
"4 * 5",
"5 * 4",
"6 * 6",
"自定义"});
this.comboBox1.Location = new System.Drawing.Point(, );
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(, );
this.comboBox1.TabIndex = ;
//
// button2
//
this.button2.Location = new System.Drawing.Point(, );
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(, );
this.button2.TabIndex = ;
this.button2.Text = "重绘布局";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "布局样式";
//
// textBoxX
//
this.textBoxX.Location = new System.Drawing.Point(, );
this.textBoxX.Name = "textBoxX";
this.textBoxX.Size = new System.Drawing.Size(, );
this.textBoxX.TabIndex = ;
//
// textBoxY
//
this.textBoxY.Location = new System.Drawing.Point(, );
this.textBoxY.Name = "textBoxY";
this.textBoxY.Size = new System.Drawing.Size(, );
this.textBoxY.TabIndex = ;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "x";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "y";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(, );
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(, );
this.label4.TabIndex = ;
this.label4.Text = "自定义布局";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBoxY);
this.Controls.Add(this.textBoxX);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.gridControl1);
this.Controls.Add(this.txtScore);
this.Controls.Add(this.label9);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "2048小游戏";
this.ResumeLayout(false);
this.PerformLayout(); } private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtScore;
private System.Windows.Forms.Label label9;
private GridControl gridControl1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxX;
private System.Windows.Forms.TextBox textBoxY;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
}
}

修改一下,做个补充,下次有时间,我会发个flash做的打飞机小游戏,是真正的打飞机,不要想歪了,呵呵。

C# 开发2048小游戏的更多相关文章

  1. 【Winform开发2048小游戏】

    先来看一下界面: 游戏帮助类 class GameCore { //游戏地图 private int[,] map = new int[4, 4]; //合并时用到的临时数组 private int[ ...

  2. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  3. Swift实战之2048小游戏

    上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...

  4. 如何在CentOS上安装一个2048小游戏

    如何在centos上安装一个2048小游戏 最近在学习CentOS系统,就琢磨着玩点什么,然后我看到有人在玩2048小游戏,所有我就在想,为啥不装一个2048小游戏搞一下嘞,于是乎,我就开始工作啦 由 ...

  5. js、jQuery实现2048小游戏

    2048小游戏 一.游戏简介:  2048是一款休闲益智类的数字叠加小游戏 二. 游戏玩法: 在4*4的16宫格中,您可以选择上.下.左.右四个方向进行操作,数字会按方向移动,相邻的两个数字相同就会合 ...

  6. 用js实现2048小游戏

    用js实现2048小游戏 笔记仓库:https://github.com/nnngu/LearningNotes 1.游戏简介 2048是一款休闲益智类的数字叠加小游戏.(文末给出源代码和演示地址) ...

  7. 2048小游戏代码解析 C语言版

    2048小游戏,也算是风靡一时的益智游戏.其背后实现的逻辑比较简单,代码量不算多,而且趣味性强,适合作为有语言基础的童鞋来加强编程训练.本篇分析2048小游戏的C语言实现代码. 前言 游戏截图:  游 ...

  8. 【转】利用 three.js 开发微信小游戏的尝试

    前言 这是一次利用 three.js 开发微信小游戏的尝试,并不能算作是教程,只能算是一篇笔记吧. 微信 WeChat 6.6.1 开始引入了微信小游戏,初期上线了一批质量相当不错的小游戏.我在查阅各 ...

  9. 使用Laya引擎开发微信小游戏(上)

    本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...

随机推荐

  1. Asp.Net 利用反射获得委托和事件以及创建委托实例和添加事件处理程序

    子程序定义: public delegate void CurrentControlListenEvent(string uniqueID, string way = null); public ev ...

  2. IE9控件安装方法

    打开上传页面,IE提示安装控件,点击安装   刷新网页,点击允许运行加载项,需要允许两次

  3. A List of Social Tagging Datasets Made Available for Research

    This list is not exhaustive - help expand it! Social Tagging Systems Research Group Source Year Obta ...

  4. WPF 图片显示中的保留字符问题

    在WPF中显示一张图片,本是一件再简单不过的事情.一张图片,一行XAML代码即可. 但是前段时间遇到了一件奇怪的事: 开发机上运行正常的程序,在某些客户机器上却显示不了图片,而且除了这个问题,其它运行 ...

  5. 关于MFC文本框输入内容的获取 与 设置文本框的内容

    八月要开始做界面了<( ̄︶ ̄)/,然而目前只会用MFC╮(╯▽╰)╭ 好吧,言归正传,设置好文本框后,要获取用户输入的内容,可以用: GetDlgItemText() ; 这个函数有两个参数,第 ...

  6. 利用GBDT模型构造新特征

    [本文链接:http://www.cnblogs.com/breezedeus/p/4109480.html,转载请注明出处] 我的博客主营地迁至github,欢迎朋友们有空去看看:http://br ...

  7. HTML和XHTML的一点事儿.

    什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (ma ...

  8. java调用Linux命令报错:java.io.IOException: Cannot run program "ps": CreateProcess error=2, ?????????

    在idea里面,java代码:Runtime.getRuntime().exec("ps -aux") 是因为默认是用windows平台运行了,所以报错,得改成调用Linux平台运 ...

  9. [原] XAF How to Edit multiple objects in a ListViewAndDetailView

    2014年好久没有更新Blog了,工作调换了,很少用XAF,但还是很关注XAF的发展和学习,对中国的中小企业数据管理软件开发真的太实用了!! 功能比较简单,但很实用,直接上图和代码! ListView ...

  10. Android ------ handler 异步处理消息

    Handler基本概念: Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,而另外一个部分逐个的在消息队列中将消息取出,然后对消息进行出来,就是发 ...