实例介绍
【实例简介】
推箱子游戏,里面有很多关,仅供参考学习
【实例截图】

【核心代码】
private void Form1_Load(object sender, EventArgs e)
{
button3.Enabled = curlvl > 0;
button4.Enabled = curlvl < Config.Levels.Length - 1;
//Controls.Clear();
lblLvl.Text = string.Format("{0:000}", curlvl 1);
step=0;
lblStep.Text = string.Format("{0:000}", step);
data = new int[16, 16];
if(items==null)
items = new ucItem[16, 16];
string[] lvl = Config.Levels[curlvl].Split(",".ToCharArray());
for (int i = 0; i < lvl.Length; i )
{
string tmp = lvl[i];
for (int j = 0; j < tmp.Length; j )
{
ucItem item=null;
if (items[i, j] == null)
{
item = new ucItem();
items[i, j] = item;
}
else
{
item = items[i, j];
}
data[i, j] = int.Parse(tmp[j].ToString());
item.Tag = data[i, j].ToString();
item.OldValue = data[i, j];
item.Left = j * item.Width 5;
item.Top = i* item.Height 5;
this.Controls.Add(item);
item.RefImage();
if (data[i, j] >= 6)
{
curPoint = new Point();
curPoint.Y = i;
curPoint.X = j;
}
}
}
ShowInfo();
textBox1.Focus();
}
private void ShowInfo()
{
textBox1.Text = "x:" curPoint.X ",y:" curPoint.Y;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Point newPoint1=curPoint;
Point newPoint2 = curPoint;
int key = e.KeyValue;
switch (key)
{
case 38://上
newPoint1.Y--;
newPoint2.Y -= 2;
data[curPoint.Y, curPoint.X] = 6;
GoNewPoint(newPoint1,newPoint2);
break;
case 39://右
newPoint1.X ;
newPoint2.X =2;
data[curPoint.Y, curPoint.X] = 9;
GoNewPoint(newPoint1,newPoint2);
break;
case 40://下
newPoint1.Y ;
newPoint2.Y =2;
data[curPoint.Y, curPoint.X] = 7;
GoNewPoint(newPoint1, newPoint2);
break;
case 37://左
newPoint1.X--;
newPoint2.X-=2;
data[curPoint.Y, curPoint.X] = 8;
GoNewPoint(newPoint1, newPoint2);
break;
}
ShowInfo();
}
private void GoNewPoint(Point newPoint1,Point newPoint2)
{
step ;
lblStep.Text = string.Format("{0:000}", step);
bool isFinshed=false;
if(newPoint1.X<0||newPoint1.Y<0) return;
int n1 = data[newPoint1.Y, newPoint1.X];
int n2 = data[newPoint2.Y, newPoint2.X];
if (n1 <= 1) return;
if (n1 == 2||n1==3)
{
data[newPoint1.Y, newPoint1.X] = data[curPoint.Y, curPoint.X];
RefImg(newPoint1);
data[curPoint.Y, curPoint.X] = items[curPoint.Y, curPoint.X].OldValue;
RefImg(curPoint);
curPoint = newPoint1;
return;
}
if (n1 == 5)
{
if (n2 == 1 || n2 == 5) return;
if (n2 == 2)
{
data[newPoint2.Y, newPoint2.X] = 5;
}
if (n2 == 3)
{
data[newPoint2.Y, newPoint2.X] = 4;
isFinshed = true;
}
RefImg(newPoint2);
data[newPoint1.Y, newPoint1.X] = data[curPoint.Y, curPoint.X];
RefImg(newPoint1);
data[curPoint.Y, curPoint.X] = items[curPoint.Y, curPoint.X].OldValue;
RefImg(curPoint);
curPoint = newPoint1;
CheckFinshed();
}
if (n1 == 4)
{
if (n2 == 2 || n2 == 3)
{
if (n2 == 2)
{
data[newPoint2.Y, newPoint2.X] = 5;
}
if (n2 == 3)
{
data[newPoint2.Y, newPoint2.X] = 4;
}
RefImg(newPoint2);
data[newPoint1.Y, newPoint1.X] = data[curPoint.Y, curPoint.X];
RefImg(newPoint1);
data[curPoint.Y, curPoint.X] = items[curPoint.Y, curPoint.X].OldValue;
RefImg(curPoint);
curPoint = newPoint1;
CheckFinshed();
}
}
}
private void CheckFinshed()
{
for (int i = 0; i < items.GetUpperBound(0); i )
{
for (int j = 0; j < items.GetUpperBound(1); j )
{
if (data[i,j]==5)
{
return;
}
}
}
MessageBox.Show("完工");
curlvl ;
if (curlvl >= Config.Levels.Length)
{
MessageBox.Show("通关!");
return;
}
Form1_Load(null, null);
}
void RefImg(Point p)
{
items[p.Y, p.X].Tag = data[p.Y, p.X];
items[p.Y, p.X].RefImage();
}
private void button1_Click(object sender, EventArgs e)
{
curlvl = 0;
Form1_Load(null, null);
}
private void button2_Click(object sender, EventArgs e)
{
Form1_Load(null, null);
}
private void button3_Click(object sender, EventArgs e)
{
if (curlvl > 0)
{
curlvl--;
Form1_Load(null, null);
}
}
private void button4_Click(object sender, EventArgs e)
{
if (curlvl <Config.Levels.Length)
{
curlvl ;
Form1_Load(null, null);
}
}
标签: 游戏
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论