您好,欢迎访问三七文档
俄罗斯方块一:介绍借鉴了以前前辈的一些做法,写了一个简单的俄罗斯方块的游戏,初始化界面如下:主界面的右下角是一个WindowsMediaPlayer控件,用于边游戏边听歌。“播放音乐”按钮按下的界面:“设置”按钮按下的界面:“帮助”按钮按下没有界面,直接弹出帮助文档。点击开始后的运行界面:可以通过向上键来变换砖块,向下键快速降落,左键向左移,右键向右移(也可以用W、S、A、D分别代替上下左右键),来调整砖块的位置及形状,从而获得分数。游戏结束的界面:二:代码我用的是VS2010编的,但是10版有个bug,就是把含有颜色的汉字贴到word上时会出现乱码,每個中文字后会连着一到两个多余的符号,不过仔细看能看个大概明白,大家就将就下,不好意思。下面我直接贴上源代码。usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;usingSystem.Runtime.InteropServices;namespaceRestart_tetris{publicpartialclassMainForm:Form{//存ä?储ä¡é所¨´有®D的Ì?砖Á?块¨¦的Ì?样¨´式º?及¡ã颜?色¦?ArrayListbricksarraylist=newArrayList();Randomrd=newRandom();intindex;//存ä?储ä¡é当Ì¡À前¡ã的Ì?panel1需¨¨要°a画-的Ì?砖Á?块¨¦standardbrickcurrentstandardbrick;//存ä?储ä¡é当Ì¡À前¡ã的Ì?panel2需¨¨要°a画-的Ì?砖Á?块¨¦standardbricknextstandardbrick;Graphicspanel1g;Graphicspanel2g;//存ä?储ä¡é新?的Ì?砖Á?块¨¦在¨²panel1上¦?画-出?来¤¡ä之?前¡ã,ê?panel1上¦?已°?有®D的Ì?图ª?案ã?Color[,]preservecolor=newColor[13,24];//xx,ê?yy调Ì¡Â整?砖Á?块¨¦在¨²panel1上¦?的Ì?位?置?intxx;intyy;//存ä?储ä¡é玩ª?家¨°所¨´得Ì?分¤?数ºyintscore;//需¨¨要°a检¨¬查¨¦的Ì?位?置?的Ì?坐Á?标À¨º(ꡧ因°¨°为a在¨²准Á?备À?执¡ä行D旋y转Áa或¨°向¨°下?等̨¨操¨´作Á¡Â时º¡À要°a检¨¬查¨¦所¨´需¨¨的Ì?位?置?是º?否¤?为a空?)ê?Point[]pout;//画-布?上¦?需¨¨要°a检¨¬查¨¦的Ì?位?置?的Ì?实º¦Ì际¨º的Ì?颜?色¦?ArrayListnextcolor=newArrayList();IntPtrhdc;[DllImport(gdi32.dll)]//windowsAPIprivatestaticexternintGetPixel(IntPtrhDc,intx,inty);publicMainForm(){InitializeComponent();bricksarraylist.Add(newconcretebrick(0000001100001000010000000,Color.Gold));bricksarraylist.Add(newconcretebrick(0010000100001000010000000,Color.Red));bricksarraylist.Add(newconcretebrick(0000000100011000100000000,Color.Pink));bricksarraylist.Add(newconcretebrick(0000000100011000010000000,Color.Purple));bricksarraylist.Add(newconcretebrick(0000000110011000000000000,Color.Green));bricksarraylist.Add(newconcretebrick(0000000110001000010000000,Color.DodgerBlue));bricksarraylist.Add(newconcretebrick(0000000110001100000000000,Color.Yellow));panel1g=panel1.CreateGraphics();panel2g=panel2.CreateGraphics();//hdc=panel1g.GetHdc();score=0;xx=6;yy=2;for(inti=0;i13;i++)for(intj=0;j24;j++)//初?始º?化¡¥为apanel1的Ì?backcolorpreservecolor[i,j]=Color.MidnightBlue;}privatevoid播£¤放¤?音°?乐¤?ToolStripMenuItem_Click(objectsender,EventArgse){//添¬¨ª加¨®了¢?一°?个?openfiledialog控?件t,ê?名?字Á?叫Dopenmusicopenmusic.CheckFileExists=true;openmusic.CheckPathExists=true;openmusic.InitialDirectory=Application.StartupPath+\\歌¨¨曲¨²;if(openmusic.ShowDialog()==DialogResult.OK)this.axWindowsMediaPlayer1.URL=openmusic.FileName;}privatevoid设¦¨¨置?ToolStripMenuItem_Click(objectsender,EventArgse){//setnotice是º?另¢¨ª一°?个?form窗ä¡ã体¬?,ê?用®?来¤¡ä设¦¨¨置?玩ª?家¨°需¨¨要°a的Ì?等̨¨级?set_noticesetnotice=newset_notice();setnotice.Show();setnotice.Owner=this;}privatevoid帮ã?助¨²ToolStripMenuItem_Click(objectsender,EventArgse){//helpdocumenthelpd=newhelpdocument();//helpd.Show();//打䨰开a帮ã?助¨²文?档̦ÌSystem.Diagnostics.Process.Start(Application.StartupPath+\\GPIB连¢?接¨®仪°?器¡Â.pdf);}//“¡ã暂Y停ª¡ê”¡À或¨°“¡ã继¨¬续?”¡À按ã¡ä钮£¤的Ì?触䣤发¤¡é事º?件tprivatevoidpause_Click(objectsender,EventArgse){if(((Button)sender).Text==暂Y停ª¡ê){this.pause.Text=继¨¬续?;this.timer1.Stop();}else{this.pause.Text=暂Y停ª¡ê;this.timer1.Start();}}//“¡ã开a始º?”¡À按ã¡ä钮£¤的Ì?触䣤发¤¡é事º?件tprivatevoidstart_Click(objectsender,EventArgse){//label2.text是º?setnotice中D表À¨ª示º?等̨¨级?的Ì?switch(this.label2.Text){case1:this.timer1.Interval=1000;break;case2:this.timer1.Interval=800;break;case3:this.timer1.Interval=600;break;case4:this.timer1.Interval=400;break;case5:this.timer1.Interval=200;break;}this.transformation.Enabled=true;this.left.Enabled=true;this.right.Enabled=true;this.down.Enabled=true;this.start.Enabled=false;this.pause.Enabled=true;//产¨²生¦¨²一°?个?砖Á?块¨¦,ê?用®?于®¨²在¨²主¡Â画-布?上¦?显?示º?currentstandardbrick=createbrick();//产¨²生¦¨²下?一°?个?砖Á?块¨¦,ê?显?示º?在¨²小?画-布?上¦?,ê?用®?于®¨²提¬¨¢示º?nextstandardbrick=createbrick();//在¨²主¡Â画-布?上¦?显?示º?currentstandardbrickpaint(currentstandardbrick.brickpointsduplicate,currentstandardbrick.brickcolorduplicate,panel1g,preservecolor);//在¨²小?上¦?画-布?上¦?显?示º?下?一°?个?砖Á?块¨¦paint2(nextstandardbrick.brickpointsduplicate,nextstandardbrick.brickcolorduplicate,panel2g);this.timer1.Start();}privatestandardbrickcreatebrick(){//每?当Ì¡À产¨²生¦¨²新?砖Á?块¨¦时º¡À,ê?都?说¦Ì明¡Âcurrentstandardbrick已°?经-更¨¹新?,ê?所¨´以°?画-到Ì?画-布?上¦?应®|时º¡À应®|调Ì¡Â整?位?置?xx=6;yy=2;//随?机¨²从䨮bricksarraylist中D取¨?出?一°?个?砖Á?块¨¦index=rd.Next(bricksarraylist.Count);concretebricktempconcretebrick=(concretebrick)bricksarraylist[index];stringtempcode=tempconcretebrick.cbcodeduplicate;Colortempcolor=tempconcretebrick.cbcolorduplicate;//下?面?主¡Â要°a是º?将?那?些?01的Ì?代䨲码?转Áa换?成¨¦实º¦Ì际¨º的Ì?坐Á?标À¨º,ê?只?有®D有®D坐Á?标À¨º才?可¨¦以°?在¨²panel1上¦?绘?出?ListPointlist=newListPoint();for(inti=0;itempcode.Length;i++){if(tempcode[i]=='1'){Pointp=newPoint(i%5,i/5);//使º1砖Á?块¨¦的Ì?坐Á?标À¨º以°?坐Á?标À¨º原-点Ì?为a中D心?,ê?便À?于®¨²后¨®面?旋y转Áa等̨¨操¨´作Á¡Â的Ì?坐Á?标À¨º变À?换?p.Offset(-2,-2);list.Add(p);}}standardbricktempstandardbrick=newstandardbrick(list.ToArray(),tempcolor);//随?机¨²选?择?是º?否¤?旋y
本文标题:俄罗斯方块小游戏
链接地址:https://www.777doc.com/doc-5044934 .html