using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MyRussianRock
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private int[,] table=new int[15,15];
private int x,y;//方块的坐标
// private int[] shape=new int[9]{1,2,3,4,5,6,7,8,9};//nine type of block
private int currentshape;//当前的形状
public void block (int i)//i 为方块的形状
{
x=150;//初始的坐标
y=10;
switch(i)
{
case 1:currentshape=1;//田字形
paintBlock(1,Color.Yellow);
break;
case 2:currentshape=2;//T字形
paintBlock(2,Color.Yellow);
break;
case 3:currentshape=3;//长条形
paintBlock(3,Color.Yellow);
break;
case 4:currentshape=4;//Z字形
paintBlock(4,Color.Yellow);
break;
}
}
private void rotate()//旋转方法
{
Graphics g=this.CreateGraphics();
int a,b;//获得在table中的坐标
a=(y-10)/20;
b=(x-10)/20;
switch(currentshape)
{
case 1:break;
case 2:
if(a>=1&&a<=12&&b<=11&&table[a-1,b+1]==0&&table[a+1,b+2]==0&&table[a+1,b]==0&&table[a-1,b]==0)
{
paintBlock(2,Color.Black);
paintBlock(5,Color.Yellow);
}
break;
case 3:
if(a<=10&&table[a+1,b]==0&&table[a+2,b]==0&&table[a+3,b]==0)
{
paintBlock(3,Color.Black);
paintBlock(8,Color.Yellow);
}
break;
case 4:
if(a<=10&&b>=1&&table[a+1,b]==0&&table[a+1,b-1]==0&&table[a+2,b-1]==0)
{
paintBlock(4,Color.Black);
paintBlock(9,Color.Yellow);
}
break;
case 5:
if(a<=12&&b>=1&&table[a+1,b]==0&&table[a+1,b-1]==0)
{
paintBlock(5,Color.Black);
paintBlock(6,Color.Yellow);
}
break;
case 6:
if(a<=11&&table[a+2,b]==0)
{
paintBlock(6,Color.Black);
paintBlock(7,Color.Yellow);
}
break;
case 7:
if(b<=12&&table[a,b+1]==0&&table[a,b+2]==0)
{
paintBlock(7,Color.Black);
paintBlock(2,Color.Yellow);
}
break;
case 8:
if(b<=10&&table[a,b+1]==0&&table[a,b+2]==0&&table[a,b+3]==0)
{
paintBlock(8,Color.Black);
paintBlock(3,Color.Yellow);
}
break;
case 9:
if(a<=12&&b<=11&&table[a+1,b+1]==0&&table[a+1,b+2]==0)
{
paintBlock(9,Color.Black);
paintBlock(4,Color.Yellow);
}
break;
}
}
private int getRandomBlock()//得到随机的形状
{
Random rd=new Random();
return rd.Next(1,4);//
}
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//this.KeyPress+=new KeyPressEventHandler(Form1_KeyPress);
this.KeyDown+=new KeyEventHandler(Form1_KeyDown);
inite();
block(getRandomBlock());
this.timer1.Start();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 500;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 317);
this.Enabled = false;
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g=e.Graphics;
g.FillRectangle(new SolidBrush(Color.Blue),10,10,300,300);
g.FillRectangle(new SolidBrush(Color.Black),30,10,260,280);
for(int i=0;i<14;i++)
for(int j=1;j<14;j++)
{
if(table[i,j]==1)
g.FillRectangle(new SolidBrush(Color.Yellow),j*20+10,i*20+10,20,20);
}
}
private void paintBlock(int z,Color w)//画方块的函数
{
// if(x>13||x<0||y<1||y>13) return;
Graphics g=this.CreateGraphics();
int a,b;//in table
int c;
Color d;
if(w==Color.Black)
{
c=0;
d=Color.Black;
}
else
{
c=1;
d=Color.Green;
}
Pen drawPen=new Pen(d,2);
SolidBrush sBrush=new SolidBrush(w);
switch(z)
{
case 1: if(x>=30&&x<=250&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
g.FillRectangle(sBrush,x,y,40,40);
g.DrawRectangle(drawPen,x,y,40,20);
g.DrawRectangle(drawPen,x,y+20,40,20);
g.DrawRectangle(drawPen,x+20,y,20,40);
table[a,b]=c;
table[a+1,b]=c;
table[a,b+1]=c;
table[a+1,b+1]=c;
currentshape=1;
}
break;
case 2: if(x>=30&&x<=230&&y>=10&&y<=250)
{
g.FillRectangle(sBrush,x,y,60,20);
g.FillRectangle(sBrush,x+20,y+20,20,20);
g.DrawRectangle(drawPen,x,y,60,20);
g.DrawRectangle(drawPen,x+20,y,20,40);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a,b+1]=c;
table[a,b+2]=c;
table[a+1,b+1]=c;
currentshape=2;
}
break;
case 3:if(x>=30&&x<=210&&y>=10&&y<=270)
{
g.FillRectangle(sBrush,x,y,80,20);
g.DrawRectangle(drawPen,x,y,80,20);
g.DrawRectangle(drawPen,x+20,y,20,20);
g.DrawRectangle(drawPen,x+40,y,20,20);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a,b+1]=c;
table[a,b+2]=c;
table[a,b+3]=c;
currentshape=3;
}
break;
case 4:if(x>=30&&x<=230&&y>=10&&y<=250)
{
g.FillRectangle(sBrush,x,y,40,20);
g.FillRectangle(sBrush,x+20,y+20,40,20);
g.DrawRectangle(drawPen,x,y,40,20);
g.DrawRectangle(drawPen,x+20,y+20,40,20);
g.DrawRectangle(drawPen,x+20,y,20,40);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a,b+1]=c;
table[a+1,b+1]=c;
table[a+1,b+2]=c;
currentshape=4;
}
break;
case 5:if(x>=30&&x<=250&&y>=30&&y<=250)
{
g.FillRectangle(sBrush,x,y,20,20);
g.FillRectangle(sBrush,x+20,y-20,20,60);
g.DrawRectangle(drawPen,x,y,40,20);
g.DrawRectangle(drawPen,x+20,y-20,20,60);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a-1,b+1]=c;
table[a,b+1]=c;
table[a+1,b+1]=c;
currentshape=5;
}
break;
case 6:if(x>=50&&x<=250&&y>=10&&y<=250)
{
g.FillRectangle(sBrush,x,y,20,20);
g.FillRectangle(sBrush,x-20,y+20,60,20);
g.DrawRectangle(drawPen,x,y,20,40);
g.DrawRectangle(drawPen,x-20,y+20,60,20);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a+1,b-1]=c;
table[a+1,b]=c;
table[a+1,b+1]=c;
currentshape=6;
}
break;
case 7:if(x>=30&&x<=250&&y>=10&&y<=230)
{
g.FillRectangle(sBrush,x,y,20,60);
g.FillRectangle(sBrush,x+20,y+20,20,20);
g.DrawRectangle(drawPen,x,y,20,60);
g.DrawRectangle(drawPen,x,y+20,40,20);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a+1,b]=c;
table[a+2,b]=c;
table[a+1,b+1]=c;
currentshape=7;
}
break;
case 8:if(x>=30&&x<=270&&y>=10&&y<=210)
{
g.FillRectangle(sBrush,x,y,20,80);
g.DrawRectangle(drawPen,x,y,20,80);
g.DrawRectangle(drawPen,x,y+20,20,20);
g.DrawRectangle(drawPen,x,y+40,20,20);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a+1,b]=c;
table[a+2,b]=c;
table[a+3,b]=c;
currentshape=8;
}
break;
case 9:if(x>=30&&x<=270&&y>=30&&y<=250)
{
g.FillRectangle(sBrush,x,y,20,40);
g.FillRectangle(sBrush,x+20,y-20,20,40);
g.DrawRectangle(drawPen,x,y,20,40);
g.DrawRectangle(drawPen,x+20,y-20,20,40);
g.DrawRectangle(drawPen,x,y,40,20);
a=(y-10)/20;
b=(x-10)/20;
table[a,b]=c;
table[a,b+1]=c;
table[a+1,b]=c;
table[a-1,b+1]=c;
currentshape=9;
}
break;
}
g.Dispose();
}
private void dropDown()//下落方法
{
Graphics g=this.CreateGraphics();
switch(currentshape)
{
case 1: if(x>=30&&x<=250&&y>=10&&y<=230)
{
paintBlock(1,Color.Black);
y+=20;
paintBlock(1,Color.Yellow);
}
break;
case 2: if(x>=30&&x<=230&&y>=10&&y<=230)
{
paintBlock(2,Color.Black);
y+=20;
paintBlock(2,Color.Yellow);
}
break;
case 3: if(x>=30&&x<=210&&y>=10&&y<=250)
{
paintBlock(3,Color.Black);
y+=20;
paintBlock(3,Color.Yellow);
}
break;
case 4: if(x>=30&&x<=210&&y>=10&&y<=230)
{
paintBlock(4,Color.Black);
y+=20;
paintBlock(4,Color.Yellow);
}
break;
case 5:if(x>=30&&x<=250&&y>=30&&y<=230)
{
paintBlock(5,Color.Black);
y+=20;
paintBlock(5,Color.Yellow);
}
break;
case 6:if(x>=50&&x<=250&&y>=10&&y<=230)
{
paintBlock(6,Color.Black);
y+=20;
paintBlock(6,Color.Yellow);
}
break;
case 7:if(x>=30&&x<=250&&y>=10&&y<=210)
{
paintBlock(7,Color.Black);
y+=20;
paintBlock(7,Color.Yellow);
}
break;
case 8:if(x>=30&&x<=270&&y>=10&&y<=190)
{
paintBlock(8,Color.Black);
y+=20;
paintBlock(8,Color.Yellow);
}
break;
case 9:if(x>=30&&x<=250&&y>=30&&y<=230)
{
paintBlock(9,Color.Black);
y+=20;
paintBlock(9,Color.Yellow);
}
break;
}
}
private void inite()//初始化
{
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
table[i,j]=1;
}
for(int i=0;i<14;i++)
for(int j=1;j<14;j++)
{
table[i,j]=0;
}
}
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),10,10,300,300);
g.FillRectangle(new SolidBrush(Color.Black),30,10,260,280);
block(getRandomBlock());
timer1.Start();
}
}
}
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--;
}
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) ////////////////move
{
int a,b;
if((int)e.KeyCode==37)
{
switch(currentshape)
{
case 1: if(x>=50&&x<=250&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a+1,b-1]==0)
{
paintBlock(1,Color.Black);
x-=20;
paintBlock(1,Color.Yellow);
}
}
break;
case 2: if(x>=50&&x<=230&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a+1,b]==0)
{
paintBlock(2,Color.Black);
x-=20;
paintBlock(2,Color.Yellow);
}
}
break;
case 3: if(x>=50&&x<=210&&y>=10&&y<=270)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0)
{
paintBlock(3,Color.Black);
x-=20;
paintBlock(3,Color.Yellow);
}
}
break;
case 4: if(x>=50&&x<=230&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a+1,b]==0)
{
paintBlock(4,Color.Black);
x-=20;
paintBlock(4,Color.Yellow);
}
}
break;
case 5: if(x>=50&&x<=250&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a-1,b]==0&&table[a+1,b]==0)
{
paintBlock(5,Color.Black);
x-=20;
paintBlock(5,Color.Yellow);
}
}
break;
case 6: if(x>=70&&x<=250&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+1,b-2]==0&&table[a,b-1]==0)
{
paintBlock(6,Color.Black);
x-=20;
paintBlock(6,Color.Yellow);
}
}
break;
case 7: if(x>=50&&x<=250&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a+1,b-1]==0&&table[a+2,b-1]==0)
{
paintBlock(7,Color.Black);
x-=20;
paintBlock(7,Color.Yellow);
}
}
break;
case 8: if(x>=50&&x<=270&&y>=10&&y<=210)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a+1,b-1]==0&&table[a+2,b-1]==0&&table[a+3,b-1]==0)
{
paintBlock(8,Color.Black);
x-=20;
paintBlock(8,Color.Yellow);
}
}
break;
case 9: if(x>=50&&x<=250&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b-1]==0&&table[a+1,b-1]==0&&table[a-1,b]==0)
{
paintBlock(9,Color.Black);
x-=20;
paintBlock(9,Color.Yellow);
}
}
break;
}
}
if((int)e.KeyCode==39)
{
switch(currentshape)
{
case 1:if(x>=30&&x<=230&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+2,b]==0&&table[a+2,b+1]==0)
{
paintBlock(1,Color.Black);
x+=20;
paintBlock(1,Color.Yellow);
}
}
break;
case 2:if(x>=30&&x<=210&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+3]==0&&table[a+1,b+2]==0)
{
paintBlock(2,Color.Black);
x+=20;
paintBlock(2,Color.Yellow);
}
}
break;
case 3:if(x>=30&&x<=190&&y>=10&&y<=270)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+4]==0)
{
paintBlock(3,Color.Black);
x+=20;
paintBlock(3,Color.Yellow);
}
}
break;
case 4:if(x>=30&&x<=210&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+2]==0&&table[a+1,b=3]==0)
{
paintBlock(4,Color.Black);
x+=20;
paintBlock(4,Color.Yellow);
}
}
break;
case 5: if(x>=30&&x<=230&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a-1,b+2]==0&&table[a,b+2]==0&&table[a+1,b+2]==0)
{
paintBlock(5,Color.Black);
x+=20;
paintBlock(5,Color.Yellow);
}
}
break;
case 6: if(x>=50&&x<=230&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+1]==0&&table[a+1,b+2]==0)
{
paintBlock(6,Color.Black);
x+=20;
paintBlock(6,Color.Yellow);
}
}
break;
case 7: if(x>=30&&x<=230&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+1]==0&&table[a+1,b+2]==0&&table[a+2,b+1]==0)
{
paintBlock(7,Color.Black);
x+=20;
paintBlock(7,Color.Yellow);
}
}
break;
case 8: if(x>=30&&x<=250&&y>=10&&y<=210)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+1]==0&&table[a+1,b+1]==0&&table[a+2,b+1]==0&&table[a+3,b+1]==0)
{
paintBlock(8,Color.Black);
x+=20;
paintBlock(8,Color.Yellow);
}
}
break;
case 9: if(x>=30&&x<=230&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a,b+2]==0&&table[a+1,b+1]==0&&table[a-1,b+2]==0)
{
paintBlock(9,Color.Black);
x+=20;
paintBlock(9,Color.Yellow);
}
}
break;
}
}
if((int)e.KeyCode==38)
rotate();
if((int)e.KeyCode==40)
while(testCanDrop())
dropDown();
}
private bool testCanDrop()//测试是否可以下落
{
int a,b;
switch(currentshape)
{
case 1: if(x>=30&&x<=250&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+2,b]==0&&table[a+2,b+1]==0)
return true;
else
return false;
}
else
return false;
case 2:if(x>=30&&x<=230&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+2,b+1]==0&&table[a+1,b]==0&&table[a+1,b+2]==0)
return true;
else
return false;
}
else
return false;
case 3:if(x>=30&&x<=210&&y>=10&&y<=250)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+1,b]==0&&table[a+1,b+1]==0&&table[a+1,b+2]==0&&table[a+1,b+3]==0)
return true;
else
return false;
}
else
return false;
case 4:if(x>=30&&x<=210&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+2,b+1]==0&&table[a+2,b+2]==0)
return true;
else
return false;
}
else
return false;
case 5:if(x>=30&&x<=250&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+1,b]==0&&table[a+2,b+1]==0)
return true;
else
return false;
}
else
return false;
case 6:if(x>=30&&x<=250&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+2,b-1]==0&&table[a+2,b]==0&&table[a+2,b+1]==0)
return true;
else
return false;
}
else
return false;
case 7:if(x>=30&&x<=250&&y>=10&&y<=210)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+3,b]==0&&table[a+2,b+1]==0)
return true;
else
return false;
}
else
return false;
case 8:if(x>=30&&x<=270&&y>=10&&y<=190)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+4,b]==0)
return true;
else
return false;
}
else
return false;
case 9:if(x>=30&&x<=250&&y>=10&&y<=230)
{
a=(y-10)/20;
b=(x-10)/20;
if(table[a+2,b]==0&&table[a+1,b+1]==0)
return true;
else
return false;
}
else
return false;
default:
return false;
}
}
}
}
浙公网安备 33010602011771号