C++俄罗斯方块代码.docx

上传人:西安人 文档编号:5013993 上传时间:2020-01-28 格式:DOCX 页数:55 大小:87.91KB
返回 下载 相关 举报
C++俄罗斯方块代码.docx_第1页
第1页 / 共55页
C++俄罗斯方块代码.docx_第2页
第2页 / 共55页
C++俄罗斯方块代码.docx_第3页
第3页 / 共55页
C++俄罗斯方块代码.docx_第4页
第4页 / 共55页
C++俄罗斯方块代码.docx_第5页
第5页 / 共55页
点击查看更多>>
资源描述

《C++俄罗斯方块代码.docx》由会员分享,可在线阅读,更多相关《C++俄罗斯方块代码.docx(55页珍藏版)》请在三一文库上搜索。

1、Block.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using System.Windows.Forms;namespace Tetris / / 砖?块的?类 / class Block protected Point structArr;/储存?砖?块组成信?息的?坐?标数y组 protected int _xPos;/研D究?砖?块顶点?所在游?戏界?面?的?x坐?标 protected int _yPos;/研D究?砖

2、?块顶点?所在游?戏界?面?的?y坐?标 protected Color _blockColor;/砖?块的?颜?色? protected Color _disapperColor;/擦出?颜?色? protected int rectPix;/每?个?单元a格?的?像?素? public Block()/默?认?构1造函数y,?声明此?函数y是?为a了?子类能顺3利?创建 / Block类的?构1造函数y的?介绍 / Block类的?构1造函数y / / 储存?砖?块各点?的?Point类 / 砖?块颜?色? / 擦出?颜?色? / 砖?块像?素? public Block(Point sa

3、,Color bColor,Color dColor,int pix) _blockColor = bColor; _disapperColor = dColor; rectPix = pix; structArr = sa; public Point thisint index get return structArrindex; #region 成员变?量?相应|属?性? public int Lengce get return structArr.Length; public int xPos get return _xPos; set _xPos = value; public int

4、 yPos get return _yPos; set _yPos = value; public Color BlockColor get return _blockColor; #endregion public void DeasilRotate()/顺3时针?旋y转a int temp; for(int i=0;istructArr.Length;+i) temp = structArri.X; structArri.X = structArri.Y; structArri.Y = -temp; public void ContraRotate()/逆?时针?旋y转a int temp

5、; for (int i = 0; i structArr.Length; +i) temp = structArri.X; structArri.X = -structArri.Y; structArri.Y = temp; private Rectangle PointToRect(Point p) return new Rectangle(_xPos + p.X) * rectPix + 1, (_yPos - p.Y) * rectPix + 1, rectPix - 2, rectPix - 2); / / 在指?定画-板?下?绘?制?砖?块 / / 一?个?用?于绘?制?的?Gra

6、phics对?象 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)/擦除y矩?形? SolidBrush sb = new SolidBrush(_disapperColor); foreach (Point p in structArr) lock (

7、gp) gp.FillRectangle(sb, PointToRect(p); BlockGroup.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using System.Collections;namespace Tetris / / 用?于砖?块生产的?类 / class BlockGroup private InfoArr info;/存?放?所有D砖?块样式?信?息 private Color disapperColor

8、;/背3景色? private int rectPix;/单元a格?像?素? public BlockGroup()/构1造函数y Config config = new Config(); config.LoadFromXmlFile(); /info = new InfoArr(); info = config.Info; disapperColor = config.BackColor; rectPix = config.RectPix; public Block GetAblock()/从砖?块中D随?机抽取?一?个?砖?块样式?并返回? Random rd = new Random(

9、);/声明一?个?产生随?机数y的?类 int keyOrder = rd.Next(0, info.Lenth);/产生一?个?随?机数y(.Next(0,3)产生的?是?0,1,2) BitArray ba = infokeyOrder.Id;/抽取?的?砖?块样式?赋3给?BitArray类的?对?象ba int strNum = 0;/确定这a个?砖?块样式?中D被?填?充?的?方?块样式? foreach(bool b in ba)/即需要a确定Point数y组的?长度 if(b) strNum+; /*创建一?个?Point数y组并确定其?长度,以?创建新?的?Block*/ Po

10、int structArr = new PointstrNum; int k = 0; for(int j=0;jba.Length;+j)/循-环给?Point数y组StructArr赋3坐?标值 if(baj) structArrk.X = j % 5 - 2; structArrk.Y = 2 - j / 5; +k; /*创建新?砖?块并返回?*/ return new Block(structArr, infokeyOrder.BColor, disapperColor, rectPix); BlockInfo.cs:using System;using System.Collect

11、ions.Generic;using System.Linq;using System.Text;using System.Collections;using System.Drawing;namespace Tetris class BlockInfo private BitArray _id; private Color _bColor; public BlockInfo(BitArray id, Color bColor) _id = id; _bColor = bColor; public BitArray Id get return _id; set _id = value; pub

12、lic 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(); BlockSet.xml: 0000001100011000000000000 -

13、65536 0000000100001000010000100 -16711936 0000000000011100010000000 -8323073 0000000100001100001000000 -16776961 0000000100011000100000000 -32704 0000000000001100010000100 -32513 0000000000011000010000100 -8372160 83 16 65 68 32 87 15 25 20 -16777216 Config.cs:using System;using System.Collections.G

14、eneric;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Reflection;using System.Collections;using System.Xml;namespace Tetris class Config #region 私?有D变?量?及相应|属?性? private Keys _downKey; pub

15、lic Keys DownKey get return _downKey; set _downKey = value; private Keys _dropKey; public Keys DropKey get return _dropKey; set _dropKey = value; private Keys _moveLeftKey; public Keys MoveLeftKey get return _moveLeftKey; set _moveLeftKey = value; private Keys _moveRightKey; public Keys MoveRightKey

16、 get return _moveRightKey; set _moveRightKey = value; private Keys _deasilRotateKey; public Keys DeasilRotateKey get return _deasilRotateKey; set _deasilRotateKey = value; private Keys _contraRotateKey; public Keys ContraRotateKey get return _contraRotateKey; set _contraRotateKey = value; private in

17、t _coorWidth; public int CoorWidth get return _coorWidth; set if (value = 10 & value = 15 & value = 10 & value = 30) _rectPix = value; private Color _backColor; public Color BackColor get return _backColor; set _backColor = value; private InfoArr info = new InfoArr(); public InfoArr Info getreturn i

18、nfo; setinfo=value; #endregion #region 从Xml中D读取?信?息 public void LoadFromXmlFile() 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 =

19、 new XmlTextReader(sm); string key = ; try while (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=

20、 (Keys)Convert.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 = MoveRightKe

21、y) _moveRightKey = (Keys)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().

22、Trim(); else if (reader.Name = CoorWidth) _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

23、 if (reader.Name = BackColor) _backColor=Color.FromArgb(Convert.ToInt32(reader.ReadElementString().Trim(); catch(Exception ex) MessageBox.Show(ex.ToString(),Error); finally if (reader != null) reader.Close(); #endregion #region 把?设置?保存?成Xml信?息 public void SaveToXmlFile() XmlDocument doc = new XmlDoc

24、ument();/在内存?中D创建xml文?件t doc.LoadXml();/在内存?中D载?入?中D的?内容Y XmlNode root = doc.SelectSingleNode(BlockSet);/创建BlockSet的?根结点? for(int i = 0; iInfo.Lenth;i+)/用?循-环创建每?一?个?砖?块样式?结点? XmlElement xmlType = doc.CreateElement(Type);/创建结点? XmlElement xmlID = doc.CreateElement(ID);/创建结点? /*在结点?中D添加信?息,把?ArriList类Infoi(Object类)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 商业贸易


经营许可证编号:宁ICP备18001539号-1