基于C#俄罗斯方块设计.docx

上传人:b****4 文档编号:3914707 上传时间:2022-11-26 格式:DOCX 页数:20 大小:350.37KB
下载 相关 举报
基于C#俄罗斯方块设计.docx_第1页
第1页 / 共20页
基于C#俄罗斯方块设计.docx_第2页
第2页 / 共20页
基于C#俄罗斯方块设计.docx_第3页
第3页 / 共20页
基于C#俄罗斯方块设计.docx_第4页
第4页 / 共20页
基于C#俄罗斯方块设计.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

基于C#俄罗斯方块设计.docx

《基于C#俄罗斯方块设计.docx》由会员分享,可在线阅读,更多相关《基于C#俄罗斯方块设计.docx(20页珍藏版)》请在冰豆网上搜索。

基于C#俄罗斯方块设计.docx

基于C#俄罗斯方块设计

此程序主要三个小结讲解:

1、设计方块类(block.cs)

2、设计游戏类(game.cs)

3、设计窗体类(form1.cs)

通俗易懂,希望对广大的学生们都有帮助

欢迎下载

技术qq1278263100

邮箱*****************

游戏所需图片:

游戏运行图:

话不多说:

设计方块类(block.cs)

程序都有注释,通俗易懂

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Drawing;//add

namespace俄罗斯方块

{

publicclassBlock

{

privateshortwidth;

privateshortheight;

privateshorttop;

privateshortleft;

privateintID;   //方块部件的ID

publicint[,]shape;  //存储方块部件的形状,0为空白,1为有砖块

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.Top=0;

this.Left=4;

//Createsthenewshapeforthisblock.

shape=newint[this.Width,this.Height];

shape[0,0]=1;shape[0,1]=1;

shape[1,0]=1;shape[1,1]=1;

break;

case3:

 //T形

this.Width=3;

this.Height=3;

this.Top=0;

this.Left=4;

//Createsthenewshapeforthisblock.

shape=newint[this.Width,this.Height];

shape[0,0]=1;shape[1,0]=1;

shape[2,0]=1;shape[1,1]=1;

shape[1,2]=1;

break;

case4:

 //L形

this.Width=2;

this.Height=3;

this.Top=0;

this.Left=4;

//Createsthenewshapeforthisblock.

shape=newint[this.Width,this.Height];

shape[0,0]=1;shape[0,1]=1;

shape[0,2]=1;shape[1,2]=1;

break;

}

}

publicshortWidth//Width属性

{

get

{

returnwidth;

}

set

{

width=value;

}

}

publicshortHeight//Height属性

{

get

{

returnheight;

}

set

{

height=value;

}

}

publicshortTop//Top属性

{

get

{

returntop;

}

set

{

top=value;

}

}

publicshortLeft//Left属性

{

get

{

returnleft;

}

set

{

left=value;

}

}

publicvoidDraw(Graphicsg)

{

ImagebrickImage=Image.FromFile("image/block0.gif");

for(inti=0;i

{

for(intj=0;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)

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Drawing;//add

namespace俄罗斯方块

{

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()//清空游戏面板中的所有方砖

{

for(inti=0;i

{

for(intj=0;j

{

pile[i,j]=0;

}

}

}

privatevoidCreateNewBlock()//产生新的俄罗斯方块

{

if(this.nextBlock!

=null)

{

currentBlock=nextBlock;

}

else

{

currentBlock=newBlock();

}

nextBlock=newBlock();

}

publicvoidDrawPile(Graphicsg)

{

ImagebrickImage=Image.FromFile("image/block1.gif");//方砖的图形

for(inti=0;i

{

for(intj=0;j

{

if(pile[i,j]==1)

{

Rectanglerect=newRectangle(i*BlockImageWidth,j*BlockImageHeight,BlockImageWidth,BlockImageHeight);//(j-1)

g.DrawImage(brickImage,rect);

}

}

}

}

publicvoidDrawCurrentBlock(Graphicsg)

{

if(currentBlock!

=null)//检查当前块是否为空

{

currentBlock.Draw(g);

}

}

publicvoidDrawNextBlock(GraphicsdrawingSurface)

{

if(nextBlock!

=null)

{

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()//固定到游戏面板上

{

for(inti=0;i

{

for(intj=0;j

{

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//检查是否接触到下一行其他已落方块

{

for(inti=0;i

{

for(intj=0;j

{

intfx,fy;

fx=currentBlock.Left+i;

fy=currentBlock.Top+j;

if((currentBlock.shape[i,j]==1)&&(pile[fx,fy]==1))//(fy+1)

{

hit=true;

}

}

}

}

if(hit)//触到其他已落方块或游戏面板底

{

currentBlock.Top--;

MoveBlockToPile();//固定到游戏面板上  

CreateNewBlock();//产生新的俄罗斯方块

}

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)

{

for(inti=0;i

{

for(intj=0;j

{

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)

{

for(inti=0;i

{

for(intj=0;j

{

intfx,fy;

fx=currentBlock.Left+i;

fy=(currentBlock.Top+1)+j;

if((currentBlock.shape[i,j]==1)&&(pile[(fx-1),fy]==1))

{

canMove=false;

}

}

}

if(canMove)

{

currentBlock.Left--;

}

}

}

else//右移动

{

if((currentBlock.Left+currentBlock.Width)

{

for(inti=0;i

{

for(intj=0;j

{

intfx,fy;

fx=currentBlock.Left+i;

fy=(currentBlock.Top+1)+j;

if((currentBlock.shape[i,j]==1)&&(pile[(fx+1),fy]==1))

{

canMove=false;

}

}

}

if(canMove)

{

currentBlock.Left++;

}

}

}

}

privateintCheckForLines()//检查是否满行并消去

{

intnumLines=0;

int[]completeLines=newint[PlayingFieldHeight];

for(intj=PlayingFieldHeight-1;j>0;j--)//j=PlayingFieldHeight

{

boolfullLine=true;

for(inti=0;i

{

if(pile[i,j]==0)

{

fullLine=false;

break;

}

}

if(fullLine)

{

numLines++;

completeLines[numLines]=j;

}

}

if(numLines>0)

{

for(inti=1;i<=numLines;i++)

{

ClearLine((completeLines[i]+(i-1)));

}

score+=5*(numLines*(numLines+1));

lines+=numLines;

}

returnnumLines;

}

privatevoidClearLine(intlineNumber)

{

for(intj=lineNumber;j>0;j--)

{

for(inti=0;i

{

pile[i,j]=pile[i,(j-1)];

}

}

for(inti=0;i

{

pile[i,0]=0;

}

}

publicboolCheckForGameOver()//检查游戏是否结束

{

if(currentBlock.Top==0)

returntrue;

else

returnfalse;

}

}

}

设计窗体类(form1.cs)

下图:

如果觉得图片不清楚可以另存为桌面,慢慢研究

 

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespace俄罗斯方块

{

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="继续游戏";

}

else

{

timer1.Enabled=true;button2.Text="暂停游戏";

}

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

this.Close();

}

privatevoidpictureBox1_Paint(objectsender,PaintEventArgse)

{

//重画游戏面板

if(game!

=null)

{

game.DrawPile(e.Graphics);

game.DrawCurrentBlock(e.Graphics);

}

}

privatevoidpictureBox2_Paint(objectsender,PaintEventArgse)

{

////重画下一个方块

if(game!

=null)game.DrawNextBlock(e.Graphics);

}

privatevoidtimer1_Tick(objectsender,EventArgse)

{

if(!

game.DownCurrentBlock())

{

pictureBox1.Invalidate();//重画游戏面板区域

pictureBox2.Invalidate();//重画下一个方块

}

lblScore.Text=game.score.ToString();

if(game.over==true)

{

timer1.Enabled=false;

MessageBox.Show("游戏结束,","提示");

button1.Enabled=true;

}

}

protectedoverrideboolProcessCmdKey(refMessagemsg,Keyse)

//重写ProcessCmdKey方法

{

if(button2.Tex

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

当前位置:首页 > 人文社科 > 哲学历史

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1