基于C#俄罗斯方块设计Word格式.docx
- 文档编号:16820919
- 上传时间:2022-11-26
- 格式:DOCX
- 页数:20
- 大小:350.37KB
基于C#俄罗斯方块设计Word格式.docx
《基于C#俄罗斯方块设计Word格式.docx》由会员分享,可在线阅读,更多相关《基于C#俄罗斯方块设计Word格式.docx(20页珍藏版)》请在冰豆网上搜索。
publicBlock()//构造函数
RandomrandomGenerator=newRandom();
intrandomBlock=randomGenerator.Next(1,5);
//产生1—4的数
this.ID=randomBlock;
switch(this.ID)
case1:
//横条形
this.Width=4;
this.Height=1;
this.Top=0;
this.Left=3;
shape=newint[this.Width,this.Height];
shape[0,0]=1;
shape[1,0]=1;
shape[2,0]=1;
shape[3,0]=1;
break;
case2:
//正方形
this.Width=2;
this.Height=2;
this.Left=4;
//Createsthenewshapeforthisblock.
shape[0,1]=1;
shape[1,1]=1;
case3:
//T形
this.Width=3;
this.Height=3;
shape[1,1]=1;
shape[1,2]=1;
case4:
//L形
shape[0,2]=1;
}
publicshortWidth//Width属性
get
returnwidth;
set
width=value;
publicshortHeight//Height属性
returnheight;
height=value;
publicshortTop//Top属性
returntop;
top=value;
publicshortLeft//Left属性
returnleft;
left=value;
publicvoidDraw(Graphicsg)
ImagebrickImage=Image.FromFile("
image/block0.gif"
);
for(inti=0;
i<
this.Width;
i++)
for(intj=0;
j<
this.Height;
j++)
if(this.shape[i,j]==1)//黑色格子
//得到绘制这个格子的在游戏面板中的矩形区域
Rectanglerect=newRectangle((this.Left+i)*Game.BlockImageWidth,(this.Top+j)*Game.BlockImageHeight,Game.BlockImageWidth,Game.BlockImageHeight);
g.DrawImage(brickImage,rect);
}//classBlock
}
设计游戏类(game.cs)
classGame
publicconstintBlockImageWidth=21;
//方砖中每个小方格的大小
publicconstintBlockImageHeight=21;
publicconstintPlayingFieldWidth=10;
//游戏面板大小
publicconstintPlayingFieldHeight=20;
privateint[,]pile;
//存储在游戏面板中的所有方砖;
privateBlockcurrentBlock;
//当前的俄罗斯方块
privateBlocknextBlock;
//下一个的俄罗斯方块
publicintscore=0,lines=0;
publicboolover=false;
//游戏是否结束
publicGame()//Game类构造函数
pile=newint[PlayingFieldWidth,PlayingFieldHeight];
ClearPile();
CreateNewBlock();
//产生新的俄罗斯方块
privatevoidClearPile()//清空游戏面板中的所有方砖
PlayingFieldWidth;
PlayingFieldHeight;
pile[i,j]=0;
privatevoidCreateNewBlock()//产生新的俄罗斯方块
{
if(this.nextBlock!
=null)
{
currentBlock=nextBlock;
}
else
currentBlock=newBlock();
nextBlock=newBlock();
}
publicvoidDrawPile(Graphicsg)
image/block1.gif"
//方砖的图形
if(pile[i,j]==1)
Rectanglerect=newRectangle(i*BlockImageWidth,j*BlockImageHeight,BlockImageWidth,BlockImageHeight);
//(j-1)
publicvoidDrawCurrentBlock(Graphicsg)
if(currentBlock!
=null)//检查当前块是否为空
currentBlock.Draw(g);
publicvoidDrawNextBlock(GraphicsdrawingSurface)
if(nextBlock!
shortcurrentLeft=nextBlock.Left;
shortcurrentTop=nextBlock.Top;
nextBlock.Left=(short)((6-nextBlock.Width)/2);
nextBlock.Top=(short)((6-nextBlock.Height)/2);
nextBlock.Draw(drawingSurface);
nextBlock.Left=currentLeft;
nextBlock.Top=currentTop;
privatevoidMoveBlockToPile()//固定到游戏面板上
currentBlock.Width;
currentBlock.Height;
intfx,fy;
fx=currentBlock.Left+i;
fy=currentBlock.Top+j;
if(currentBlock.shape[i,j]==1)
pile[fx,fy]=1;
CheckForLines();
if(CheckForGameOver())//检查游戏是否结束
over=true;
publicboolDownCurrentBlock()
boolhit=false;
currentBlock.Top++;
if((currentBlock.Top+currentBlock.Height)>
PlayingFieldHeight)
hit=true;
//当前块触游戏面板底
else//检查是否接触到下一行其他已落方块
if((currentBlock.shape[i,j]==1)&
&
(pile[fx,fy]==1))//(fy+1)
if(hit)//触到其他已落方块或游戏面板底
currentBlock.Top--;
MoveBlockToPile();
//固定到游戏面板上
//产生新的俄罗斯方块
returnhit;
publicvoidRotateCurrentBlock()//旋转方块
boolcanRotate=true;
shortnewWidth=0;
shortnewHeight=0;
int[,]newShape;
newWidth=currentBlock.Height;
newHeight=currentBlock.Width;
newShape=newint[newWidth,newHeight];
intx,y;
if(((currentBlock.Left+newWidth)<
=Game.PlayingFieldWidth)
&
((currentBlock.Top+newHeight)<
Game.PlayingFieldHeight))
x=((currentBlock.Height-1)-j);
y=i;
newShape[x,y]=currentBlock.shape[i,j];
if(newShape[x,y]==1&
pile[x+currentBlock.Left,y+currentBlock.Top]==1)
canRotate=false;
return;
//不能旋转}
if(canRotate)
currentBlock.Width=newWidth;
currentBlock.Height=newHeight;
currentBlock.shape=newShape;
}
publicvoidMoveCurrentBlockSide(boolleft)//左右移动
boolcanMove=true;
if(left)//左移动
if(currentBlock.Left>
0)
fy=(currentBlock.Top+1)+j;
(pile[(fx-1),fy]==1))
canMove=false;
if(canMove)
currentBlock.Left--;
else//右移动
if((currentBlock.Left+currentBlock.Width)<
PlayingFieldWidth)
(pile[(fx+1),fy]==1))
currentBlock.Left++;
privateintCheckForLines()//检查是否满行并消去
intnumLines=0;
int[]completeLines=newint[PlayingFieldHeight];
for(intj=PlayingFieldHeight-1;
j>
0;
j--)//j=PlayingFieldHeight
boolfullLine=true;
PlayingFieldWidth;
if(pile[i,j]==0)
fullLine=false;
if(fullLine)
numLines++;
completeLines[numLines]=j;
if(numLines>
for(inti=1;
=numLines;
ClearLine((completeLines[i]+(i-1)));
score+=5*(numLines*(numLines+1));
lines+=numLines;
returnnumLines;
privatevoidClearLine(intlineNumber)
for(intj=lineNumber;
j--)
pile[i,j]=pile[i,(j-1)];
pile[i,0]=0;
publicboolCheckForGameOver()//检查游戏是否结束
if(currentBlock.Top==0)
returntrue;
else
returnfalse;
设计窗体类(form1.cs)
下图:
如果觉得图片不清楚可以另存为桌面,慢慢研究
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Windows.Forms;
publicpartialclassForm1:
Form
publicForm1()
InitializeComponent();
Gamegame=null;
privatevoidbutton1_Click(objectsender,EventArgse)
game=newGame();
pictureBox1.Height=Game.BlockImageHeight*Game.PlayingFieldHeight+3;
pictureBox1.Width=Game.BlockImageWidth*Game.PlayingFieldWidth+3;
pictureBox1.Invalidate();
//重画游戏面板区域
timer1.Enabled=true;
button1.Enabled=false;
privatevoidbutton2_Click(objectsender,EventArgse)
if(button2.Text=="
暂停游戏"
)
timer1.Enabled=false;
button2.Text="
继续游戏"
;
privatevoidbutton3_Click(objectsender,EventArgse)
this.Close();
privatevoidpictureBox1_Paint(objectsender,PaintEventArgse)
//重画游戏面板
if(game!
{
game.DrawPile(e.Graphics);
game.DrawCurrentBlock(e.Graphics);
privatevoidpictureBox2_Paint(objectsender,PaintEventArgse)
////重画下一个方块
=null)game.DrawNextBlock(e.Graphics);
privatevoidtimer1_Tick(objectsender,EventArgse)
if(!
game.DownCurrentBlock())
pictureBox2.Invalidate();
//重画下一个方块
lblScore.Text=game.score.ToString();
if(game.over==true)
MessageBox.Show("
游戏结束,"
"
提示"
button1.Enabled=true;
protectedoverrideboolProcessCmdKey(refMessagemsg,Keyse)
//重写ProcessCmdKey方法
if(button2.Tex
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 C# 俄罗斯方块 设计
![提示](https://static.bdocx.com/images/bang_tan.gif)