以下为"俄罗斯方块"的C#源代码:MainForm.cs,Block.cs,SettingForm.cs
MainForm.cs
----------------------->>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace Tetris0._9
{
public class MainForm : System.Windows.Forms.Form
{
#region 变量
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Timer timer1;
private Block block;
private Block nextBlock;
private int nextShapeNO;
private bool paused;
private DateTime atStart;
private DateTime atPause;
private TimeSpan pauseTime;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.TextBox textBox1;
private SettingForm sform;
private Keys[] keys;
private System.Windows.Forms.Label label4;
private int level;
private int startLevel;
private bool trans;
private int rowDelNum;
private bool failed;
private System.Windows.Forms.Label label5;
private System.ComponentModel.IContainer components;
#endregion
public MainForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
static void Main()
{
Application.Run(new MainForm());
}
private void Initiate()
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load("c://setting.cob");
XmlNodeList nodes=doc.DocumentElement.ChildNodes;
this.startLevel=Convert.ToInt32(nodes[0].InnerText);
this.level=this.startLevel;
this.trans=Convert.ToBoolean(nodes[1].InnerText);
keys=new Keys[5];
for(int i=0;i
KeysConverter kc=new KeysConverter();
this.keys[i]=(Keys)(kc.ConvertFromString(nodes[2].ChildNodes[i].InnerText));
}
}
catch
{
this.trans=false;
keys=new Keys[5];
keys[0]=Keys.Left;
keys[1]=Keys.Right;
keys[2]=Keys.Down;
keys[3]=Keys.NumPad8;
keys[4]=Keys.NumPad9;
this.level=1;
this.startLevel=1;
}
this.timer1.Interval=500-50*(level-1);
this.label4.Text="级别: "+this.startLevel;
if(trans)
{
this.TransparencyKey=Color.Black;
}
}
private void Start()
{
this.block=null;
this.nextBlock=null;
this.label1.Text="手速: 0";
this.label2.Text="块数: 0";
this.label3.Text="行数: 0";
this.label4.Text="级别: "+this.startLevel;
this.level=this.startLevel;
this.timer1.Interval=500-50*(level-1);
this.paused=false;
this.failed=false;
this.panel1.Invalidate();
this.panel2.Invalidate();
this.nextShapeNO=0;
this.CreateBlock();
this.CreateNextBlock();//
this.timer1.Enabled=true;
this.atStart=DateTime.Now;
this.pauseTime=new TimeSpan(0);
}
private void Fail()
{
this.failed=true;
this.panel1.Invalidate(new Rectangle(0,0,panel1.Width,100));
this.timer1.Enabled=false;
this.paused=true;
}
private bool CreateBlock()
{
Point firstPos;
Color color;
if(this.nextShapeNO==0)
{
Random rand=new Random();
this.nextShapeNO=rand.Next(1,8);
}
switch(this.nextShapeNO)
{
case 1://田
firstPos=new Point(4,0);
color=Color.Turquoise;
break;
case 2://一
firstPos=new Point(3,0);
color=Color.Red;
break;
case 3://土
firstPos=new Point(4,0);
color=Color.Silver;
break;
case 4://z
firstPos=new Point(4,0);
color=Color.LawnGreen;
break;
case 5://倒z
firstPos=new Point(4,1);
color=Color.DodgerBlue;
break;
case 6://L
firstPos=new Point(4,0);
color=Color.Yellow;
break;
default://倒L
firstPos=new Point(4,0);
color=Color.Salmon;
break;
}
if(this.block==null)
{
block=new Block(this.panel1,9,19,25,this.nextShapeNO,firstPos,color);
}
else
{
if(!block.GeneBlock(this.nextShapeNO,firstPos,color))
{
return false;
}
}
block.EraseLast();
block.Move(2);
return true;
}
private void CreateNextBlock()
{
Random rand=new Random();
this.nextShapeNO=rand.Next(1,8);
Point firstPos;
Color color;
switch(this.nextShapeNO)
{
case 1://田
firstPos=new Point(1,0);
color=Color.Turquoise;
break;
case 2://一
firstPos=new Point(0,1);
color=Color.Red;
break;
case 3://土
firstPos=new Point(0,0);
color=Color.Silver;
break;
case 4://z
firstPos=new Point(0,0);
color=Color.LawnGreen;
break;
case 5://倒z
firstPos=new Point(0,1);
color=Color.DodgerBlue;
break;
case 6://L
firstPos=new Point(0,0);
color=Color.Yellow;
break;
default://倒L
firstPos=new Point(0,0);
color=Color.Salmon;
break;
}
if(nextBlock==null)
nextBlock=new Block(this.panel2,3,1,20,this.nextShapeNO,firstPos,color);
else
{
nextBlock.GeneBlock(this.nextShapeNO,firstPos,color);
nextBlock.EraseLast();
}
}
private void FixAndCreate()
{
block.FixBlock();
this.label1.Text="手速:"+Math.Round((double)block.BlockNum/((TimeSpan)(DateTime.Now-this.atStart)).Subtract(this.pauseTime).TotalSeconds,3)+"块/秒";
this.label2.Text="块数: "+block.BlockNum;
this.label3.Text="行数: "+block.RowDelNum;
if(this.level<10 && block.RowDelNum-this.rowDelNum>=30)
{
this.rowDelNum+=30;
this.level++;
this.timer1.Interval=500-50*(level-1);
this.label4.Text="级别: "+this.level;
}
bool createOK=this.CreateBlock();
this.CreateNextBlock();
if(!createOK)
this.Fail();
}
private void SaveSetting()
{
try
{
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDec=doc.CreateXmlDeclaration ("1.0","gb2312",null);
XmlElement setting=doc.CreateElement("SETTING");
doc.AppendChild(setting);
XmlElement level=doc.CreateElement("LEVEL");
level.InnerText=this.startLevel.ToString();
setting.AppendChild(level);
XmlElement trans=doc.CreateElement("TRANSPARENT");
trans.InnerText=this.trans.ToString();
setting.AppendChild(trans);
XmlElement keys=doc.CreateElement("KEYS");
setting.AppendChild(keys);
foreach(Keys k in this.keys)
{
KeysConverter kc=new KeysConverter();
XmlElement x=doc.CreateElement("SUBKEYS");
x.InnerText=kc.ConvertToString(k);
keys.AppendChild(x);
}
XmlElement root=doc.DocumentElement;
doc.InsertBefore(xmlDec,root);
doc.Save("c://setting.cob");
}
catch(Exception xe)
{
MessageBox.Show(xe.Message);
}
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.panel3 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(251, 501);
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.Black;
this.panel2.Location = new System.Drawing.Point(16, 8);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(81, 41);
this.panel2.TabIndex = 1;
this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Magenta;
this.label1.Location = new System.Drawing.Point(264, 248);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 22);
this.label1.TabIndex = 2;
this.label1.Text = "手速:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.ForeColor = System.Drawing.Color.DarkGoldenrod;
this.label2.Location = new System.Drawing.Point(264, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 22);
this.label2.TabIndex = 3;
this.label2.Text = "块数:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.ForeColor = System.Drawing.Color.Blue;
this.label3.Location = new System.Drawing.Point(264, 208);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(55, 22);
this.label3.TabIndex = 4;
this.label3.Text = "行数:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));
this.button1.Location = new System.Drawing.Point(288, 304);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 40);
this.button1.TabIndex = 10;
this.button1.Text = "开始(F2)";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button3.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));
this.button3.Location = new System.Drawing.Point(288, 368);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(72, 40);
this.button3.TabIndex = 7;
this.button3.Text = "环境设置";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button4.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));
this.button4.Location = new System.Drawing.Point(288, 432);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(72, 40);
this.button4.TabIndex = 8;
this.button4.Text = "暂停(F3)";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// panel3
//
this.panel3.BackColor = System.Drawing.Color.Black;
this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel3.Controls.Add(this.panel2);
this.panel3.Location = new System.Drawing.Point(272, 8);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(112, 56);
this.panel3.TabIndex = 9;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(288, 240);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(0, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.ForeColor = System.Drawing.Color.Red;
this.label4.Location = new System.Drawing.Point(264, 128);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(55, 22);
this.label4.TabIndex = 11;
this.label4.Text = "级别:";
this.label4.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// label5
//
this.label5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.label5.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label5.ForeColor = System.Drawing.Color.Blue;
this.label5.Location = new System.Drawing.Point(264, 488);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(128, 16);
this.label5.TabIndex = 12;
this.label5.Text = "作者:Hujiiori";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label5.MouseEnter += new System.EventHandler(this.label5_MouseEnter);
this.label5.MouseLeave += new System.EventHandler(this.label5_MouseLeave);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 517);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.panel3);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.ForeColor = System.Drawing.SystemColors.ControlText;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MainForm";
this.Text = "Tetris";
this.TransparencyKey = System.Drawing.Color.Transparent;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyDown);
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.Load += new System.EventHandler(this.MainForm_Load);
this.panel3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
this.Start();
this.textBox1.Focus();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
}
private void MainForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(block!=null && this.paused==false && !this.failed)
{
if(e.KeyCode==this.keys[0])
{
if(block.Move(0))
{
block.EraseLast();
}
}
else if(e.KeyCode==this.keys[1])
{
if(block.Move(1))
{
block.EraseLast();
}
}
else if(e.KeyCode==this.keys[2])
{
if(!block.Move(2))
{
this.FixAndCreate();
}
else
{
block.EraseLast();
}
}
else if(e.KeyCode==this.keys[3])
{
if(block.Rotate())
{
block.EraseLast();
}
}
else if(e.KeyCode==this.keys[4])
{
block.Drop();
block.EraseLast();
this.FixAndCreate();
}
}
if(e.KeyCode==Keys.F2)
{
this.Start();
}
else if(e.KeyCode==Keys.F3)
{
this.button4_Click(null,null);
}
}
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(block!=null)
{
block.DrawBlocks(e.ClipRectangle);
}
if(this.failed)
{
Graphics gra=e.Graphics;
gra.DrawString("Game Over",new Font("Arial Black",25f),new SolidBrush(Color.Gray),30,30);
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(block!=null && !this.failed)
{
if(!block.Move(2))
{
this.FixAndCreate();
}
else
{
block.EraseLast();
}
}
}
private void panel2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(nextBlock!=null)
{
nextBlock.DrawBlocks(e.ClipRectangle);
}
}
private void button4_Click(object sender, System.EventArgs e)
{
if(!this.failed)
{
if(paused)
{
this.pauseTime+=DateTime.Now-this.atPause;
paused=false;
this.timer1.Start();
}
else
{
this.atPause=DateTime.Now;
paused=true;
this.timer1.Stop();
}
}
this.textBox1.Focus();
}
private void button3_Click(object sender, System.EventArgs e)
{
if(!paused)
{
this.atPause=DateTime.Now;
this.paused=true;
this.timer1.Stop();
}
sform=new SettingForm();
sform.SetOptions(this.keys,this.startLevel,this.trans);
sform.DialogResult=DialogResult.Cancel;
sform.ShowDialog();
if(sform.DialogResult==DialogResult.OK)
{
sform.GetOptions(ref this.keys,ref this.startLevel,ref this.trans);
this.level=this.startLevel;
this.label4.Text="级别: "+this.level;
this.timer1.Interval=500-50*(level-1);
if(this.trans)
{
this.TransparencyKey=Color.Black;
}
else
this.TransparencyKey=Color.Transparent;
}
this.paused=false;
this.pauseTime+=DateTime.Now-this.atPause;
this.timer1.Start();
this.textBox1.Focus();
}
private void MainForm_Load(object sender, System.EventArgs e)
{
this.Initiate();
}
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.SaveSetting();
}
private void label5_MouseEnter(object sender, System.EventArgs e)
{
this.label5.Text="QQ:39297745";
}
private void label5_MouseLeave(object sender, System.EventArgs e)
{
this.label5.Text="作者:Hujiiori";
}
}
}
Block.cs
----------------------->>
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Tetris0._9
{
public class Block
{
public Block(Control con,int leftBorder,int bottomBorder,int unitPix,int shapeNO,Point firstPos,Color color)
{
this.con=con;
this.leftBorder=leftBorder;
this.bottomBorder=bottomBorder;
this.unitPix=unitPix;
this.SetPos(shapeNO,firstPos);
this.color=color;
this.huji=new bool[leftBorder+1,bottomBorder+1];
this.iori=new Color[leftBorder+1,bottomBorder+1];
this.lastPos=new Point[4];
}
private int shapeNO;//形状号
private Control con;//绘图控件
private Point[] pos;//当前位置
private Point[] lastPos;//上一次位置
private int leftBorder;//左边界
private int bottomBorder;//下边界
private int unitPix;//每块象素数
private int blockNum=0;
private int rowDelNum=0;
private bool[,] huji;
private Color[,] iori;
private Color color;//当前块颜色
public void EraseLast()
{
foreach(Point p in this.lastPos)
{
this.con.Invalidate(new Rectangle(p.X*unitPix,p.Y*unitPix,unitPix+1,unitPix+1));
}
}
private void SetLastPos()
{
for(int i=0;i
this.lastPos[i]=this.pos[i];
}
}
private void SetPos(int shapeNO,Point firstPos)
{
this.shapeNO=shapeNO;
this.pos=new Point[4];
pos[0]=firstPos;
switch(shapeNO)
{
case 1:
pos[1]=new Point(firstPos.X+1,firstPos.Y);
pos[2]=new Point(firstPos.X,firstPos.Y+1);
pos[3]=new Point(firstPos.X+1,firstPos.Y+1);
break;
case 2:
pos[1]=new Point(firstPos.X+1,firstPos.Y);
pos[2]=new Point(firstPos.X+2,firstPos.Y);
pos[3]=new Point(firstPos.X+3,firstPos.Y);
break;
case 3:
pos[1]=new Point(firstPos.X+1,firstPos.Y);
pos[2]=new Point(firstPos.X+1,firstPos.Y+1);
pos[3]=new Point(firstPos.X+2,firstPos.Y);
break;
case 4:
pos[1]=new Point(firstPos.X+1,firstPos.Y);
pos[2]=new Point(firstPos.X+1,firstPos.Y+1);
pos[3]=new Point(firstPos.X+2,firstPos.Y+1);
break;
case 5:
pos[1]=new Point(firstPos.X+1,firstPos.Y);
pos[2]=new Point(firstPos.X+1,firstPos.Y-1);
pos[3]=new Point(firstPos.X+2,firstPos.Y-1);
break;
case 6:
pos[1]=new Point(firstPos.X,firstPos.Y+1);
pos[2]=new Point(firstPos.X+1,firstPos.Y);
pos[3]=new Point(firstPos.X+2,firstPos.Y);
break;
default:
pos[1]=new Point(firstPos.X+1,firstPos.Y);
pos[2]=new Point(firstPos.X+2,firstPos.Y);
pos[3]=new Point(firstPos.X+2,firstPos.Y+1);
break;
}
}
private bool CanMove(int direction)
{
bool canMove=true;
if(direction==0)
{
foreach(Point p in this.pos)
{
if(p.X-1<0 || this.huji[p.X-1,p.Y])
{
canMove=false;
break;
}
}
}
else if(direction==1)
{
foreach(Point p in this.pos)
{
if(p.X+1>this.leftBorder || this.huji[p.X+1,p.Y])
{
canMove=false;
break;
}
}
}
else
{
foreach(Point p in this.pos)
{
if(p.Y+1>this.bottomBorder || this.huji[p.X,p.Y+1])
{
canMove=false;
break;
}
}
}
return canMove;
}
private bool CanRotate(Point[] pos)
{
bool canRotate=true;
foreach(Point p in pos)
{
if(p.X<0 || p.X>this.leftBorder || p.Y<0 || p.Y>this.bottomBorder || this.huji[p.X,p.Y])
{
canRotate=false;
break;
}
}
if(canRotate==true)
this.SetLastPos();
return canRotate;
}
private void DelRows()
{
int count=0;
int highRow=20;
int lowRow=-1;
int[] delRow={-1,-1,-1,-1};
foreach(Point p in this.pos)
{
if(p.Y==highRow || p.Y==lowRow)
continue;
int i;
for(i=0;i
break;
if(i==this.huji.GetLength(0))
{
delRow[count]=p.Y;
if(p.Y
if(p.Y>lowRow)
lowRow=p.Y;
count++;
}
}
if(count>0)
{
//-----------------------------------------------------------------
Graphics gra=con.CreateGraphics();
foreach(Point p in this.lastPos)
{
gra.FillRectangle(new SolidBrush(con.BackColor),p.X*this.unitPix,p.Y*unitPix,25,25);
}
foreach(Point p in this.pos)
{
this.DrawOne(p.X,p.Y,this.color,gra);
}
foreach(int i in delRow)
{
if(i>0)
{
for(int j=0;j
gra.FillRectangle(new SolidBrush(Color.FromArgb(60,Color.Black)),j*this.unitPix,i*unitPix,25,25);
}
}
}
System.Threading.Thread.CurrentThread.Join(180);
//-----------------------------------------------------------------
if(count==2 && lowRow-highRow>1)
{
for(int i=lowRow;i>highRow+1;i--)
{
for(int j=0;j
this.huji[j,i]=this.huji[j,i-1];
this.iori[j,i]=this.iori[j,i-1];
}
}
for(int i=highRow;i>=count;i--)
{
for(int j=0;j
this.huji[j,i+1]=this.huji[j,i-1];
this.iori[j,i+1]=this.iori[j,i-1];
}
}
}
else if(count==3 && lowRow-highRow>2)
{
int midRow=-1;
foreach(int row in delRow)
{
if(row!=highRow && row!=lowRow)
{
midRow=row;
break;
}
}
for(int j=0;j
this.huji[j,lowRow]=this.huji[j,lowRow+highRow-midRow];
}
for(int i=highRow;i>=count;i--)
{
for(int j=0;j
this.huji[j,i+2]=this.huji[j,i-1];
this.iori[j,i+2]=this.iori[j,i-1];
}
}
}
else
{
for(int i=lowRow;i>=count;i--)
{
for(int j=0;j
this.huji[j,i]=this.huji[j,i-count];
this.iori[j,i]=this.iori[j,i-count];
}
}
}
for(int i=0;i
for(int j=0;j
this.huji[j,i]=false;
}
}
con.Invalidate(new Rectangle(0,0,con.Width,(lowRow+1)*this.unitPix));
this.rowDelNum+=count;
}
}
public void FixBlock()
{
this.blockNum++;
foreach(Point p in this.pos)
{
this.huji[p.X,p.Y]=true;
this.iori[p.X,p.Y]=this.color;
}
this.DelRows();
}
public bool GeneBlock(int shapeNO,Point firstPos,Color color)
{
this.SetLastPos();
this.EraseLast();
this.SetPos(shapeNO,firstPos);
if(!this.CanRotate(this.pos))
{
this.pos=null;
return false;
}
else
{
this.color=color;
return true;
}
}
public bool Rotate()
{
bool rotated=true;
Point[] temp={pos[0],pos[1],pos[2],pos[3]};
switch(this.shapeNO)
{
case 1:
rotated=false;
break;
case 2:
temp[0].Offset(2,2);
temp[1].Offset(1,1);
temp[3].Offset(-1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(2,2);
this.pos[1].Offset(1,1);
this.pos[3].Offset(-1,-1);
this.shapeNO=8;
}
else
rotated=false;
break;
case 3:
temp[0].Offset(1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,-1);
this.shapeNO=9;
}
else
rotated=false;
break;
case 4:
temp[0].Offset(2,0);
temp[1].Offset(0,2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(2,0);
this.pos[1].Offset(0,2);
this.shapeNO=12;
}
else
{
rotated=false;
}
break;
case 5:
temp[2].Offset(-1,0);
temp[3].Offset(-1,2);
if(this.CanRotate(temp))
{
this.pos[2].Offset(-1,0);
this.pos[3].Offset(-1,2);
this.shapeNO=13;
}
else
{
rotated=false;
}
break;
case 6:
temp[0].Offset(1,1);
temp[1].Offset(2,0);
temp[3].Offset(-1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,1);
this.pos[1].Offset(2,0);
this.pos[3].Offset(-1,-1);
this.shapeNO=14;
}
else
{
rotated=false;
}
break;
case 7:
temp[0].Offset(1,1);
temp[2].Offset(-1,-1);
temp[3].Offset(0,-2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,1);
this.pos[2].Offset(-1,-1);
this.pos[3].Offset(0,-2);
this.shapeNO=17;
}
else
{
rotated=false;
}
break;
case 8:
temp[0].Offset(-2,-2);
temp[1].Offset(-1,-1);
temp[3].Offset(1,1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-2,-2);
this.pos[1].Offset(-1,-1);
this.pos[3].Offset(1,1);
this.shapeNO=2;
}
else
rotated=false;
break;
case 9:
temp[2].Offset(-1,-1);
if(this.CanRotate(temp))
{
this.pos[2].Offset(-1,-1);
this.shapeNO=10;
}
else
rotated=false;
break;
case 10:
temp[3].Offset(-1,1);
if(this.CanRotate(temp))
{
this.pos[3].Offset(-1,1);
this.shapeNO=11;
}
else
rotated=false;
break;
case 11:
temp[0].Offset(-1,1);
temp[2].Offset(1,1);
temp[3].Offset(1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-1,1);
this.pos[2].Offset(1,1);
this.pos[3].Offset(1,-1);
this.shapeNO=3;
}
else
rotated=false;
break;
case 12:
temp[0].Offset(-2,0);
temp[0].Offset(0,-2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-2,0);
this.pos[1].Offset(0,-2);
this.shapeNO=4;
}
else
{
rotated=false;
}
break;
case 13:
temp[2].Offset(1,0);
temp[3].Offset(1,-2);
if(this.CanRotate(temp))
{
this.pos[2].Offset(1,0);
this.pos[3].Offset(1,-2);
this.shapeNO=5;
}
else
{
rotated=false;
}
break;
case 14:
temp[2].Offset(1,0);
temp[3].Offset(-1,2);
if(this.CanRotate(temp))
{
this.pos[2].Offset(1,0);
this.pos[3].Offset(-1,2);
this.shapeNO=15;
}
else
{
rotated=false;
}
break;
case 15:
temp[1].Offset(-1,-1);
temp[2].Offset(-1,-1);
temp[3].Offset(0,-2);
if(this.CanRotate(temp))
{
this.pos[1].Offset(-1,-1);
this.pos[2].Offset(-1,-1);
this.pos[3].Offset(0,-2);
this.shapeNO=16;
}
else
{
rotated=false;
}
break;
case 16:
temp[0].Offset(-1,-1);
temp[1].Offset(-1,1);
temp[2].Offset(0,1);
temp[3].Offset(2,1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-1,-1);
this.pos[1].Offset(-1,1);
this.pos[2].Offset(0,1);
this.pos[3].Offset(2,1);
this.shapeNO=6;
}
else
{
rotated=false;
}
break;
case 17:
temp[0].Offset(1,-1);
temp[2].Offset(-1,1);
temp[3].Offset(-2,0);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,-1);
this.pos[2].Offset(-1,1);
this.pos[3].Offset(-2,0);
this.shapeNO=18;
}
else
{
rotated=false;
}
break;
case 18:
temp[0].Offset(-1,-1);
temp[2].Offset(1,1);
temp[3].Offset(0,2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-1,-1);
this.pos[2].Offset(1,1);
this.pos[3].Offset(0,2);
this.shapeNO=19;
}
else
{
rotated=false;
}
break;
case 19:
temp[0].Offset(-1,1);
temp[2].Offset(1,-1);
temp[3].Offset(2,0);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-1,1);
this.pos[2].Offset(1,-1);
this.pos[3].Offset(2,0);
this.shapeNO=7;
}
else
{
rotated=false;
}
break;
}
return rotated;
}
public bool Move(int direction)
{
int offx=0,offy=0;
if(direction==0 && this.CanMove(0))//左
{
offx=-1;
offy=0;
}
else if(direction==1 && this.CanMove(1))//右
{
offx=1;
offy=0;
}
else if(direction==2 && this.CanMove(2))//下
{
offx=0;
offy=1;
}
else
{
return false;
}
this.SetLastPos();
for(int i=0;i
pos[i].Offset(offx,offy);
}
return true;
}
public void Drop()
{
if(this.CanMove(2))
this.SetLastPos();
while(this.CanMove(2))
{
for(int i=0;i
pos[i].Offset(0,1);
}
}
}
private void DrawOne(int x,int y,Color color,Graphics gra)
{
gra.FillRectangle(new SolidBrush(color),x*unitPix+1,y*unitPix+1,this.unitPix-1,this.unitPix-1);
gra.DrawRectangle(new Pen(Color.Black,1),x*unitPix,y*unitPix,unitPix,unitPix);
}
public void DrawBlocks(Rectangle rec)
{
Graphics gra=this.con.CreateGraphics();
if(this.pos!=null)
{
foreach(Point p in this.pos)
{
this.DrawOne(p.X,p.Y,this.color,gra);
}
}
int x=rec.Height-1;//special for eraselast()'s size+1
int y=rec.Width-1;
if((x==this.unitPix && y==4*this.unitPix) || (x==2*unitPix && y==2*unitPix) || (x==2*unitPix && y==3*unitPix) || (x==3*unitPix && y==2*unitPix) || (x==4*unitPix && y==unitPix) )
return;
else
{
for(int i=this.huji.GetLength(0)-1;i>=0;i--)
{
for(int j=this.huji.GetLength(1)-1;j>=0;j--)
{
if(huji[i,j]==true && i*unitPix-rec.Left>-unitPix && i*unitPix
this.DrawOne(i,j,this.iori[i,j],gra);
}
}
}
}
}
public int BlockNum
{
get
{
return this.blockNum;
}
}
public int RowDelNum
{
get
{
return this.rowDelNum;
}
}
}
}
SettingForm.cs
----------------------->>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Tetris0._9
{
public class SettingForm : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private Keys[] keys;
private int level;
private bool trans;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.ComponentModel.Container components = null;
public SettingForm()
{
InitializeComponent();
this.keys=new Keys[5];
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SettingForm));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label6 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox5);
this.groupBox1.Controls.Add(this.textBox4);
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(16, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(168, 168);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "键盘设置";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(64, 124);
this.textBox5.Name = "textBox5";
this.textBox5.ReadOnly = true;
this.textBox5.Size = new System.Drawing.Size(72, 21);
this.textBox5.TabIndex = 9;
this.textBox5.Text = "";
this.textBox5.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox5.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox5_KeyDown);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(64, 100);
this.textBox4.Name = "textBox4";
this.textBox4.ReadOnly = true;
this.textBox4.Size = new System.Drawing.Size(72, 21);
this.textBox4.TabIndex = 8;
this.textBox4.Text = "";
this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox4.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox4_KeyDown);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(64, 76);
this.textBox3.Name = "textBox3";
this.textBox3.ReadOnly = true;
this.textBox3.Size = new System.Drawing.Size(72, 21);
this.textBox3.TabIndex = 7;
this.textBox3.Text = "";
this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox3.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox3_KeyDown);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(64, 52);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(72, 21);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 28);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(72, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// label5
//
this.label5.Location = new System.Drawing.Point(24, 80);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 16);
this.label5.TabIndex = 4;
this.label5.Text = "下移:";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label4
//
this.label4.Location = new System.Drawing.Point(24, 128);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 16);
this.label4.TabIndex = 3;
this.label4.Text = "丢下:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(40, 16);
this.label3.TabIndex = 2;
this.label3.Text = "旋转:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 16);
this.label2.TabIndex = 1;
this.label2.Text = "右移:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 16);
this.label1.TabIndex = 0;
this.label1.Text = "左移:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 272);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 272);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 2;
this.button2.Text = "取消";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.checkBox2);
this.groupBox2.Controls.Add(this.checkBox1);
this.groupBox2.Controls.Add(this.comboBox1);
this.groupBox2.Controls.Add(this.label6);
this.groupBox2.Location = new System.Drawing.Point(16, 184);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(168, 80);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "环境设置";
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10"});
this.comboBox1.Location = new System.Drawing.Point(64, 20);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(72, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(64, 16);
this.label6.TabIndex = 0;
this.label6.Text = "开始级别:";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// checkBox1
//
this.checkBox1.Enabled = false;
this.checkBox1.Location = new System.Drawing.Point(8, 48);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(56, 24);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "音效";
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(72, 48);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(80, 24);
this.checkBox2.TabIndex = 3;
this.checkBox2.Text = "透明模式";
this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
//
// SettingForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(200, 301);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SettingForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "设置";
this.TopMost = true;
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
this.DialogResult=DialogResult.OK;
this.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
public void SetOptions(Keys[] keys,int level,bool trans)
{
for(int i=0;i
this.keys[i]=keys[i];
}
textBox1.Text=keys[0].ToString();
textBox2.Text=keys[1].ToString();
textBox3.Text=keys[2].ToString();
textBox4.Text=keys[3].ToString();
textBox5.Text=keys[4].ToString();
this.level=level;
this.comboBox1.SelectedIndex=level-1;
this.trans=trans;
if(trans)
{
this.checkBox2.Checked=true;
}
}
public void GetOptions(ref Keys[] keys,ref int level,ref bool trans)
{
keys=this.keys;
level=this.level;
trans=this.trans;
}
private void ChangeKey(TextBox textBox,System.Windows.Forms.KeyEventArgs e,int i)
{
if((e.KeyValue>=32 && e.KeyValue<=40) || (e.KeyValue>=45 && e.KeyValue<=46) || (e.KeyValue>=48 && e.KeyValue<=57) || (e.KeyValue>=65 && e.KeyValue<=90) ||
(e.KeyValue>=96 && e.KeyValue<=107) || (e.KeyValue>=109 && e.KeyValue<=111) || (e.KeyValue>=186 && e.KeyValue<=192) ||
(e.KeyValue>=219 && e.KeyValue<=222))
{
textBox.Text=e.KeyCode.ToString();
this.keys[i]=e.KeyCode;
}
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
this.ChangeKey(textBox1,e,0);
}
private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
this.ChangeKey(textBox2,e,1);
}
private void textBox3_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
this.ChangeKey(textBox3,e,2);
}
private void textBox4_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
this.ChangeKey(textBox4,e,3);
}
private void textBox5_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
this.ChangeKey(textBox5,e,4);
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.level=comboBox1.SelectedIndex+1;
}
private void checkBox2_CheckedChanged(object sender, System.EventArgs e)
{
if(this.checkBox2.Checked==true)
{
this.trans=true;
}
else
this.trans=false;
}
}
}