文件名称:c#做的俄罗斯方块,不下后悔
文件大小:242KB
文件格式:RAR
更新时间:2012-06-07 10:16:35
c# 俄罗斯方块 c#做的俄罗斯方块
提供一个核心类,接口仅几个,方便外围开发,你可以在此基础上做一些扩展,如使其界面更美观,方块更美观等
class DyPanel :Panel
{
#region 类属性
private Timer t_slow,t_fast;
private XY current_p, start_p;
private XY []cur_square;
private XY []afterChange_squar;
private int width, height;
private SolidBrush bkbrush;
private bool [][]ha;
private int m, n;
private XY [][][]a;
private int rotate_outer, rotate_inner;
private int outer, inner;
private Random ra;
private Panel p;
private XY reference;
private int mark;
private Label markl;
public char move_flag;
private SolidBrush sqbrush;
#endregion
#region 公有的方法
public DyPanel(Rectangle rf,int width,int height,SolidBrush brush,ref Timer slow,ref Timer fast,Panel p,XY reference,Label markl)
{
sqbrush = new SolidBrush(Color.FromArgb(150,225,200,12));
this.markl = markl;
mark = 0;
markl.Text = mark.ToString();
ra = new Random();
this.p = p;
this.reference = reference;
a=new XY[6][][];
for (int i = 0; i < 6;i++ )
a[i]=new XY[4][];
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
a[i][j]=new XY[4];
for (int e = 0; e < 4; e++)
a[i][j][e] = new XY();
}
}
setA();
cur_square=new XY[4];
afterChange_squar=new XY[4];
for (int i = 0; i < 4; i++)
{
cur_square[i] = new XY();
afterChange_squar[i] = new XY();
}
outer = rand(0, 6);
inner = rand(0, 4);
rotate_outer = outer;
rotate_inner = inner;
copyXY(a[outer][inner], ref cur_square);
outer = rand(0, 6);
inner = rand(0, 4);
current_p = new XY();
start_p = new XY();
this.width = width;
this.height = height;
this.bkbrush = brush;
this.t_slow = slow;
this.t_fast = fast;
move_flag = 'p';
this.Size = new Size(rf.Right - rf.Left, rf.Bottom - rf.Top);
this.Location = rf.Location;
n = (this.ClientRectangle.Right - this.ClientRectangle.Left) / width;
m = (this.ClientRectangle.Bottom - this.ClientRectangle.Top) / height;
this.start_p.x = n / 2;
this.start_p.y = 2;
current_p.x = start_p.x;
current_p.y = start_p.y;
ha = new bool[m][];
for (int i = 0; i < m; i++)
{
ha[i] = new bool[n];
for (int j = 0; j < n; j++)
ha[i][j] = false;
}
this.BackColor = Color.SlateBlue;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_Paint);
setTimer(true, false);
}
public int getMark()
{
return mark;
}
public void ReStart()
{
setTimer(false, false);
mark = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
ha[i][j] = false;
current_p.x=start_p.x;
current_p.y = start_p.y;
mark = 0;
markl.Text = mark.ToString();
Logic_Expunction();
Real_Expunction();
}
public void draw()
{
Timer cur_t = t_fast.Enabled ? t_fast : t_slow;
cur_t.Enabled = false;
XY xy = new XY();
XY []original=new XY[4];
for (int i = 0; i < 4; i++)
original[i] = new XY();
if (Square_Move(move_flag, ref xy,ref original))
{
if (move_flag != 'w' && move_flag != 'W')
{
setSelfLogic(cur_square, xy, false, 4);
int i;
for (i = 0; i < 4; i++)
{
if (ha[cur_square[i].y - cur_square[0].y + current_p.y][cur_square[i].x - cur_square[0].x + current_p.x])
break;
}
if(i==4)
{
draw_square(new SolidBrush(this.BackColor), xy);
setSelfLogic(cur_square, current_p, true, 4);
draw_square(sqbrush, current_p);
}
else
{
current_p.x = xy.x;
current_p.y = xy.y;
setSelfLogic(cur_square, current_p, true, 4);
}
}
else
{
setSelfLogic(original, xy, false, 4);
draw_square(new SolidBrush(this.BackColor), xy,original);
setSelfLogic(cur_square, current_p, true, 4);
draw_square(sqbrush, current_p);
}
}
else
{
setSelfLogic(cur_square, current_p, true, 4);
current_p.x = start_p.x;
current_p.y = start_p.y;
if (IsFail())
{
MessageBox.Show("Fail!");
setTimer(false, false);
return;
}
copyXY(a[outer][inner], ref cur_square);
rotate_outer = outer;
rotate_inner = inner;
outer = rand(0, 6);
inner = rand(0, 4);
p.Invalidate();
Logic_Expunction();
Real_Expunction();
}
move_flag = 'p';
cur_t.Enabled = true;
}
public void setTimer(bool slow, bool fast)
{
t_slow.Enabled = slow;
t_fast.Enabled = fast;
}
public void draw(Panel p,bool bkColor)
{
Color color;
if(bkColor)
{
color = p.BackColor;
}
else
{
color = sqbrush.Color;
}
for (int i = 0; i < 4; i++)
{
p.CreateGraphics().DrawRectangle(new Pen(Brushes.Chocolate, 1), (a[outer][inner][i].x - a[outer][inner][0].x + reference.x) * width, (a[outer][inner][i].y - a[outer][inner][0].y + reference.y) * height, width, height);
}
}
#endregion
#region 私有的方法
private bool Square_Move(char a, ref XY xy,ref XY []original)
{
switch (a)
{
case 'a':
case 'A':
if (CanLeft() && CanDown())
{
xy.x = current_p.x;
xy.y = current_p.y;
current_p.x--;
current_p.y++;
}
else if(CanDown())
{
xy.x = current_p.x;
xy.y = current_p.y;
current_p.y++;
}
else
return false;
break;
case 'd':
case 'D':
if (CanRight() && CanDown())
{
xy.x = current_p.x;
xy.y = current_p.y;
current_p.x++;
current_p.y++;
}
else if (CanDown())
{
xy.x = current_p.x;
xy.y = current_p.y;
current_p.y++;
}
else
return false;
break;
case 'w':
case 'W':
if(CanRotate())
{
copyXY(cur_square, ref original);
copyXY(afterChange_squar, ref cur_square);
xy.x = current_p.x;
xy.y = current_p.y;
//current_p.y++;
}
break;
default:
if (CanDown())
{
xy.x = current_p.x;
xy.y = current_p.y;
current_p.y++;
}
else
return false;
break;
}
return true;
}
private int rand(int least,int largest)
{
return ra.Next(least, largest);
}
private void copyXY(XY[] s, ref XY[] t)
{
for (int i = 0; i < 4; i++)
{
t[i].x = s[i].x;
t[i].y = s[i].y;
}
}
private void draw_square(SolidBrush sb, XY reference)
{
for (int i = 0; i < 4; i++)
{
this.CreateGraphics().FillRectangle(sb, (cur_square[i].x - cur_square[0].x + reference.x) * width, (cur_square[i].y - cur_square[0].y + reference.y) * height, width, height);
}
}
private void draw_square(SolidBrush sb, XY reference,XY []orientation)
{
for (int i = 0; i < 4; i++)
{
this.CreateGraphics().FillRectangle(sb, (orientation[i].x - orientation[0].x + reference.x) * width, (orientation[i].y - orientation[0].y + reference.y) * height, width, height);
}
}
private void setA()
{
a[0][0][0].x = 1;
a[0][0][0].y = 1;
a[0][0][1].x = 0;
a[0][0][1].y = 1;
a[0][0][2].x = 0;
a[0][0][2].y = 2;
a[0][0][3].x = 1;
a[0][0][3].y = 0;
a[0][1][0].x = 1;
a[0][1][0].y = 1;
a[0][1][1].x = 0;
a[0][1][1].y = 0;
a[0][1][2].x = 1;
a[0][1][2].y = 0;
a[0][1][3].x = 2;
a[0][1][3].y = 1;
a[0][2][0].x = 1;
a[0][2][0].y = 1;
a[0][2][1].x = 0;
a[0][2][1].y = 1;
a[0][2][2].x = 0;
a[0][2][2].y = 2;
a[0][2][3].x = 1;
a[0][2][3].y = 0;
a[0][3][0].x = 1;
a[0][3][0].y = 1;
a[0][3][1].x = 0;
a[0][3][1].y = 0;
a[0][3][2].x = 1;
a[0][3][2].y = 0;
a[0][3][3].x = 2;
a[0][3][3].y = 1;
//end 0
a[1][0][0].x = 1;
a[1][0][0].y = 1;
a[1][0][1].x = 0;
a[1][0][1].y = 0;
a[1][0][2].x = 0;
a[1][0][2].y = 1;
a[1][0][3].x = 1;
a[1][0][3].y = 2;
a[1][1][0].x = 1;
a[1][1][0].y = 1;
a[1][1][1].x = 0;
a[1][1][1].y = 1;
a[1][1][2].x = 1;
a[1][1][2].y = 0;
a[1][1][3].x = 2;
a[1][1][3].y = 0;
a[1][2][0].x = 1;
a[1][2][0].y = 1;
a[1][2][1].x = 0;
a[1][2][1].y = 0;
a[1][2][2].x = 0;
a[1][2][2].y = 1;
a[1][2][3].x = 1;
a[1][2][3].y = 2;
a[1][3][0].x = 1;
a[1][3][0].y = 1;
a[1][3][1].x = 0;
a[1][3][1].y = 1;
a[1][3][2].x = 1;
a[1][3][2].y = 0;
a[1][3][3].x = 2;
a[1][3][3].y = 0;
//end 1
a[2][0][0].x = 1;
a[2][0][0].y = 1;
a[2][0][1].x = 0;
a[2][0][1].y = 0;
a[2][0][2].x = 1;
a[2][0][2].y = 0;
a[2][0][3].x = 0;
a[2][0][3].y = 1;
a[2][1][0].x = 1;
a[2][1][0].y = 1;
a[2][1][1].x = 0;
a[2][1][1].y = 0;
a[2][1][2].x = 1;
a[2][1][2].y = 0;
a[2][1][3].x = 0;
a[2][1][3].y = 1;
a[2][2][0].x = 1;
a[2][2][0].y = 1;
a[2][2][1].x = 0;
a[2][2][1].y = 0;
a[2][2][2].x = 1;
a[2][2][2].y = 0;
a[2][2][3].x = 0;
a[2][2][3].y = 1;
a[2][3][0].x = 1;
a[2][3][0].y = 1;
a[2][3][1].x = 0;
a[2][3][1].y = 0;
a[2][3][2].x = 1;
a[2][3][2].y = 0;
a[2][3][3].x = 0;
a[2][3][3].y = 1;
//end 2
a[3][0][0].x = 1;
a[3][0][0].y = 1;
a[3][0][1].x = 0;
a[3][0][1].y = 0;
a[3][0][2].x = 1;
a[3][0][2].y = 0;
a[3][0][3].x = 1;
a[3][0][3].y = 2;
a[3][1][0].x = 2;
a[3][1][0].y = 1;
a[3][1][1].x = 2;
a[3][1][1].y = 0;
a[3][1][2].x = 1;
a[3][1][2].y = 1;
a[3][1][3].x = 0;
a[3][1][3].y = 1;
a[3][2][0].x = 1;
a[3][2][0].y = 2;
a[3][2][1].x = 0;
a[3][2][1].y = 0;
a[3][2][2].x = 0;
a[3][2][2].y = 1;
a[3][2][3].x = 0;
a[3][2][3].y = 2;
a[3][3][0].x = 1;
a[3][3][0].y = 0;
a[3][3][1].x = 2;
a[3][3][1].y = 0;
a[3][3][2].x = 0;
a[3][3][2].y = 0;
a[3][3][3].x = 0;
a[3][3][3].y = 1;
//end 3
a[4][0][0].x = 0;
a[4][0][0].y = 1;
a[4][0][1].x = 0;
a[4][0][1].y = 0;
a[4][0][2].x = 0;
a[4][0][2].y = 2;
a[4][0][3].x = 1;
a[4][0][3].y = 0;
a[4][1][0].x = 2;
a[4][1][0].y = 1;
a[4][1][1].x = 0;
a[4][1][1].y = 0;
a[4][1][2].x = 1;
a[4][1][2].y = 0;
a[4][1][3].x = 2;
a[4][1][3].y = 0;
a[4][2][0].x = 1;
a[4][2][0].y = 2;
a[4][2][1].x = 0;
a[4][2][1].y = 2;
a[4][2][2].x = 1;
a[4][2][2].y = 0;
a[4][2][3].x = 1;
a[4][2][3].y = 1;
a[4][3][0].x = 1;
a[4][3][0].y = 1;
a[4][3][1].x = 0;
a[4][3][1].y = 0;
a[4][3][2].x = 0;
a[4][3][2].y = 1;
a[4][3][3].x = 2;
a[4][3][3].y = 1;
//end 4
a[5][0][0].x = 1;
a[5][0][0].y = 0;
a[5][0][1].x = 0;
a[5][0][1].y = 0;
a[5][0][2].x = 2;
a[5][0][2].y = 0;
a[5][0][3].x = 3;
a[5][0][3].y = 0;
a[5][1][0].x = 0;
a[5][1][0].y = 1;
a[5][1][1].x = 0;
a[5][1][1].y = 2;
a[5][1][2].x = 0;
a[5][1][2].y = 3;
a[5][1][3].x = 0;
a[5][1][3].y = 0;
a[5][2][0].x = 1;
a[5][2][0].y = 0;
a[5][2][1].x = 0;
a[5][2][1].y = 0;
a[5][2][2].x = 2;
a[5][2][2].y = 0;
a[5][2][3].x = 3;
a[5][2][3].y = 0;
a[5][3][0].x = 0;
a[5][3][0].y = 1;
a[5][3][1].x = 0;
a[5][3][1].y = 2;
a[5][3][2].x = 0;
a[5][3][2].y = 3;
a[5][3][3].x = 0;
a[5][3][3].y = 0;
}
private bool IsFail()
{
for (int i = 0; i < 4; i++)
if (ha[cur_square[i].y - cur_square[0].y + current_p.y + 1][cur_square[i].x - cur_square[0].x + current_p.x])
{
setSelfLogic(cur_square, current_p, true, 4);
return true;
}
return false;
}
private void setSelfLogic(XY[] xy, XY reference,bool logic,int m)
{
for(int i=0;i
【文件预览】:
fangkuai
----fangkuai()
--------bin()
--------Form1.Designer.cs(16KB)
--------Program.cs(467B)
--------obj()
--------fangkuai.csproj(3KB)
--------Form1.cs(26KB)
--------Form1.resx(33KB)
--------Properties()
----项目1.ico(1KB)
----fangkuai.suo(27KB)
----fangkuai.sln(2KB)
----方块定义示意(数组中顺序).bmp(199KB)
----TestProject1()
--------bin()
--------obj()
--------TestProject1.csproj(2KB)
--------GenericTest1.GenericTest(3KB)
--------AuthoringTests.txt(5KB)
--------Properties()
----fangkuai.vsmdi(1KB)
----localtestrun.testrunconfig(5KB)