注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

2015年06月01日  

2015-06-01 22:51:52|  分类: C#基础 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
2015年06月01日 - 983433479 - 告别迷茫
1.根据等级的大小自动的改变窗口的大小

2.点击事件,根据本快是不是有文字,且周围有空的为标准。
 

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.IO;

namespace Game1
{
public struct Record
{
public string Dengji;
public string Name;
public string Time;
public Record(string deng, string name, string time)
{
this.Dengji = deng;
this.Name = name;
this.Time = time;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int Level = 3;//等级的设置;
private int Length = 100;//长度*Level为我们的界面的大小;
private Button[,] button;
private Image[] img = new Image[] { Properties.Resources.bird, Properties.Resources.horse, Properties.Resources.lion };
Timer time = new Timer();
Record[] cord = new Record[] { new Record(), new Record(), new Record() };

private void BeginOnClick(object sender, EventArgs e)//初级
{
panel1.Controls.Clear();
time.Start();
toolStripStatusLabel2.Text = "0";
this.Level = 3;
this.ClientSize = new Size(Level * Length, Length * Level + menuStrip1.Height + statusStrip1.Height);
InitGames();
changeTextforbutton();
Invalidate();
}

private void ZhongjiOnClick(object sender, EventArgs e)
{
panel1.Controls.Clear();
time.Start();
toolStripStatusLabel2.Text = "0";
this.Level = 4;
this.ClientSize = new Size(Level * Length, Length * Level + menuStrip1.Height + statusStrip1.Height);
InitGames();
changeTextforbutton();
Invalidate();

}

private void GaoOnCLick(object sender, EventArgs e)
{
panel1.Controls.Clear();
this.Level = 5;
time.Start();
toolStripStatusLabel2.Text = "0";
this.ClientSize = new Size(Level * Length, Length * Level + menuStrip1.Height + statusStrip1.Height);
InitGames();
changeTextforbutton();
Invalidate();

}

private void ExitOnCLick(object sender, EventArgs e)
{
if (MessageBox.Show("你将要退出游戏?", "退出", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
toolStripStatusLabel2.Text = "0";
time.Stop();
}
}

private void BeginGamesOnClick(object sender, EventArgs e)
{
panel1.Enabled = true;
ChujiToolStripMenuItem.Enabled = true;
GaojiToolStripMenuItem.Enabled = true;
ZhongjiToolStripMenuItem.Enabled = true;
AgainToolStripMenuItem.Enabled = true;
toolStripStatusLabel2.Text = "0";
time.Start();

}

private void About(object sender, EventArgs e)
{
MessageBox.Show("这个是我们学习C#开发的一个小游戏", "About", MessageBoxButtons.OK);
}

private void AgainGameOnClick(object sender, EventArgs e)
{
time.Stop();
panel1.Controls.Clear();
InitGames();
panel1.Enabled = true;
changeTextforbutton();
time.Start();
toolStripStatusLabel2.Text = "0";
Invalidate();

}
public void InitGames()//初始化游戏的界面
{
button = new Button[Level, Level];
for (int i = 0; i < Level; i++)
{
for (int j = 0; j < Level; j++)
{
button[i, j] = new Button();
Font itFont = new Font("黑体", 20);
button[i, j].Font = itFont;
button[i, j].Size = new Size(Length, Length);
button[i, j].Location = new Point(j * Length, i * Length);//确定我们的位置;
int index = i * Level + j + 1;

if (index < Level * Level)
{
button[i, j].Text = (index).ToString();

}
else
{
ChangeImageForButton(button[i,j],Level);
}
button[i, j].Tag = index-1;
button[i, j].Click += new EventHandler(this.ChangeButtonOnCLick);//为按钮增加点击事件
panel1.Controls.Add(button[i, j]);

}


}
}


private void ChangeButtonOnCLick(object sender, EventArgs e)//点击事件的实现
{
int[,] Step = new int[,] { {-1,0},{0,1},{1,0},{0,-1}};
int index = (int)((Button)sender).Tag;
if (((Button)sender).Text == "")//本身就是空的
{
return;
}
int now_Row=index/Level;
int now_Col=index%Level;
for(int i = 0; i < 4; i++)
{
int row,col;
row = now_Row + Step[i, 0];
col = now_Col + Step[i, 1];
if (row >= 0 && col >= 0 && row < Level && col < Level)
{
if (button[row, col].Text == "")//转换图片,文字
{
button[row, col].BackgroundImage = null;
ChangeImageForButton((Button)sender, Level);
string changeText = ((Button)sender).Text;
((Button)sender).Text = "";
button[row, col].Text = changeText;
break;

}

}

}
if (IsSuccess() == true)
{
time.Stop();

//增加统计我们英雄榜;


if (IsCreateNewRecord())
{
Sucess sucess = new Sucess();
sucess.ChanCan(Level, toolStripStatusLabel2.Text);
sucess.ShowDialog();
}
else
{
MessageBox.Show("恭喜你用时" + toolStripStatusLabel2.Text, "GOOD JOB", MessageBoxButtons.OK);
}

toolStripStatusLabel2.Text = "0";
panel1.Enabled = false;
}


}
public bool IsCreateNewRecord()//是否刷新了新的记录;
{
if (File.Exists("degree.txt"))
{
FileStream file = new FileStream("degree.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);


for (int i = 0; i < 3; i++)
{
cord[i].Dengji = reader.ReadLine();
cord[i].Name = reader.ReadLine();
cord[i].Time = reader.ReadLine();

}
reader.Close();
file.Close();
int gone = Int32.Parse(cord[Level - 3].Time);
int now = Int32.Parse(toolStripStatusLabel2.Text);
return gone > now ? true : false;
}
return false;
}
public bool IsSuccess()//判断是否达到最终的状态;
{
for (int i = 0; i < Level; i++)
{
for (int j = 0; j < Level; j++)
{
int nowtext = i * Level + j + 1;
if(nowtext<Level*Level)//排除最后的一个!
{
int index =(int) button[i, j].Tag+1;
if (button[i, j].Text != index.ToString())
{
return false;
}

}

}

}
return true;

}
public void ChangeImageForButton(Button button, int e)
{
switch (e)
{
case 3:
button.BackgroundImage = img[0];
break;

case 4:
button.BackgroundImage = img[1];
break;
case 5:
button.BackgroundImage = img[2];
break;
}
return;
}

public void changeTextforbutton()
{
Random m_changeText = new Random();
for (int i = 0; i < Level; i++)
{
int row1, row2, coll1, coll2;
row1 = m_changeText.Next(Level);
row2 = m_changeText.Next(Level);
coll1 = m_changeText.Next(Level);
coll2 = m_changeText.Next(Level);
if (button[row1, coll1].Text == "")
{
button[row1, coll1].BackgroundImage = null;
ChangeImageForButton(button[row2, coll2], Level);
}
else if (button[row2, coll2].Text == "")
{

button[row2, coll2].BackgroundImage = null;
ChangeImageForButton(button[row1, coll1], Level);
}
string changeText;
changeText = button[row1, coll1].Text;
button[row1, coll1].Text = button[row2, coll2].Text;
button[row2, coll2].Text = changeText;



}
}

private void GameOnLoad(object sender, EventArgs e)
{
this.ClientSize = new Size(Level * Length, Length * Level + menuStrip1.Height + statusStrip1.Height);
//动态的改变窗口的大小
InitGames();

//测试数据,可以把下面注释掉。
changeTextforbutton();
panel1.Enabled = false;
ChujiToolStripMenuItem.Enabled = false;
GaojiToolStripMenuItem.Enabled = false;
ZhongjiToolStripMenuItem.Enabled = false;
AgainToolStripMenuItem.Enabled = false;
time.Interval = 1000;
time.Tick += new EventHandler(ChangeTimeByTimer);//动态的增加事件的改变!
CreateFile();//如果没有查看英雄榜,文件就没有写入!


}
public void CreateFile()
{
if (!File.Exists("degree.txt"))
{
cord[0].Name="匿名";
cord[0].Time="999";
cord[0].Dengji = "初级";
cord[1].Name = "匿名";
cord[1].Time = "999";
cord[1].Dengji = "中级";
cord[2].Name = "匿名";
cord[2].Time = "999";
cord[2].Dengji = "高级";
FileStream file = new FileStream("degree.txt", FileMode.Create, FileAccess.Write);
StreamWriter write = new StreamWriter(file);
for (int i = 0; i < 3; i++)
{
write.WriteLine(cord[i].Dengji);
write.WriteLine(cord[i].Name);
write.WriteLine(cord[i].Time);
}
write.Close();
file.Close();
}
}
private void ChangeTimeByTimer(Object sender, EventArgs e)
{
int timenow =Convert.ToInt32(toolStripStatusLabel2.Text);
timenow++;
toolStripStatusLabel2.Text=timenow.ToString();

}

private void YingXiongBangOnCLick(object sender, EventArgs e)
{
YingXiongBang ying = new YingXiongBang();
ying.ShowDialog();
}
private void WriteToFile()
{

}

}
}


其他的都差不多啊!
  评论这张
 
阅读(2)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017