📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace NumPuzzle
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
int GameSize = 0;
byte[] Position; //绝对地址
Button[] Buttons;
const int MAP_WIDTH = 300;
bool IsRun = false;
int Clicks = 0;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4,
this.menuItem5,
this.menuItem6});
this.menuItem1.Text = "Game(&G)";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "New Game(&N)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "High Score(&H)";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "About(&A)";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Index = 3;
this.menuItem5.Text = "-";
//
// menuItem6
//
this.menuItem6.Index = 4;
this.menuItem6.Text = "Exit(&X)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 335);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(354, 24);
this.statusBar1.TabIndex = 0;
this.statusBar1.Text = "NumPuzzle Game C# By Red_angelX Thanks [YoYo]";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
this.ClientSize = new System.Drawing.Size(354, 359);
this.Controls.Add(this.statusBar1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "NumPuzzle Game C# By Red_angelX";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
NewGameConfig ngc = new NewGameConfig();
if(ngc.ShowDialog() == DialogResult.OK)
{
switch(ngc.comboBox1.SelectedIndex)
{
case 0:
GameSize = 3;
break;
case 1:
GameSize = 4;
break;
case 2:
GameSize = 5;
break;
case 3:
GameSize = 6;
break;
default:
MessageBox.Show("请正确选择游戏难度");
return;
}
InitGame();
}
}
//初始化游戏相关设置
private void InitGame()
{
if(Buttons != null)
{
//有没有更好的办法?
for(int i=0;i<Buttons.Length;i++)
Buttons[i].Dispose();
}
Buttons = new Button[GameSize*GameSize];
Position = new byte[GameSize*GameSize];
Position[0] = 0xff; //空的位置
for(int i=1;i<Position.Length;i++)
{
Position[i] = (byte)i;
}
//随机洗牌算法
byte[] key = new byte[GameSize*GameSize];
new Random().NextBytes(key);
Array.Sort(key,Position);
//动态生成按扭,其实可以用GDI画?
int BWidth = MAP_WIDTH / GameSize;
for(int i=0;i<Buttons.Length;i++)
{
Buttons[i] = new Button();
Buttons[i].Size = new Size(BWidth,BWidth);
int j = i / GameSize;
int k = i % GameSize;
Buttons[i].Location = new Point(24+k*BWidth,16+j*BWidth);
if(Position[i] == 0xff)
{
Buttons[i].Visible = false;
}
Buttons[i].Text = Position[i].ToString();
Buttons[i].Enabled = false;
this.Controls.Add(Buttons[i]);
}
IsRun = true;
this.Clicks = 0;
}
private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(IsRun == false)
return;
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Left:
DoChange(e.KeyCode);
break;
default:
break;
}
}
private void DoChange(Keys key)
{
//寻找空格位置
int offest = -1;
int MoveIndex = -1;
for(int i=0;i<Position.Length;i++)
{
if(Position[i] == 0xff)
{
offest = i;
break;
}
}
switch(key)
{
case Keys.Up:
MoveIndex = offest + GameSize;
break;
case Keys.Down:
MoveIndex = offest - GameSize;
break;
case Keys.Left:
MoveIndex = offest + 1;
if(offest % GameSize == GameSize - 1)
return;
break;
case Keys.Right:
MoveIndex = offest - 1;
if(offest % GameSize == 0)
return;
break;
default:
break;
}//End Switch
//判断有效范围
//判断是否能移动
if(MoveIndex < 0 || MoveIndex >= Position.Length)
return;
Clicks++;
this.statusBar1.Text = Clicks.ToString()+" Move";
PlaySound.Play("MOVE.WAV");
byte temp;
temp = Position[offest];
Position[offest] = Position[MoveIndex];
Position[MoveIndex] = temp;
//更新UI
UpDataUI(offest,MoveIndex);
CheckWin();
}
private void UpDataUI(int offest,int MoveIndex)
{
if(this.IsRun == false)
return;
Buttons[offest].Visible = true;
Buttons[offest].Text = Position[offest].ToString();
Buttons[MoveIndex].Visible = false;
}
//检查是否胜利
private void CheckWin()
{
for(int i=1;i<Position.Length;i++)
{
if(Position[i-1] != (byte)i)
{
return;
}
}
//Win
PlaySound.Play("WIN.WAV");
IsRun = false;
this.statusBar1.Text+=" 过关!";
Winner wn = new Winner(GameSize,this.Clicks);
wn.ShowDialog();
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
Close();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
About ab = new About();
ab.ShowDialog();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
HighSocre hs = new HighSocre();
hs.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -