九九热最新网址,777奇米四色米奇影院在线播放,国产精品18久久久久久久久久,中文有码视频,亚洲一区在线免费观看,国产91精品在线,婷婷丁香六月天

C++俄羅斯方塊程序設(shè)計(jì)詳細(xì)說明

上傳人:仙*** 文檔編號:26954344 上傳時間:2021-08-15 格式:DOC 頁數(shù):34 大?。?69KB
收藏 版權(quán)申訴 舉報(bào) 下載
C++俄羅斯方塊程序設(shè)計(jì)詳細(xì)說明_第1頁
第1頁 / 共34頁
C++俄羅斯方塊程序設(shè)計(jì)詳細(xì)說明_第2頁
第2頁 / 共34頁
C++俄羅斯方塊程序設(shè)計(jì)詳細(xì)說明_第3頁
第3頁 / 共34頁

下載文檔到電腦,查找使用更方便

15 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《C++俄羅斯方塊程序設(shè)計(jì)詳細(xì)說明》由會員分享,可在線閱讀,更多相關(guān)《C++俄羅斯方塊程序設(shè)計(jì)詳細(xì)說明(34頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。

1、該實(shí)驗(yàn)制作的是小游戲-俄羅斯方塊1.可實(shí)現(xiàn)以下基本功能:用戶可自定義添加或刪除方塊樣式及顏色;用戶可自定義修改游戲背景顏色及按鍵設(shè)置。2.另增加了幾個功能:按鍵設(shè)置改變后點(diǎn)擊保存,會彈出對話框提示“保存成功” ;點(diǎn)擊“開始”運(yùn)行游戲,背景音樂自動播放,點(diǎn)擊暫停后,背景音樂也隨之停止;每消除一行,會有特效聲音提示消除成功;根據(jù)消行多少會自動加分并顯示。游戲界面效果圖如下:配置窗體效果圖如下:磚塊樣式配置效果圖如下:游戲設(shè)計(jì)分為如下九個部分:一, 新建窗體“配置窗體”(TrmConfig)添加TabControl控件(1) 磚塊樣式配置I.abel控件(lblMode)點(diǎn)擊“事件”,選擇“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);點(diǎn)擊“事件”,選擇“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)控件點(diǎn)擊“事件”,選擇“click”colorDialog1.ShowDialog();blockColor=colorDialog1.Color;lblColor.BackColor=colorDialog1.Color;lblMode.Invalidate();III.添加listView控件(lsvBlockSet)點(diǎn)擊“事件”,選擇“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(圖案為空,請先用鼠標(biāo)點(diǎn)擊左邊窗口繪制圖案!,提示窗口,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(該圖案已經(jīng)存在!,提示窗口,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(請?jiān)谟疫叴翱谶x擇一個條目進(jìn)行刪除!,提示窗口,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)“環(huán)境設(shè)置”i.拖入四個label控件ii.拖入三個TextBox控件(改名) 一個label控件(lblBackColor)選擇“事件”,選擇“click”colorDialog1.ShowDialog();lblBackColor.BackColor=colorDialog1.Color;III.i.參數(shù)初始化設(shè)置為配置窗體的代碼窗口添加私有成員變量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();讀快捷鍵及環(huán)境設(shè)置參數(shù)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”保存用戶更改的設(shè)置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.添加字符轉(zhuǎn)換成員方法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(x

33、elRectPix);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;pro

34、tected 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 thisint indexgetreturn structArrindex;public int Lengthgetreturn structA

35、rr.Length;#region 成員變量相應(yīng)的屬性public int XPosgetreturn _xPos;set_xPos=value;public int YPosgetreturn _yPos;set_yPos=value;public Color BlockColorgetreturn _blockColor;#endregion-public void DeasilRotate()/順時針旋轉(zhuǎn)int temp;for(int i=0;istructArr.Length;i+)temp=structArri.X;structArri.X=structArri.Y;structA

36、rri.Y=-temp;public void ContraRotate()/逆時針旋轉(zhuǎn)int temp;for(int i=0;istructArr.Length;i+)temp=structArri.X;structArri.X=-structArri.Y;structArri.Y=temp;private Rectangle PointToRect(Point p)/坐標(biāo)點(diǎn)轉(zhuǎn)化為畫布坐標(biāo)值return new Rectangle(_xPos + p.X)*rectPix+1,(_yPos-p.Y)*rectPix+1,rectPix-2,rectPix-2);public virtual

37、 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);七

38、, 新建生產(chǎn)磚塊的類解決方案中添加BlockGroup.cs類private InfoArr info;private Color disapperColor;private int rectPix;public BlockGroup()Config config=new Config();config.LoadFromXmlFile();info=new InfoArr();info=config.Info;disapperColor=config.BackColor;rectPix=config.RectPix;public Block GetABlock()Random rd=new R

39、andom();int keyOrder=rd.Next(0,info.Length);BitAarry ba=infokeyOrder.ID;int struNum=0;foreach(bool b in ba)if(b)struNum+;Point structArr=new PointstruNum;int k=0;for(int j=0;jba.Length;j+)if(baj)structArrk.X=j/5-2;structArrk.Y=2-j%5;k+;return new Block(structArr,infokeyOrder.BColor,disapperColor,rec

40、tPix);八, 新建為磚塊活動定規(guī)則的類在解決方案中添加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.Tim

41、ers.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();run

42、Block.XPos=_width/2;int y=0;for(int i=0;iy)y=runBlocki.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 Syste

43、m.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(i

44、nt i=0;i_height-1)return false;if(!coorArrxPos+runBlocki.X,yPos-runBlocki.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=

45、runBlock.YPos;for(int i=0;irunBlock.Length;i+)if(xPos+runBlocki.X0)return;if(!coorArrxPos+runBlocki.X,yPos-runBlocki.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_width-1)return;if(!coorArrxPos+runBlocki.X,yPos-runBlocki.Y.IsEmpty)return;runBlock.erase(gpPalette);runBlock.XPos+;runBlock.Paint(gpPalette); public void DeasilRotate()for(int i=0;irunBlock.Length;i+)int x=run

展開閱讀全文
溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!