版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、C#俄羅斯方塊程序設(shè)計姓 名: 學 號: 專 業(yè):計算機科學與技術(shù)學院班 級: 級 班時 間: 20092010年第一學期該實驗制作的是小游戲-俄羅斯方塊1.可實現(xiàn)以下基本功能:用戶可自定義添加或刪除方塊樣式及顏色;用戶可自定義修改游戲背景顏色及按鍵設(shè)置。2.另增加了幾個功能:按鍵設(shè)置改變后點擊保存,會彈出對話框提示“保存成功” ;點擊“開始”運行游戲,背景音樂自動播放,點擊暫停后,背景音樂也隨之停止;每消除一行,會有特效聲音提示消除成功;根據(jù)消行多少會自動加分并顯示。游戲界面效果圖如下:配置窗體效果圖如下:磚塊樣式配置效果圖如下:游戲設(shè)計分為如下九個部分:一, 新建窗體“配置窗體”(TrmC
2、onfig)添加TabControl控件(1) 磚塊樣式配置I.abel控件(lblMode)點擊“事件”,選擇“Paint”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(
3、int y=0;y5;y+)if(struArrx,y)gp.FillRectangle(s,31*x+1,31*y+1,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;Graph
4、ics 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;lblMod
5、e.Invalidate();III.添加listView控件(lsvBlockSet)點擊“事件”,選擇“ItemSelectionChanged”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.“添加”按鈕
6、(btnAdd)bool isEmpty=false;foreach (bool i in struArr)if(i)isEmpty=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.ToStr
7、ing();foreach(ListViewItem item in lsvBlockSet.Items)if (item.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(blockCo
8、lor.ToArgb();V.“刪除”按鈕(btnDel)if(lsvBlockSet.SelectedItems.Count=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.KeyVa
9、lue=45 & e.KeyValue=48 & e.KeyValue=65 & e.KeyValue=96 & e.KeyValue=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)Te
10、mpC).Tag=Keys.None;(TextBox)sender).Text=e.KeyCode.ToString();(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è)置為配置窗體的
11、代碼窗口添加私有成員變量private Config config=new Config();初始化config.LoadFromXmlFile();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)
12、.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)c
13、onfig.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();
14、lblBackColor.BackColor=config.BackColor;ii.保存更改選擇“事件”,選擇“click”保存用戶更改的設(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=(K
15、eys)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=lblBac
16、kColor.BackColor;config.SaveToXmlFile();二, 信息保存解決方案中添加BlockSet.Xml文件 0000001100011000000000000 -65536 0000000000111100000000000 -16711936 0000000000001100010000100 -8323073 0010000100001100000000000 -16776961 0000000100001100010000000 -32704 0000000100001100001000000 -32513 0000000010001100010000000
17、 -8372160 83 88 65 68 99 97 15 25 20 -16777216 三, 一個磚塊的信息類解決方案中添加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 Ge
18、tIdStr()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.Collection
19、s;四, 多個磚塊信息類解決方案中添加InfoArr.cs類I .各個方塊信息類private ArrayList info = 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;/由于使用到了MessageB
20、ox,要添加命名空間using System.Windows.Forms;由于使用到了Color類,還要添加using Syetem.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
21、;public Color BackColorgetreturn _backColor;set_backColor=value;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.GetExecutingAs
22、sembly();Stream sm=asm.GetManifestResourceStream(Tetris.BlockSet.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.ReadElementS
23、tring().Trim();else if (reader.Name =DownKey)_downKey=(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()
24、.Trim();else if (reader.Name =MoveRightKey)_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
25、(reader.ReadElementString().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.ReadElementStri
26、ng().Trim();else if (reader.Name =BackColor)_backColor=Color.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();do
27、c.LoadXml();XmlNode root=doc.SelectSingleNode(BlockSet);for(int 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.InnerTex
28、t=(BlockInfo)infoi).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.
29、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
30、.CreateElement(MoveRightKey);xelMoveRightKey.InnerText=Convert.ToInt32(_moveRightKey).ToString();xelKey.AppendChild(xelMoveRightKey);XmlElement xelDeasilRotateKey=doc.CreateElement(DeasilRotateKey);xelDeasilRotateKey.InnerText=Convert.ToInt32(_deasilRotateKey).ToString();xelKey.AppendChild(xelDeasil
31、RotateKey);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.Cre
32、ateElement(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);xelRe
33、ctPix.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類protecte
34、d 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 thisint ind
35、exgetreturn structArrindex;public int Lengthgetreturn structArr.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.
36、Length;i+)temp=structArri.X;structArri.X=structArri.Y;structArri.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)/坐標點轉(zhuǎn)化為畫布坐標值return new Rectangle(_xPos + p.X)*rectPi
37、x+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(Poin
38、t p in structArr)lock(gp)gp.FillRectangle(sb,PointToRect(p);七, 新建生產(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;r
39、ectPix=config.RectPix;public Block GetABlock()Random rd=new Random();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+;ret
40、urn new Block(structArr,infokeyOrder.BColor,disapperColor,rectPix);八, 新建為磚塊活動定規(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.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=pi
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 道口崗前安全培訓(xùn)會課件
- 2024+共識聲明:成人心臟手術(shù)患者快速拔管建議解讀
- 達州大灘消防安全培訓(xùn)課件
- 邊溝開挖安全教育培訓(xùn)課件
- 數(shù)據(jù)中心ups單機并機試題及答案
- 車隊防疫安全培訓(xùn)課件
- 車隊安全培訓(xùn)計劃方案課件
- 分公司副經(jīng)理內(nèi)部競聘經(jīng)營管理類筆試題
- 車間級崗前安全培訓(xùn)流程課件
- 酒店客房預(yù)訂與收益最大化策略制度
- 2026屆云南省昆明市西山區(qū)民中數(shù)學高一上期末考試模擬試題含解析
- 2025年大學第一學年(食品營養(yǎng)與健康)營養(yǎng)學基礎(chǔ)測試題及答案
- 2025-2030烏干達基于咖啡的種植行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 2026年共青團中央所屬單位招聘66人備考題庫及答案詳解一套
- 人民警察法培訓(xùn)課件
- 2026年哈爾濱職業(yè)技術(shù)學院單招職業(yè)適應(yīng)性考試題庫參考答案詳解
- 2025云南昆明巫家壩建設(shè)發(fā)展有限責任公司及下屬公司第四季度社會招聘31人歷年真題匯編帶答案解析
- 輸尿管切開取石課件
- 小貓絕育協(xié)議書
- 66kV及以下架空電力線路設(shè)計標準
- 2025年浙江乍浦經(jīng)濟開發(fā)區(qū)(嘉興港區(qū))區(qū)屬國有公司公開招聘28人筆試考試備考試題及答案解析
評論
0/150
提交評論