📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Xml;
using System.Text;
using System.IO;
namespace FiyaSoft.DiamondGame
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
# region 全局变量
private bool gameOver;
private const int CELL=15;
private int mark=0;
private Block blockNow;
private Block blockLast;
private Block blockNext;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.Label label3;
private Image blockImage;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Reflection.Assembly execAssem;
public static Wall wall;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label labName;
private string _MaxPlayer;
private int _MaxScore;
private System.Windows.Forms.Label labScore;
private string _FilePath;
private string _ApplicationDirectory;
private Sound _SouGong;
private Sound _SouClick;
private Sound _SouChimes;
private Sound _SouApplause;
# endregion
# region 构造函数
public Form1()
{
InitializeComponent();
//得到最高成绩与名称
execAssem = System.Reflection.Assembly.GetExecutingAssembly();
string appFilePath = execAssem.GetModules()[0].FullyQualifiedName;
_ApplicationDirectory = System.IO.Path.GetDirectoryName(appFilePath);
if (!_ApplicationDirectory.EndsWith(@"\"))
{
_ApplicationDirectory += @"\";
}
_FilePath = _ApplicationDirectory + "Score.xml";
if ( LoadXml(_FilePath,ref _MaxPlayer,ref _MaxScore) )
{
this.labName.Text = _MaxPlayer;
this.labScore.Text = _MaxScore.ToString();
}
else
{
SaveXml( _FilePath,"",0 );
}
//实例化声音
FileStream strm = new FileStream( _ApplicationDirectory + "Gong.wav", FileMode.Open );
_SouGong = new Sound( strm );
strm = new FileStream( _ApplicationDirectory + "Click.wav", FileMode.Open );
_SouClick = new Sound( strm );
strm = new FileStream( _ApplicationDirectory + "Chimes.wav", FileMode.Open );
_SouChimes = new Sound( strm );
strm = new FileStream( _ApplicationDirectory + "Applause.wav", FileMode.Open );
_SouApplause = new Sound( strm );
gameOver=false;
wall=new Wall();
blockNext=new Block();
//绘制蓝色背景,大小 200*200 pix
Rectangle rect = new Rectangle(0, 0, 200, 200);
blockImage=new Bitmap(200,200);
Graphics tmpg=Graphics.FromImage(blockImage);
SolidBrush tmpBrush=new SolidBrush( Color.Blue );
tmpg.FillRectangle(tmpBrush,rect);
tmpg.Dispose();
tmpBrush.Dispose();
label3.Text=mark.ToString();
}
# endregion
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Windows.Forms.Timer();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.label5 = new System.Windows.Forms.Label();
this.labName = new System.Windows.Forms.Label();
this.labScore = new System.Windows.Forms.Label();
//
// timer1
//
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(184, 32);
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.Text = "下一块";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(184, 152);
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.Text = "分 数";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// menuItem1
//
this.menuItem1.Text = "";
//
// menuItem3
//
this.menuItem3.Text = "";
//
// menuItem4
//
this.menuItem4.Text = "";
//
// menuItem2
//
this.menuItem2.Text = "";
//
// label3
//
this.label3.ForeColor = System.Drawing.Color.Red;
this.label3.Location = new System.Drawing.Point(184, 168);
this.label3.Size = new System.Drawing.Size(48, 16);
this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold);
this.label4.ForeColor = System.Drawing.Color.Black;
this.label4.Location = new System.Drawing.Point(32, 72);
this.label4.Size = new System.Drawing.Size(120, 56);
this.label4.Visible = false;
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem5);
this.mainMenu1.MenuItems.Add(this.menuItem6);
this.mainMenu1.MenuItems.Add(this.menuItem7);
//
// menuItem5
//
this.menuItem5.Text = "开始";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Text = "结束";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Text = "说明";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// label5
//
this.label5.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.label5.ForeColor = System.Drawing.Color.Red;
this.label5.Location = new System.Drawing.Point(184, 200);
this.label5.Size = new System.Drawing.Size(56, 16);
this.label5.Text = "记 录";
this.label5.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// labName
//
this.labName.ForeColor = System.Drawing.Color.Red;
this.labName.Location = new System.Drawing.Point(184, 216);
this.labName.Size = new System.Drawing.Size(48, 16);
this.labName.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// labScore
//
this.labScore.ForeColor = System.Drawing.Color.Red;
this.labScore.Location = new System.Drawing.Point(184, 232);
this.labScore.Size = new System.Drawing.Size(48, 16);
this.labScore.Text = "0";
this.labScore.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(250, 272);
this.Controls.Add(this.labScore);
this.Controls.Add(this.labName);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Text = "钻石方块";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.GotFocus += new System.EventHandler(this.Form1_GotFocus);
this.LostFocus += new System.EventHandler(this.Form1_LostFocus);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
///[STAThread]
static void Main()
{
Application.Run(new Form1());
}
# region 重载基数函数与属性
protected override void OnPaint(PaintEventArgs e)
{
Graphics grfx=e.Graphics;
Brush temBh=new SolidBrush( Color.Blue );
grfx.FillRectangle(temBh,181,0,this.Width,this.Height);
temBh.Dispose();
int i,j;
Point[] b1=blockNext.GetCakes();
for(i=14;i<16;i++)
for(j=5;j<9;j++)
{
fillCake(new Point(i,j),new SolidBrush(SystemColors.Control),grfx);
}
for( i=0;i<4;i++)
{
b1[i].X+=8;
b1[i].Y+=4;
fillCake(b1[i],new SolidBrush( Color.Red ),grfx);
b1[i].X-=8;
b1[i].Y-=4;
}
for(i=1;i<13;i++)
for( j=1;j<19;j++)
{
if(wall.Cakes[i,j])
fillCake(new Point(i,j),new SolidBrush( Color.AliceBlue ),grfx);
else
fillCake(new Point(i,j),new SolidBrush( Color.Red ),grfx);
}
}
# endregion
# region 私有函数
/// <summary>
/// 绘制小方块
/// </summary>
private void fillCake(Point p,Brush bru,Graphics grfx)
{
SolidBrush sBrush = (SolidBrush) bru;
if( sBrush.Color == Color.Red | sBrush.Color == Color.Blue )
{
grfx.FillRectangle( bru,new Rectangle((p.X-1)*CELL+1,(p.Y-1)*CELL+1,CELL-2,CELL-2) );
for(int d=0;d<2;d++)
{
grfx.DrawRectangle( new Pen( Color.Black ),(p.X-1)*CELL+1+d,(p.Y-1)*CELL+d+1,CELL-d*2-2,CELL-d*2-2);
}
}
else
{
grfx.FillRectangle(bru,(p.X-1)*CELL,(p.Y-1)*CELL,CELL,CELL);
for(int d=0;d<2;d++)
{
grfx.DrawRectangle(new Pen( Color.AliceBlue ),(p.X-1)*CELL+d,(p.Y-1)*CELL+d,CELL-d*2,CELL-d*2);
}
}
}
/// <summary>
/// 显示一组方块的运动
/// </summary>
private void Display()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -