C#俄罗斯方块(附代码)

  • 格式:doc
  • 大小:170.00 KB
  • 文档页数:35

C#俄罗斯方块程序设计姓名:学号:专业:计算机科学与技术学院班级:级班时间:2009—2010年第一学期该实验制作的是小游戏----俄罗斯方块1.可实现以下基本功能:用户可自定义添加或删除方块样式及颜色;用户可自定义修改游戏背景颜色及按键设置。

2.另增加了几个功能:按键设置改变后点击保存,会弹出对话框提示“保存成功”;点击“开始”运行游戏,背景音乐自动播放,点击暂停后,背景音乐也随之停止;每消除一行,会有特效声音提示消除成功;根据消行多少会自动加分并显示。

游戏界面效果图如下:配置窗体效果图如下:砖块样式配置效果图如下:游戏设计分为如下九个部分:一,新建窗体“配置窗体”(TrmConfig)添加TabControl控件(1)砖块样式配置I.abel控件(lblMode)点击“事件”,选择“Paint”Graphics gp=e.Graphics;gp.Clear(Color.Black);Pen p=new Pen(Color.White);for (int i=31;i<155;i=i+31)gp.DrawLine(p,1,i,155,i);for (int i=31;i<155;i=i+31)gp.DrawLine(p,i,1,i,155);SolidBrush s=new SolidBrush(blockColor);for (int x=0;x<5;x++){for(int y=0;y<5;y++){if(struArr[x,y]){gp.FillRectangle(s,31*x+1,31*y+1,30,30);}}}点击“事件”,选择“MouseClick”private bool[,] struArr=new bool[5,5];private Color blockColor=Color.Red;--------------------------------------------------------if (e.Button!=MouseButtons.Left)return;int xPos,yPos;xPos=e.X/31;yPos=e.Y/31;struArr[xPos,yPos]=!struArr[xPos,yPos];bool b=struArr[xPos,yPos];Graphics gp=lblMode.CreateGraphics();SolidBrush s=new SolidBrush(b ? blockColor:Color.Black); gp.FillRectangle(s,31*xPos+1,31*yPos+1,30,30);gp.Dispose();II.添加ColorDialog控件添加label(lblColor)控件点击“事件”,选择“click”colorDialog1.ShowDialog();blockColor=colorDialog1.Color;lblColor.BackColor=colorDialog1.Color;lblMode.Invalidate();III.添加listView控件(lsvBlockSet)点击“事件”,选择“ItemSelectionChanged”if (e.IsSelected){blockColor=Color.FromArgb(int.Parse(e.Item.SubItems[1].Text));lblColor.BackColor=blockColor;string s=e.Item.SubItems[0].Text;for(int i=0;i<s.Length;i++){struArr[i/5,i%5]=(s[i]=='1')?true:false;}lblMode.Invalidate();}IV.“添加”按钮(btnAdd)bool isEmpty=false;foreach (bool i in struArr){if(i){isEmpty=true;break;}}if (!isEmpty){MessageBox.Show("图案为空,请先用鼠标点击左边窗口绘制图案!","提示窗口", MessageBoxButtons.OK,rmation);return;}StringBuilder sb=new StringBuilder(25);foreach (bool i in struArr){sb.Append(i?"1":"0");}string blockString=sb.ToString();foreach(ListViewItem item in lsvBlockSet.Items){if (item.SubItems[0].Text==blockString){MessageBox.Show("该图案已经存在!","提示窗口",MessageBoxButtons.OK,rmation);return;}}ListViewItem myItem=new ListViewItem();myItem=lsvBlockSet.Items.Add(blockString);myItem.SubItems.Add(Convert.ToString(blockColor.ToArgb()));V.“删除”按钮(btnDel)if(lsvBlockSet.SelectedItems.Count==0){MessageBox.Show("请在右边窗口选择一个条目进行删除!","提示窗口", MessageBoxButtons.OK,rmation);return;}lsvBlockSet.Items.Remove(lsvBlockSet.SelectedItems[0]);btnClear.PerformClick();VI.“清空”(btnClear)for (int x=0;x<5;x++){for(int y=0;y<5;y++){struArr[x,y]=false;}}lblMode.Invalidate();VII.“修改”(btnUpdate)if(lsvBlockSet.SelectedItems.Count==0){MessageBox.Show("请在右边窗口选择一个条目进行修改!","提示窗口", MessageBoxButtons.OK,rmation);return;}bool isEmpty=false;foreach (bool i in struArr){if(i){isEmpty=true;break;}}if (!isEmpty){MessageBox.Show("图案为空,请先用鼠标点击左边窗口绘制图案再进行修改!","提示窗口",MessageBoxButtons.OK,rmation);return;}StringBuilder sb=new StringBuilder(25);foreach (bool i in struArr){sb.Append(i?"1":"0");}lsvBlockSet.SelectedItems[0].SubItems[0].Text=sb.ToString();lsvBlockSet.SelectedItems[0].SubItems[1].Text=Convert.ToString(blockColor.ToArgb());(2)参数配置I.添加GroupBox1控件(gbKeySet)“键盘设置”i.拖入六个label控件ii.拖入六个TextBox控件(改名)全部选中,选择“事件”,选择“KeyDown”if((e.KeyValue>=33 && e.KeyValue<=36)||(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)){foreach(Control c in gbKeySet.Controls){Control TempC=c as TextBox;if(TempC!=null &&((TextBox)TempC).Text!=""){if(((int)((TextBox)TempC).Tag)==e.KeyValue){((TextBox)TempC).Text="";((TextBox)TempC).Tag=Keys.None;}}}((TextBox)sender).Text=e.KeyCode.ToString(); ((TextBox)sender).Tag=(Keys)e.KeyValue;}II.添加GroupBox2控件(gbEnvironmentSet)“环境设置”i.拖入四个label控件ii.拖入三个TextBox控件(改名)一个label控件(lblBackColor)选择“事件”,选择“click”colorDialog1.ShowDialog();lblBackColor.BackColor=colorDialog1.Color;III.i.参数初始化设置为配置窗体的代码窗口添加私有成员变量private Config config=new Config();初始化config.LoadFromXmlFile();InfoArr info = ;ListViewItem myItem=new ListViewItem();for(int i=0;i<info.Length;i++){myItem=lsvBlockSet.Items.Add(info[i].GetIdStr()); myItem.SubItems.Add(info[i].GetColorStr());}读快捷键及环境设置参数txtDown.Text=((Keys)config.DownKey).ToString(); txtDown.Tag=config.DownKey;txtDrop.Text=((Keys)config.DropKey).ToString(); txtDrop.Tag=config.DropKey;txtLeft.Text=((Keys)config.MoveLeftKey).ToString(); txtLeft.Tag=config.MoveLeftKey;txtRight.Text=((Keys)config.MoveRightKey).ToString(); txtRight.Tag=config.MoveRightKey;txtDeasil.Text=((Keys)config.DeasilRotateKey).ToString(); txtDeasil.Tag=config.DeasilRotateKey;txtContra.Text=((Keys)config.ContraRotateKey).ToString(); txtContra.Tag=config.ContraRotateKey;txtCoorWidth.Text=config.CoorWidth.ToString(); txtCoorHeight.Text=config.CoorHeight.ToString();txtRectPix.Text=config.RectPix.ToString(); lblBackColor.BackColor=config.BackColor;ii.保存更改选择“事件”,选择“click”保存用户更改的设置InfoArr info=new InfoArr();foreach(ListViewItem item in lsvBlockSet.Items){info.Add(item.SubItems[0].Text,item.SubItems[1].Text); }=info;config.DownKey=(Keys)txtDown.Tag;config.DropKey=(Keys)txtDrop.Tag;config.MoveLeftKey=(Keys)txtLeft.Tag;config.MoveRightKey=(Keys)txtRight.Tag;config.DeasilRotateKey=(Keys)txtDeasil.Tag; config.ContraRotateKey=(Keys)txtContra.Tag; config.CoorWidth=int.Parse(txtCoorWidth.Text); config.CoorHeight=int.Parse(txtCoorHeight.Text); config.RectPix=int.Parse(txtRectPix.Text);config.BackColor=lblBackColor.BackColor;config.SaveToXmlFile();二,信息保存解决方案中添加BlockSet.Xml文件<BlockSet><Type><ID>0000001100011000000000000</ID><Color>-65536</Color></Type><Type><ID>0000000000111100000000000</ID><Color>-16711936</Color></Type><Tppe><ID>0000000000001100010000100</ID><Color>-8323073</Color></Tppe><Type><ID>0010000100001100000000000</ID><Color>-16776961</Color></Type><Type><ID>0000000100001100010000000</ID><Color>-32704</Color></Type><Type><ID>0000000100001100001000000</ID><Color>-32513</Color></Type><Type><ID>0000000010001100010000000</ID><Color>-8372160</Color></Type><Key><DownKey>83</DownKey><DropKey>88</DropKey><MoveLeftKey>65</MoveLeftKey><MoveRightKey>68</MoveRightKey><DeasilRotateKey>99</DeasilRotateKey><ContraRotateKey>97</ContraRotateKey> </Key><Surface><CoorWidth>15</CoorWidth><CoorHeight>25</CoorHeight><RectPix>20</RectPix><BackColor>-16777216</BackColor></Surface></BlockSet>三,一个砖块的信息类解决方案中添加BlockInfo.cs类private BitArray _id;private Color _bColor;public BlockInfo(BitArray id,Color bColor){_id=id;_bColor=bColor;}public BitArray ID{get{return _id;}{_id=value;}}public Color BColor{get{return _bColor;}set{_bColor=value;}}public string GetIdStr(){StringBuilder s=new StringBuilder(25); foreach(bool b in _id){s.Append(b?"1":"0");}return s.ToString();}public string GetColorStr(){return Convert.ToString(_bColor.ToArgb()); }由于使用到了Color类,要添加命名空间using Syetem.Drawing;由于使用了StringBuilder类以及BitArray类using System.Collections.Generic;-->using System.Collections;四,多个砖块信息类解决方案中添加InfoArr.cs类I .各个方块信息类private ArrayList info = new ArrayList(); private int _length=0;public int Length{getreturn _length;}}public BlockInfo this[int index]{get{return (BlockInfo)info[index];}}public string this[string id]{set{if(value ==""){return;}for(int i=0;i<info.Count;i++){if(((BlockInfo)info[i]).GetIdStr()==id){try{((BlockInfo)info[i]).BColor=Color.FromArgb(Convert.ToInt32(value));}catch(System.FormatException){MessageBox.Show("颜色信息错误!请删除BlockSet.xml文件,并重新启动程序","错误信息",MessageBoxButtons.OK,MessageBoxIcon.Error);}}}}}//由于使用到了ArrayList,要添加命名空间using System.Collections.Generic;-->using System.Collections;//由于使用到了MessageBox,要添加命名空间using System.Windows.Forms;由于使用到了Color类,还要添加using Syetem.Drawing;II.添加字符转换成员方法private BitArray StrToBit(string id){if(id.Length !=25){throw new System.FormatException("砖块样式信息不合法!请删除BlockSet.xml文件,并重新启动程序");}BitArray ba =new BitArray(25);for(int i=0;i<25;i++){ba[i]=(id[i]=='0')?false:true;}return ba;}III.添加砖块信息成员方法public void Add(BitArray id,Color bColor){if(id.Length !=25){throw new System.FormatException("砖块样式信息不合法!请删除BlockSet.xml文件,并重新启动程序");}info.Add(new BlockInfo(id,bColor));_length++;}IV.对上面方法重载public void Add(string id,string bColor){Color temp;if(!(bColor=="")){temp=Color.FromArgb(Convert.ToInt32(bColor));}else{temp=Color.Empty;}info.Add(new BlockInfo(StrToBit(id),temp));_length++;}五,新建配置类解决方案中添加 Config.cs类private Keys _downKey;private Keys _dropKey;private Keys _moveLeftKey; private Keys _moveRightKey; private Keys _deasilRotateKey; private Keys _contraRotateKey; private int _coorWidth;private int _coorHeight;private int _rectPix;private Color _backColor;private InfoArr info=new InfoArr();I.私有变量属性#region 私有变量相应的属性public Keys DownKey{get{return _downKey;}set{_downKey=value;}}public Keys DropKey{get{return _dropKey;}set{_dropKey=value;}}public Keys MoveLeftKey{get{}set{_moveLeftKey=value;}}public Keys MoveRightKey {get{return _moveRightKey;}set{_moveRightKey=value;}}public Keys DeasilRotateKey {get{return _deasilRotateKey;}set{_deasilRotateKey=value;}}public Keys ContraRotateKey {get{return _contraRotateKey;}set{_contraRotateKey=value;}}public int CoorWidth{get{}set{if(value>=10&&value<=50) _coorWidth=value;}}public int CoorHeight{get{return _coorHeight;}set{if(value>=15&&value<=50) _coorHeight=value;}}public int RectPix{get{return _rectPix;}set{if(value>=10&&value<=30) _rectPix=value;}}public Color BackColor {get{return _backColor;}set{_backColor=value;}}public InfoArr Info{get{return info;}set{info=value;}}#endregionII.从xml读取信息public void LoadFromXmlFile()//从xml读取信息{XmlTextReader reader;if (File.Exists("BlockSet.xml")){reader=new XmlTextReader("BlockSet.xml");}else{Assembly asm=Assembly.GetExecutingAssembly();Stream sm=asm.GetManifestResourceStream("Tetris.BlockSet.xml"); reader=new XmlTextReader(sm);}string key="";try{while(reader.Read()){if(reader.NodeType ==XmlNodeType.Element){if( =="ID"){key=reader.ReadElementString().Trim();info.Add(key,"");}else if ( =="Color"){info[key]=reader.ReadElementString().Trim();}else if ( =="DownKey"){_downKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="DropKey"){_dropKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="MoveLeftKey"){_moveLeftKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="MoveRightKey"){_moveRightKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="DeasilRotateKey"){_deasilRotateKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="ContraRotateKey"){_contraRotateKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="CoorWidth"){_coorWidth=Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="CoorHeight"){_coorHeight=Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="RectPix"){_rectPix=Convert.ToInt32(reader.ReadElementString().Trim());}else if ( =="BackColor"){_backColor=Color.FromArgb(Convert.ToInt32(reader.ReadElementString().Trim())); }}}}catch(Exception ex){MessageBox.Show(ex.ToString());}finally{if(reader !=null)reader.Close();}III.把信息保存为xml文件public void SaveToXmlFile() //把信息保存为xml文件{XmlDocument doc =new XmlDocument();doc.LoadXml("<BlockSet></BlockSet>");XmlNode root=doc.SelectSingleNode("BlockSet");for(int i=0;i<info.Length;i++){XmlElement xelType=doc.CreateElement("Type");XmlElement xelID=doc.CreateElement("ID");xelID.InnerText=((BlockInfo)info[i]).GetIdStr();xelType.AppendChild(xelID);XmlElement xelColor=doc.CreateElement("Color");xelColor.InnerText=((BlockInfo)info[i]).GetColorStr();xelType.AppendChild(xelColor);root.AppendChild(xelType);}//写快捷键XmlElement xelKey=doc.CreateElement("Key");XmlElement xelDownKey=doc.CreateElement("DownKey"); xelDownKey.InnerText=Convert.ToInt32(_downKey).ToString(); xelKey.AppendChild(xelDownKey);XmlElement xelDropKey=doc.CreateElement("DropKey"); xelDropKey.InnerText=Convert.ToInt32(_dropKey).ToString();xelKey.AppendChild(xelDropKey);XmlElement xelMoveLeftKey=doc.CreateElement("MoveLeftKey"); xelMoveLeftKey.InnerText=Convert.ToInt32(_moveLeftKey).ToString(); xelKey.AppendChild(xelMoveLeftKey);XmlElement xelMoveRightKey=doc.CreateElement("MoveRightKey"); xelMoveRightKey.InnerText=Convert.ToInt32(_moveRightKey).ToString(); xelKey.AppendChild(xelMoveRightKey);XmlElement xelDeasilRotateKey=doc.CreateElement("DeasilRotateKey");xelDeasilRotateKey.InnerText=Convert.ToInt32(_deasilRotateKey).ToString(); xelKey.AppendChild(xelDeasilRotateKey);XmlElement xelContraRotateKey=doc.CreateElement("ContraRotateKey"); xelContraRotateKey.InnerText=Convert.ToInt32(_contraRotateKey).ToString(); xelKey.AppendChild(xelContraRotateKey);root.AppendChild(xelKey);//写界面信息XmlElement xelSurface=doc.CreateElement("Surface");XmlElement xelCoorWidth=doc.CreateElement("CoorWidth"); xelCoorWidth.InnerText= _coorWidth.ToString();xelSurface.AppendChild(xelCoorWidth);XmlElement xelCoorHeight=doc.CreateElement("CoorHeight"); xelCoorHeight.InnerText= _coorHeight.ToString();xelSurface.AppendChild(xelCoorHeight);XmlElement xelRectPix=doc.CreateElement("RectPix");xelRectPix.InnerText= _rectPix.ToString();xelSurface.AppendChild(xelRectPix);XmlElement xelBackColor=doc.CreateElement("BackColor"); xelBackColor.InnerText=_backColor.ToArgb().ToString();xelSurface.AppendChild(xelBackColor);root.AppendChild(xelSurface);doc.Save("BlockSet.xml");}六,新建方块类解决方案中添加Block.cs类protected Point[] structArr;protected int _xPos;protected int _yPos;protected Color _blockColor;protected Color disapperColor;protected int rectPix;public Block(){}public Block(Point[] sa, Color bColor, Color dColor, int pix) {_blockColor=bColor;disapperColor=dColor;rectPix=pix;structArr=sa;}public Point this[int index]{get{return structArr[index];}}public int Length{get{return structArr.Length;}}#region 成员变量相应的属性public int XPos{get{return _xPos;}set{_xPos=value;}}public int YPos{get{return _yPos;}set{_yPos=value;}}public Color BlockColor{get{return _blockColor;}}#endregion--------------------------------public void DeasilRotate()//顺时针旋转{int temp;for(int i=0;i<structArr.Length;i++){temp=structArr[i].X;structArr[i].X=structArr[i].Y;structArr[i].Y=-temp;}}public void ContraRotate()//逆时针旋转{int temp;for(int i=0;i<structArr.Length;i++){temp=structArr[i].X;structArr[i].X=-structArr[i].Y;structArr[i].Y=temp;}}private Rectangle PointToRect(Point p)//坐标点转化为画布坐标值{return new Rectangle((_xPos + p.X)*rectPix+1,(_yPos-p.Y)*rectPix+1,rectPix-2,rectPix-2);}public virtual void Paint(Graphics gp)//制定画板下绘制砖块{SolidBrush sb=new SolidBrush(_blockColor);foreach(Point p in structArr){lock(gp){gp.FillRectangle(sb,PointToRect(p));}}}public void erase(Graphics gp)//擦除矩形{SolidBrush sb=new SolidBrush(disapperColor); foreach(Point p in structArr){lock(gp){gp.FillRectangle(sb,PointToRect(p));}}}七,新建生产砖块的类解决方案中添加BlockGroup.cs类private InfoArr info;private Color disapperColor;private int rectPix;public BlockGroup(){Config config=new Config();config.LoadFromXmlFile();info=new InfoArr();info=;disapperColor=config.BackColor;rectPix=config.RectPix;}public Block GetABlock(){Random rd=new Random();int keyOrder=rd.Next(0,info.Length); BitAarry ba=info[keyOrder].ID;int struNum=0;foreach(bool b in ba){if(b){struNum++;}}Point[] structArr=new Point[struNum];int k=0;for(int j=0;j<ba.Length;j++){if(ba[j]){structArr[k].X=j/5-2;structArr[k].Y=2-j%5;k++;}}return new Block(structArr,info[keyOrder].BColor,disapperColor,rectPix); }八,新建为砖块活动定规则的类在解决方案中添加Palette.cs类private int _width=15;private int _height=25;private Color[,] coorArr;private Color disapperColor;private Graphics gpPalette;private Graphics gpReady;private BlockGroup bGroup;private Block runBlock;private Block readyBlock;private int rectPix;private System.Timers.Timer timerBlock;private int timeSpan=800;public Palette(int x,int y,int pix,Color dColor,Graphics gp,Graphics gr) {_width=x;_height=y;coorArr=new Color[_width,_height];disapperColor=dColor;gpPalette=gp;gpReady=gr;rectPix=pix;}public void Start(){bGroup=new BlockGroup();runBlock=bGroup.GetABlock();runBlock.XPos=_width/2;int y=0;for(int i=0;i<runBlock.Length;i++){if(runBlock[i].Y>y){y=runBlock[i].Y;}}runBlock.YPos=y;gpPalette.Clear(disapperColor);//清空画板runBlock.Paint(gpPalette);Thread.Sleep(20);readyBlock=bGroup.GetABlock();readyBlock.XPos=2;readyBlock.YPos=2;gpReady.Clear(disapperColor);//清空画板readyBlock.Paint(gpReady);timerBlock=new System.Timers.Timer(timeSpan);timerBlock.Elapsed +=new System.Timers.ElapsedEventHandler(OnTimedEvent); timerBlock.AutoReset=true;timerBlock.Start();}private void OnTimedEvent(object source,ElapsedEventArgs e){CheckAndOverBlock();Down();}public bool Down(){int xPos=runBlock.XPos;int yPos=runBlock.YPos+1;for(int i=0;i<runBlock.Length;i++){if(yPos-runBlock[i].Y>_height-1)return false;if(!coorArr[xPos+runBlock[i].X,yPos-runBlock[i].Y].IsEmpty)return false;}runBlock.erase(gpPalette);runBlock.YPos++;runBlock.Paint(gpPalette);return true;}public void Drop(){timerBlock.Stop();while(Down());timerBlock.Start();}public void MoveLeft(){int xPos=runBlock.XPos-1;int yPos=runBlock.YPos;for(int i=0;i<runBlock.Length;i++){if(xPos+runBlock[i].X<0)return;if(!coorArr[xPos+runBlock[i].X,yPos-runBlock[i].Y].IsEmpty) return;}runBlock.erase(gpPalette);runBlock.XPos--;runBlock.Paint(gpPalette);}public void MoveRight(){int xPos=runBlock.XPos+1;int yPos=runBlock.YPos;for(int i=0;i<runBlock.Length;i++){if(xPos+runBlock[i].X>_width-1)return;if(!coorArr[xPos+runBlock[i].X,yPos-runBlock[i].Y].IsEmpty) return;}runBlock.erase(gpPalette);runBlock.XPos++;runBlock.Paint(gpPalette);}public void DeasilRotate(){for(int i=0;i<runBlock.Length;i++){int x=runBlock.XPos+runBlock[i].Y;int y=runBlock.YPos+runBlock[i].X;if(x<0||x>_width-1)return;if(y<0||y>_height-1)return;if(!coorArr[x,y].IsEmpty)return;}runBlock.erase(gpPalette);runBlock.DeasilRotate();runBlock.Paint(gpPalette);}public void ContraRotate(){for(int i=0;i<runBlock.Length;i++){int x=runBlock.XPos-runBlock[i].Y;int y=runBlock.YPos-runBlock[i].X;if(x<0||x>_width-1)return;if(y<0||y>_height-1)return;if(!coorArr[x,y].IsEmpty)return;}runBlock.erase(gpPalette);runBlock.ContraRotate();runBlock.Paint(gpPalette);}private void PaintBackground(Graphics gp) {gp.Clear(Color.Black);for(int i=0;i<_height;i++){for(int j=0;j<_width;j++){if(!coorArr[j,i].IsEmpty){SolidBrush sb=new SolidBrush(coorArr[j,i]); gp.FillRectangle(sb,j*rectPix+1,i*rectPix+1,rectPix-2,rectPix-2);}}}}public void PaintPalette(Graphics gp){PaintBackground(gp);if(runBlock !=null){runBlock.Paint(gp);}}public void PaintReady(Graphics gp){if(readyBlock !=null){readyBlock.Paint(gp);}}public void CheckAndOverBlock(){bool over=false;for(int i=0;i<runBlock.Length;i++){int x=runBlock.XPos+runBlock[i].X;int y=runBlock.YPos-runBlock[i].Y;if(y==_height-1){over=true;break;}if(!coorArr[x,y+1].IsEmpty){over=true;break;}}if(over){for(int i=0;i<runBlock.Length;i++){coorArr[runBlock.XPos+runBlock[i].X,runBlock.YPos-runBlock[i].Y]=runBlock.BlockColor; }CheckAndDelFullRow();runBlock=readyBlock;runBlock.XPos=_width/2;int y=0;for(int i=0;i<runBlock.Length;i++){if(runBlock[i].Y>y){y=runBlock[i].Y;}}runBlock.YPos=y;for (int i=0;i<runBlock.Length;i++){if(!coorArr[runBlock.XPos+runBlock[i].X,runBlock.YPos-runBlock[i].Y].IsEmpty) {StringFormat drawFormat=new StringFormat();drawFormat.Alignment=StringAlignment.Center;gpPalette.DrawString("GAME OVER",new Font("Arial Black",25f),new SolidBrush(Color.White),new RectangleF(0,_height*rectPix/2-100,_width*rectPix,100), drawFormat);timerBlock.Stop();return;}}runBlock.Paint(gpPalette);readyBlock=bGroup.GetABlock();readyBlock.XPos=2;readyBlock.YPos=2;gpReady.Clear(Color.Black);readyBlock.Paint(gpReady);}}private void CheckAndDelFullRow(){int lowRow=runBlock.YPos-runBlock[0].Y;int highRow=lowRow;for(int i=1;i<runBlock.Length;i++){int y=runBlock.YPos-runBlock[i].Y;if(y<lowRow){lowRow=y;if(y>highRow){highRow=y;}}bool repaint=false;for(int i=lowRow;i<=highRow;i++) {bool rowFull=true;for(int j=0;j<_width;j++){if(coorArr[j,i].IsEmpty){rowFull=false;break;}}if(rowFull){repaint=true;for(int k=i;k>0;k--){for(int j=0;j<_width;j++){coorArr[j,k]=coorArr[j,k-1];}}for(int j=0;j<_width;j++){coorArr[j,0]=Color.Empty;}}}if(repaint){PaintBackground(gpPalette);}}public void Pause(){if(timerBlock.Enabled==true){timerBlock.Enabled=false;}public void EndPause(){if(timerBlock.Enabled==false){timerBlock.Enabled=true;}}public void Close(){timerBlock.Close();gpPalette.Dispose();gpReady.Dispose();}}}九,游戏运行窗体的设计解决方案中添加FrmTeris窗体I.PictureBox控件(pbRun)点击“事件”,选择“paint”,调用palette中相应的方法bel控件(lblReady)点击“事件”,选择“paint”,调用palette中相应的方法III.添加Panel控件IV.“开始”按钮(btnStart)if(p !=null){p.Close();}p=new Palette(paletteWidth,paletteHeight,rectPix,paletteColor, Graphics.FromHwnd(pbRun.Handle),Graphics.FromHwnd(lblReady.Handle));p.Start();V.单击“事件”,选择“load”Config config=new Config();config.LoadFromXmlFile();downKey=config.DownKey;dropKey=config.DropKey;moveLeftKey=config.MoveLeftKey;moveRightKey=config.MoveRightKey;deasilRotateKey=config.DeasilRotateKey; contraRotateKey=config.ContraRotateKey;paletteWidth=config.CoorWidth; paletteHeight=config.CoorHeight; paletteColor=config.BackColor;rectPix=config.RectPix;this.Width=paletteWidth*rectPix+152; this.Height=paletteHeight*rectPix+58; pbRun.Width=paletteWidth*rectPix; pbRun.Height=paletteHeight*rectPix;VI.单击“事件”,选择“KeyDown”if(e.KeyValue==32){e.Handled=true;}if(e.KeyCode==downKey){p.Down();}else if(e.KeyCode==dropKey){p.Drop();}else if(e.KeyCode==moveLeftKey){p.MoveLeft();}else if(e.KeyCode==moveRightKey){p.MoveRight();}else if(e.KeyCode==deasilRotateKey) {p.DeasilRotate();}else if(e.KeyCode==contraRotateKey) {p.ContraRotate();}VII.“暂停”按钮(btnPause)if(p==null){return;}if(btnPause.Text=="暂停"){p.Pause();btnPause.Text="继续";}else{p.EndPause();btnPause.Text="暂停";}VIII.“设置”按钮(btnConfig)if(btnPause.Text==""){btnPause.PerformClick();}using(FrmConfig frmConfig=new FrmConfig()){frmConfig.ShowDialog();}IX.单击“事件”,选择“Formclosing”z1if(p !=null){p.Close();}以下为改进方法:改进一:FrmConfig窗口中“保存”按钮代码中加入下面代码:MessageBox.Show("保存设置成功!", "提示");改进二:FrmTetris窗口中添加WindowsMediaPlayer控件在其“开始”按钮代码中添加如下代码:this.axWindowsMediaPlayer1.URL = "E:\\Tcl手机自带铃声.wav";在其“暂停”按钮代码中合适位置分别添加如下代码:this.axWindowsMediaPlayer1.URL = "";this.axWindowsMediaPlayer1.URL = "E:\\Tcl手机自带铃声.wav";改进三:在Palette代码中private void CheckAndDelFullRow()合适位置添加如下代码:System.Media.SoundPlayer s1 = new System.Media.SoundPlayer("E:\\CARTOON3.WAV"); s1.Play();改进四:在Palette代码中合适位置分别添加如下代码:public FrmTetris s;static int score = 0;score = 0;以下表示游戏结束后,计分归零s.lblScore.Text = "得分:" + score.ToString() + "分";int count = 0;以下表示每消几行,计分增加count++;if (count == 1)score += 10;if (count == 2)score += 30;if (count == 3)score += 60;if (count == 4)score += 100;if (repaint)s.lblScore.Text = "得分:" + score.ToString() + "分";最后在FrmTetris窗口的Load事件中添加如下代码:CheckForIllegalCrossThreadCalls = false;。