《C++俄罗斯方块程序设计详细说明.doc》由会员分享,可在线阅读,更多相关《C++俄罗斯方块程序设计详细说明.doc(34页珍藏版)》请在三一办公上搜索。
1、该实验制作的是小游戏-俄罗斯方块1.可实现以下基本功能:用户可自定义添加或删除方块样式及颜色;用户可自定义修改游戏背景颜色及按键设置。2.另增加了几个功能:按键设置改变后点击保存,会弹出对话框提示“保存成功” ;点击“开始”运行游戏,背景音乐自动播放,点击暂停后,背景音乐也随之停止;每消除一行,会有特效声音提示消除成功;根据消行多少会自动加分并显示。游戏界面效果图如下:配置窗体效果图如下:砖块样式配置效果图如下:游戏设计分为如下九个部分:一, 新建窗体“配置窗体”(TrmConfig)添加TabControl控件(1) 砖块样式配置I.abel控件(lblMode)点击“事件”,选择“Pain
2、t”Graphics gp=e.Graphics;gp.Clear(Color.Black);Pen p=new Pen(Color.White);for (int i=31;i155;i=i+31)gp.DrawLine(p,1,i,155,i);for (int i=31;i155;i=i+31)gp.DrawLine(p,i,1,i,155);SolidBrush s=new SolidBrush(blockColor);for (int x=0;x5;x+)for(int y=0;y5;y+)if(struArrx,y)gp.FillRectangle(s,31*x+1,31*y+1,
3、30,30);点击“事件”,选择“MouseClick”private bool, struArr=new bool5,5;private Color blockColor=Color.Red;-if (e.Button!=MouseButtons.Left)return;int xPos,yPos;xPos=e.X/31;yPos=e.Y/31;struArrxPos,yPos=!struArrxPos,yPos;bool b=struArrxPos,yPos;Graphics gp=lblMode.CreateGraphics();SolidBrush s=new SolidBrush(b
4、 ? 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)点击“事件”,选择“ItemSel
5、ectionChanged”if (e.IsSelected)blockColor=Color.FromArgb(int.Parse(e.Item.SubItems1.Text);lblColor.BackColor=blockColor;string s=e.Item.SubItems0.Text;for(int i=0;is.Length;i+)struArri/5,i%5=(si=1)?true:false;lblMode.Invalidate();IV.“添加”按钮(btnAdd)bool isEmpty=false;foreach (bool i in struArr)if(i)is
6、Empty=true;break;if (!isEmpty)MessageBox.Show(图案为空,请先用鼠标点击左边窗口绘制图案!,提示窗口,MessageBoxButtons.OK,MessageBoxIcon.Information);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
7、.SubItems0.Text=blockString)MessageBox.Show(该图案已经存在!,提示窗口,MessageBoxButtons.OK,MessageBoxIcon.Information);return;ListViewItem myItem=new ListViewItem();myItem=lsvBlockSet.Items.Add(blockString);myItem.SubItems.Add(Convert.ToString(blockColor.ToArgb();V.“删除”按钮(btnDel)if(lsvBlockSet.SelectedItems.Cou
8、nt=0)MessageBox.Show(请在右边窗口选择一个条目进行删除!,提示窗口,MessageBoxButtons.OK,MessageBoxIcon.Information);return;lsvBlockSet.Items.Remove(lsvBlockSet.SelectedItems0);btnClear.PerformClick();VI.“清空”(btnClear)for (int x=0;x5;x+)for(int y=0;y=33 & e.KeyValue=45 & e.KeyValue=48 & e.KeyValue=65 & e.KeyValue=96 & e.Ke
9、yValue=109 & e.KeyValue=186 & 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()
10、;(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.Load
11、FromXmlFile();InfoArr info = config.Info;ListViewItem myItem=new ListViewItem();for(int i=0;iinfo.Length;i+)myItem=lsvBlockSet.Items.Add(infoi.GetIdStr();myItem.SubItems.Add(infoi.GetColorStr();读快捷键及环境设置参数txtDown.Text=(Keys)config.DownKey).ToString();txtDown.Tag=config.DownKey;txtDrop.Text=(Keys)con
12、fig.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.Deasil
13、RotateKey;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.保存更改选择“事件”,选择“clic
14、k”保存用户更改的设置InfoArr info=new InfoArr();foreach(ListViewItem item in lsvBlockSet.Items)info.Add(item.SubItems0.Text,item.SubItems1.Text);config.Info=info;config.DownKey=(Keys)txtDown.Tag;config.DropKey=(Keys)txtDrop.Tag;config.MoveLeftKey=(Keys)txtLeft.Tag;config.MoveRightKey=(Keys)txtRight.Tag;config
15、.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();二, 信息保存解决方案中添加BlockSe
16、t.Xml文件 0000001100011000000000000 -65536 0000000000111100000000000 -16711936 0000000000001100010000100 -8323073 0010000100001100000000000 -16776961 0000000100001100010000000 -32704 0000000100001100001000000 -32513 0000000010001100010000000 -8372160 83 88 65 68 99 97 15 25 20 -16777216 三, 一个砖块的信息类解决方
17、案中添加BlockInfo.cs类private BitArray _id;private Color _bColor;public BlockInfo(BitArray id,Color bColor)_id=id;_bColor=bColor;public BitArray IDgetreturn _id;set_id=value;public Color BColorgetreturn _bColor;set_bColor=value;public string GetIdStr()StringBuilder s=new StringBuilder(25);foreach(bool b
18、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 inf
19、o = new ArrayList();private int _length=0;public int Lengthgetreturn _length;public BlockInfo thisint indexgetreturn (BlockInfo)infoindex;public string thisstring idsetif(value =)return;for(int i=0;iusing System.Collections;/由于使用到了MessageBox,要添加命名空间using System.Windows.Forms;由于使用到了Color类,还要添加using S
20、yetem.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=10&value=15&value=10&value=30)_rectPix=value;public Color BackColorgetreturn _backColor;set_backColor=val
21、ue;public InfoArr Infogetreturn info;setinfo=value;#endregionII.从xml读取信息public void LoadFromXmlFile()/从xml读取信息XmlTextReader reader;if (File.Exists(BlockSet.xml)reader=new XmlTextReader(BlockSet.xml);elseAssembly asm=Assembly.GetExecutingAssembly();Stream sm=asm.GetManifestResourceStream(Tetris.Block
22、Set.xml);reader=new XmlTextReader(sm);string key=;trywhile(reader.Read()if(reader.NodeType =XmlNodeType.Element)if(reader.Name =ID)key=reader.ReadElementString().Trim();info.Add(key,);else if (reader.Name =Color)infokey=reader.ReadElementString().Trim();else if (reader.Name =DownKey)_downKey=(Keys)C
23、onvert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =DropKey)_dropKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =MoveLeftKey)_moveLeftKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =MoveRightKey)_moveRightKey=(Key
24、s)Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =DeasilRotateKey)_deasilRotateKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =ContraRotateKey)_contraRotateKey=(Keys)Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =Coor
25、Width)_coorWidth=Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =CoorHeight)_coorHeight=Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =RectPix)_rectPix=Convert.ToInt32(reader.ReadElementString().Trim();else if (reader.Name =BackColor)_backColor=Color.
26、FromArgb(Convert.ToInt32(reader.ReadElementString().Trim();catch(Exception ex)MessageBox.Show(ex.ToString();finallyif(reader !=null)reader.Close();III.把信息保存为xml文件public void SaveToXmlFile() /把信息保存为xml文件XmlDocument doc =new XmlDocument();doc.LoadXml();XmlNode root=doc.SelectSingleNode(BlockSet);for(i
27、nt i=0;iinfo.Length;i+)XmlElement xelType=doc.CreateElement(Type);XmlElement xelID=doc.CreateElement(ID);xelID.InnerText=(BlockInfo)infoi).GetIdStr();xelType.AppendChild(xelID);XmlElement xelColor=doc.CreateElement(Color);xelColor.InnerText=(BlockInfo)infoi).GetColorStr();xelType.AppendChild(xelColo
28、r);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(_
29、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=Conver
30、t.ToInt32(_moveRightKey).ToString();xelKey.AppendChild(xelMoveRightKey);XmlElement xelDeasilRotateKey=doc.CreateElement(DeasilRotateKey);xelDeasilRotateKey.InnerText=Convert.ToInt32(_deasilRotateKey).ToString();xelKey.AppendChild(xelDeasilRotateKey);XmlElement xelContraRotateKey=doc.CreateElement(Co
31、ntraRotateKey);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.ToSt
32、ring();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.CreateEle