📄 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;
namespace BLOCK_HB
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
[DllImport("winmm.dll")]
public static extern int sndPlaySound(string path,int falgs);
private System.ComponentModel.IContainer components;
private bool gameOver=false;
private const int CELL=15;
private const int H=26;
private const int W=20;//12+8;
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.MainMenu mainMenu1;
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;
public static Wall wall=new Wall();
private void fillCake(Point p,Brush bru,Graphics grfx)
{
if(bru ==Brushes.Aquamarine | bru==Brushes.Blue)
{
grfx.DrawImage(blockImage,(p.X-1)*CELL+1,(p.Y-1)*CELL+1,CELL-2,CELL-2);
for(int d=0;d<2;d++)
{
grfx.DrawRectangle(Pens.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(Pens.AliceBlue,(p.X-1)*CELL+d,(p.Y-1)*CELL+d,CELL-d*2,CELL-d*2);
}
}
}
public Form1()
{
blockNext=new Block();
Rectangle rect = new Rectangle(0, 0, 200, 200);
blockImage=new Bitmap(200,200,this.CreateGraphics());
Graphics tmpg=Graphics.FromImage(blockImage);
LinearGradientBrush tmpBrush=new LinearGradientBrush(
rect,
Color.LightBlue,Color.Red ,LinearGradientMode.ForwardDiagonal);
tmpg.FillRectangle(tmpBrush,rect);
tmpg.Dispose();
tmpBrush.Dispose();
InitializeComponent();
label3.Text=mark.ToString();
}
/// <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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
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.SuspendLayout();
//
// timer1
//
this.timer1.Interval = 150;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Goldenrod;
this.label1.Location = new System.Drawing.Point(216, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 0;
this.label1.Text = "下一块";
//
// label2
//
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.ForeColor = System.Drawing.Color.DarkGoldenrod;
this.label2.Location = new System.Drawing.Point(216, 200);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 1;
this.label2.Text = "分数";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem3,
this.menuItem4});
this.menuItem1.Text = "游戏";
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Text = "开始";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "结束";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "关于";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// label3
//
this.label3.BackColor = System.Drawing.Color.Transparent;
this.label3.Cursor = System.Windows.Forms.Cursors.IBeam;
this.label3.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(216, 224);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 32);
this.label3.TabIndex = 2;
//
// label4
//
this.label4.BackColor = System.Drawing.Color.LightGray;
this.label4.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.ForeColor = System.Drawing.Color.Red;
this.label4.Location = new System.Drawing.Point(48, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(120, 56);
this.label4.TabIndex = 0;
this.label4.Visible = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(294, 411);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint (e);
Graphics grfx=e.Graphics;
Brush temBh=new LinearGradientBrush(this.ClientRectangle,Color.Red,Color.Blue,LinearGradientMode.Vertical);
grfx.FillRectangle(temBh,181,0,this.Width,this.Height);
temBh.Dispose();
int i,j;
Point[] b1=blockNext.GetCakes();
for(i=16;i<18;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+=10;//显示在右边
b1[i].Y+=4;
fillCake(b1[i],Brushes.Blue,grfx);
b1[i].X-=10;
b1[i].Y-=4;
}
for(i=1;i<13;i++)
for( j=1;j<27;j++)
{
if(wall.Cakes[i,j])
fillCake(new Point(i,j),Brushes.AliceBlue,grfx);
else
fillCake(new Point(i,j),Brushes.Aquamarine,grfx);
}
//grfx.FillRectangle(Brushes.AliceBlue,0,0,CELL*(W-8),CELL*H);
}
void Display()
{
Point[] b1=blockLast.GetCakes();
Point[] b2=blockNow.GetCakes();
int i=0;
Graphics grfx=this.CreateGraphics();
for(i=0;i<4;i++)
{
fillCake(b1[i],Brushes.AliceBlue,grfx);
}
for(i=0;i<4;i++)
{
fillCake(b2[i],Brushes.Aquamarine,grfx);
}
}
private void Init()
{
blockNow=new Block();
blockLast=new Block();
blockNow.Shape=blockNext.Shape;
blockNow.SetBlock(blockNext.GetCakes());
blockLast.SetBlock(blockNow.GetCakes());
blockNext=new Block();
flushNext();
Point[] p=blockNow.GetCakes();
for(int i=0;i<4;i++)
{
if (!wall.Cakes[p[i].X,p[i].Y])
{
timer1.Stop();
GameOver();
break;
}
}
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
try
{
blockLast.SetBlock(blockNow.GetCakes());//重点:当方块移动时消除原来的方块
switch(e.KeyCode)
{
case Keys.Up:
if(blockNow.CanRotate())
{
blockNow.Rotate(blockNow.GetCakes());
Display();
}
break;
case Keys.Down:
while(blockNow.CanMove(Block.Direction.DOWN) )
{
blockNow.Move(Block.Direction.DOWN);
}
Display();
break;
case Keys.Left:
blockNow.Move(Block.Direction.LEFT);
Display();
break;
case Keys.Right:
blockNow.Move(Block.Direction.RIGHT);
Display();
break;
default:
break;
}
}
catch
{}
}
void GameOver()
{
sndPlaySound("Applause.wav",1);
gameOver=true;
for(int j=27;j>0;j--)
{
for(int i=1;i<13;i++)
{
wall.Cakes[i,j]=false;
}
Invalidate(); Thread.Sleep(50);
Application.DoEvents();
label4.Visible=true;
label4.Text="G a m e O v e r";
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
int flag=0;
if(blockNow.CanMove(Block.Direction.DOWN))
{
blockLast.SetBlock(blockNow.GetCakes());
this.blockNow.Move(Block.Direction.DOWN);
Display();
}
else
{
Point[] p=blockNow.GetCakes();
for(int i=0;i<4;i++)
{
wall.Cakes[p[i].X,p[i].Y]=false;
if(wall.CheckRoll(p[i].Y))
{
flag=1;
sndPlaySound("glass.wav",1);
mark+=10;
label3.Text=mark.ToString();
Invalidate(false);
}
}
if(flag==0)
{
sndPlaySound("Click.wav",1);
}
Init();
}
}//timer1_Tick()
private void flushNext()
{
int i,j;
Graphics grfx=this.CreateGraphics();
Point[] b1=blockNext.GetCakes();
for(i=16;i<18;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+=10;
b1[i].Y+=4;
fillCake(b1[i],Brushes.Blue,grfx);
b1[i].X-=10;
b1[i].Y-=4;
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
sndPlaySound("Gong.wav",1);
gameOver=false;
label4.Visible=false;
mark=0;
for(int i=1;i<13;i++)
for(int j=1;j<27;j++)
wall.Cakes[i,j]=true;
Invalidate(false);
Init();
Display();
timer1.Start();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
about dlg=new about();
dlg.Show();
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
if(gameOver!=true)
{
base.OnPaintBackground (pevent);
}
}
}//Class Form1
}//NameSpace BLOCK_HB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -