private void timer1_Tick(object sender, System.EventArgs e)
{
if (testCanDrop())
dropDown();
else
{
disappear();
block(getRandomBlock());
if(testCanDrop()==false)
{
timer1.Stop();
MessageBox.Show("Cow boy, Game over!try again!");
inite();
Graphics g=CreateGraphics();
g.FillRectangle(new SolidBrush(Color.Blue),0,0,300,420);
g.FillRectangle(new SolidBrush(Color.Black),10,0,280,420);
block(getRandomBlock());
timer1.Start();
}
}
}
private void inite()//初始化
{
for(int i=0;i<14;i++)
for(int j=0;j<21;j++)
{
table[i,j]=1;
}
for(int i=1;i<13;i++)
for(int j=0;j<20;j++)
{
table[i,j]=0;
}
}
private int getRandomBlock()//得到随机的形状
{
Random rd=new Random();
return rd.Next(1,19);
}
public void block (int i)//i 为方块的形状
{
x=130;//初始的坐标
y=60;
currentshape=i;
paintBlock(i,Color.Yellow);
}
private void paintBlock(int z,Color w)//画方块的函数
{
Graphics g=this.CreateGraphics();
SolidBrush sBrush=new SolidBrush(w);
g.FillRectangle(sBrush,x+shapes[z-1].xy[0]*20,y+shapes[z-1].xy[1]*20,20,20);
g.FillRectangle(sBrush,x+shapes[z-1].xy[2]*20,y+shapes[z-1].xy[3]*20,20,20);
g.FillRectangle(sBrush,x+shapes[z-1].xy[4]*20,y+shapes[z-1].xy[5]*20,20,20);
g.FillRectangle(sBrush,x+shapes[z-1].xy[6]*20,y+shapes[z-1].xy[7]*20,20,20); g.Dispose(); int a,b,c;
a=(x-10)/20;
b=y/20;
if(w==Color.Black) c=0;
else c=1;
int bb,cc,xx,yy;
try
{
for(int i=0;i<8;i++,i++)
{
bb=shapes[z-1].xy[i];
cc=shapes[z-1].xy[i+1];
xx=a+bb;
yy=b+cc;
table[xx,yy]=c;
}
}
catch(IndexOutOfRangeException e)
{
MessageBox.Show(e.Message);
} }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g=e.Graphics;
g.FillRectangle(new SolidBrush(Color.Blue),0,0,300,420);
g.FillRectangle(new SolidBrush(Color.Black),10,0,280,420);
for(int i=1;i<13;i++)
for(int j=0;j<20;j++)
{
if(table[i,j]==1)
g.FillRectangle(new SolidBrush(Color.Yellow),i*20+10,j*20,20,20);
}
}
private bool testCanDrop()//测试是否可以下落
{
int a,b;
a=(x-10)/20;
b=y/20;
int c,d;
for(int i=0;i<8;i++,i++)
{
c=a+shapes[currentshape].xy[i];
d=b+shapes[currentshape].xy[i]+1;
if(table[c,d]==1) return false;
}
return true;
}
private void dropDown()//下落方法
{
paintBlock(currentshape,Color.Black);
y+=20;
paintBlock(currentshape,Color.Yellow);
}
private void disappear()//消行
{
//求高手帮忙写完此算法.
}
}
}
{
if (testCanDrop())
dropDown();
else
{
disappear();
block(getRandomBlock());
if(testCanDrop()==false)
{
timer1.Stop();
MessageBox.Show("Cow boy, Game over!try again!");
inite();
Graphics g=CreateGraphics();
g.FillRectangle(new SolidBrush(Color.Blue),0,0,300,420);
g.FillRectangle(new SolidBrush(Color.Black),10,0,280,420);
block(getRandomBlock());
timer1.Start();
}
}
}
private void inite()//初始化
{
for(int i=0;i<14;i++)
for(int j=0;j<21;j++)
{
table[i,j]=1;
}
for(int i=1;i<13;i++)
for(int j=0;j<20;j++)
{
table[i,j]=0;
}
}
private int getRandomBlock()//得到随机的形状
{
Random rd=new Random();
return rd.Next(1,19);
}
public void block (int i)//i 为方块的形状
{
x=130;//初始的坐标
y=60;
currentshape=i;
paintBlock(i,Color.Yellow);
}
private void paintBlock(int z,Color w)//画方块的函数
{
Graphics g=this.CreateGraphics();
SolidBrush sBrush=new SolidBrush(w);
g.FillRectangle(sBrush,x+shapes[z-1].xy[0]*20,y+shapes[z-1].xy[1]*20,20,20);
g.FillRectangle(sBrush,x+shapes[z-1].xy[2]*20,y+shapes[z-1].xy[3]*20,20,20);
g.FillRectangle(sBrush,x+shapes[z-1].xy[4]*20,y+shapes[z-1].xy[5]*20,20,20);
g.FillRectangle(sBrush,x+shapes[z-1].xy[6]*20,y+shapes[z-1].xy[7]*20,20,20); g.Dispose(); int a,b,c;
a=(x-10)/20;
b=y/20;
if(w==Color.Black) c=0;
else c=1;
int bb,cc,xx,yy;
try
{
for(int i=0;i<8;i++,i++)
{
bb=shapes[z-1].xy[i];
cc=shapes[z-1].xy[i+1];
xx=a+bb;
yy=b+cc;
table[xx,yy]=c;
}
}
catch(IndexOutOfRangeException e)
{
MessageBox.Show(e.Message);
} }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g=e.Graphics;
g.FillRectangle(new SolidBrush(Color.Blue),0,0,300,420);
g.FillRectangle(new SolidBrush(Color.Black),10,0,280,420);
for(int i=1;i<13;i++)
for(int j=0;j<20;j++)
{
if(table[i,j]==1)
g.FillRectangle(new SolidBrush(Color.Yellow),i*20+10,j*20,20,20);
}
}
private bool testCanDrop()//测试是否可以下落
{
int a,b;
a=(x-10)/20;
b=y/20;
int c,d;
for(int i=0;i<8;i++,i++)
{
c=a+shapes[currentshape].xy[i];
d=b+shapes[currentshape].xy[i]+1;
if(table[c,d]==1) return false;
}
return true;
}
private void dropDown()//下落方法
{
paintBlock(currentshape,Color.Black);
y+=20;
paintBlock(currentshape,Color.Yellow);
}
private void disappear()//消行
{
//求高手帮忙写完此算法.
}
}
}
怎么打包上传呀?
这里有个用C#写的俄罗斯.它的消层方法是这样的.希望对你有帮助
private void disappear()//消行
{
int n;
Graphics g=this.CreateGraphics();
int i=13;
while(i>=0)
{
n=0;
for(int j=1;j<14;j++)
{
if(table[i,j]==1)
n++;
else
break;
}
if(n==13)
{
for(int k=i;k>0;k--)
for(int w=1;w<14;w++)
{
if(table[k-1,w]==1)
{
g.FillRectangle(new SolidBrush(Color.Yellow),w*20+10,k*20+10,20,20);
g.DrawRectangle(new Pen(Color.Green,2),w*20+10,k*20+10,20,20);
}
else
g.FillRectangle(new SolidBrush(Color.Black),w*20+10,k*20+10,20,20);
table[k,w]=table[k-1,w];
}
}
else
i--;
}
}