C#做的俄罗斯方块游戏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 RussiaBlock
{
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 ControlForm 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.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.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private AxWMPLib.AxWindowsMediaPlayer axMediaPlayer1;
private System.Windows.Forms.MenuItem menuItem7;
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.ini");
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=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.ini");
}
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.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.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.axMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.panel3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axMediaPlayer1)).BeginInit();
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, 48);
this.panel2.TabIndex = 1;
this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint);
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.label1.Font = new System.Drawing.Font("楷体_GB2312", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(264, 248);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 25);
this.label1.TabIndex = 2;
this.label1.Text = "手速:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.label2.Font = new System.Drawing.Font("楷体_GB2312", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(264, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 25);
this.label2.TabIndex = 3;
this.label2.Text = "块数:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.AutoSize = true;
this.label3.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.label3.Font = new System.Drawing.Font("楷体_GB2312", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.ForeColor = System.Drawing.Color.Red;
this.label3.Location = new System.Drawing.Point(264, 208);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(66, 25);
this.label3.TabIndex = 4;
this.label3.Text = "行数:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.ForeColor = System.Drawing.Color.Red;
this.button1.Location = new System.Drawing.Point(288, 296);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 10;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button3.ForeColor = System.Drawing.Color.Red;
this.button3.Location = new System.Drawing.Point(288, 352);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(72, 24);
this.button3.TabIndex = 7;
this.button3.Text = "设置";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button4.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button4.ForeColor = System.Drawing.Color.Red;
this.button4.Location = new System.Drawing.Point(288, 408);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(72, 24);
this.button4.TabIndex = 8;
this.button4.Text = "暂停";
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, 72);
this.panel3.TabIndex = 9;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(352, 408);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(1, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// label4
//
this.label4.AutoSize = true;
this.label4.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.label4.Font = new System.Drawing.Font("楷体_GB2312", 14.25F, 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(264, 128);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(66, 25);
this.label4.TabIndex = 11;
this.label4.Text = "级别:";
this.label4.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// label5
//
this.label5.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.label5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.label5.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label5.ForeColor = System.Drawing.Color.Blue;
this.label5.Location = new System.Drawing.Point(328, 488);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 16);
this.label5.TabIndex = 12;
this.label5.Text = "版本:1.0";
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);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem5});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "游戏设置";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "退出";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem6});
this.menuItem3.Text = "控制";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "操作控制";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "打开音乐";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem5
//
this.menuItem5.Index = 2;
this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem7});
this.menuItem5.Text = "帮助";
//
// axMediaPlayer1
//
this.axMediaPlayer1.Enabled = true;
this.axMediaPlayer1.Location = new System.Drawing.Point(272, 456);
this.axMediaPlayer1.Name = "axMediaPlayer1";
this.axMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMediaPlayer1.OcxState")));
this.axMediaPlayer1.Size = new System.Drawing.Size(40, 48);
this.axMediaPlayer1.TabIndex = 14;
//
// menuItem7
//
this.menuItem7.Index = 0;
this.menuItem7.Text = "帮助";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Window;
this.ClientSize = new System.Drawing.Size(400, 517);
this.Controls.Add(this.axMediaPlayer1);
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.IsMdiContainer = true;
this.KeyPreview = true;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Name = "MainForm";
this.Text = "俄罗斯方块";
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);
((System.ComponentModel.ISupportInitialize)(this.axMediaPlayer1)).EndInit();
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 ControlForm();
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="开源";
}
private void label5_MouseLeave(object sender, System.EventArgs e)
{
this.label5.Text="版本:1.0";
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
Form controlForm = new ControlForm();
controlForm.Show();
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.AddExtension = true;
ofDialog.CheckFileExists = true;
ofDialog.CheckPathExists = true;
//the next sentence must be in single line
ofDialog.Filter = "MP3文件(*.mp3)|*.mp3|Audio文件(*.avi)|*.avi|VCD文件(*.dat)|*.dat|WAV文件(*.wav)|*.wav|所有文件 (*.*)|*.*";
ofDialog.DefaultExt = "*.mp3";
if(ofDialog.ShowDialog() == DialogResult.OK)
{
this.axMediaPlayer1.URL= ofDialog.FileName;
}
}
}
}
1