请问一下各位大侠,
是用C#语言写
那个俄罗斯方块的游戏,开始只画出一个方块,然后利用写出的这个方块,让它随机生成那个由四个方块组成的四种不同的形状,怎么写,
要求:
1 还要能让它的各个形状的颜色也是随机产生,
2 各个方块的颜色也不同,在线等
是用C#语言写
那个俄罗斯方块的游戏,开始只画出一个方块,然后利用写出的这个方块,让它随机生成那个由四个方块组成的四种不同的形状,怎么写,
要求:
1 还要能让它的各个形状的颜色也是随机产生,
2 各个方块的颜色也不同,在线等
{{{1,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}},
{{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0}},
{{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}},
{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0}}}
看出来没有?没有的再仔细看看吧
再定义方块的颜色,如下
int[] g_clrColors = {Color.ARGB(...), Color.ARGB(...)......}然后程序画的时候,就生成两个随机数,一个为方块的id1(即哪一种方块),一个是方块的方向
id2,例如方块id为1,方向id为2,则根据方块数组[id1][id2][][]这个数组来画,如果为1的话,就画一个正方向FillRect(....),需要的可以画边线,那么方块用什么颜色呢?就用方块的id号吧,即g_clrColors[id1],不过你的需求是颜色随机,那么就自己控制,生成一个未被占用的随机(就是自己循环生成随机数,直到生成一个被使用过的数),大概就是这样做,不知道楼主是不是想问这些(不是的话。。我撞豆腐算了)
1 还要能让它的各个形状的颜色也是随机产生,
2 各个方块的颜色也不同,形状和颜色范围都是固定的,用random随机产生后,然后合成即可。
{
Create(index);
} private Block[] blockList;
private int indexDef;
private Point ptPosition; private struct TETRIS
{
public int[,] def;
public int size; }
private static TETRIS[] tetrisDef;
public static void InitTetrisDefine()
{
tetrisDef = new TETRIS[7];
/*
for (int i=0; i<7; i++)
{
for (int m=0; m<4; m++)
{
for (int n=0; n<4; n++)
{
tetrisDef[i].def[m,n] = 0;
}
}
}
*/
tetrisDef[0].def = new int[4,4];
tetrisDef[0].def[1,0] = 8; // 0800
tetrisDef[0].def[1,1] = 8; // 0800
tetrisDef[0].def[1,2] = 8; // 0800
tetrisDef[0].def[1,3] = 8; // 0800
tetrisDef[0].size = 4; tetrisDef[1].def = new int[3,3];
tetrisDef[1].def[1,0] = 8; // 088
tetrisDef[1].def[1,1] = 8; // 080
tetrisDef[1].def[1,2] = 8; // 080
tetrisDef[1].def[2,0] = 8;
tetrisDef[1].size = 3; tetrisDef[2].def = new int[3,3];
tetrisDef[2].def[1,0] = 8; // 880
tetrisDef[2].def[1,1] = 8; // 080
tetrisDef[2].def[1,2] = 8; // 080
tetrisDef[2].def[0,0] = 8;
tetrisDef[2].size = 3; tetrisDef[3].def = new int[3,3];
tetrisDef[3].def[1,0] = 8; // 080
tetrisDef[3].def[0,1] = 8; // 888
tetrisDef[3].def[1,1] = 8; // 000
tetrisDef[3].def[2,1] = 8;
tetrisDef[3].size = 3; tetrisDef[4].def = new int[3,3];
tetrisDef[4].def[0,0] = 8; // 880
tetrisDef[4].def[1,0] = 8; // 088
tetrisDef[4].def[1,1] = 8; // 000
tetrisDef[4].def[2,1] = 8;
tetrisDef[4].size = 3; tetrisDef[5].def = new int[3,3];
tetrisDef[5].def[1,0] = 8; // 088
tetrisDef[5].def[2,0] = 8; // 880
tetrisDef[5].def[0,1] = 8; // 000
tetrisDef[5].def[1,1] = 8;
tetrisDef[5].size = 3; tetrisDef[6].def = new int[2,2];
tetrisDef[6].def[0,0] = 8; // 88
tetrisDef[6].def[0,1] = 8; // 88
tetrisDef[6].def[1,0] = 8;
tetrisDef[6].def[1,1] = 8;
tetrisDef[6].size = 2;
} public void Create(int index)
{
indexDef = index;
blockList = new Block[4];
int count = 0;
for (int i=0; i<tetrisDef[index].size; i++)
{
for (int j=0; j<tetrisDef[index].size; j++)
{
if (tetrisDef[index].def[i,j] != 0)
{
blockList[count] = new Block(index, i, j);
count++;
if (count > 4) return;
}
}
}
} public void Copy(Shape s)
{
ptPosition = s.Position;
for (int i=0; i<4; i++)
{
blockList[i].Position = s.GetBlock(i).Position;
}
} public Point Position
{
get
{
return ptPosition;
}
set
{
ptPosition = value;
}
} public int IndexDef
{
get
{
return indexDef;
}
} public void Draw(Graphics g)
{
Draw(g, false);
} public void Draw(Graphics g, Size sz)
{
Point pt = new Point((sz.Width - tetrisDef[indexDef].size * Block.Size) / 2,
(sz.Height - tetrisDef[indexDef].size * Block.Size) / 2);
for (int i=0; i<blockList.GetLength(0); i++)
{
blockList[i].Draw(g, pt, false);
}
} public void Draw(Graphics g, bool clear)
{
for (int i=0; i<blockList.GetLength(0); i++)
{
blockList[i].Draw(g, new Point(ptPosition.X * Block.Size, ptPosition.Y * Block.Size), clear);
}
} public void Roate()
{
for (int i=0; i<blockList.GetLength(0); i++)
{
Point pt = blockList[i].Position;
int temp = pt.X;
pt.X = tetrisDef[indexDef].size - pt.Y - 1;
pt.Y = temp;
blockList[i].Position = pt;
}
} public Block GetBlock(int index)
{
return blockList[index];
}
public void Draw(Graphics g)
{
DrawNextShape(g); for (int i=0; i<blockList.Count; i++)
{
((Block)(blockList[i])).Draw(g);
}
} public bool MoveShape(Graphics g, MOVE_TYPE m)
{
Shape s = new Shape(nextShape.IndexDef);
s.Copy(nextShape); Point pt = s.Position;
switch (m)
{
case MOVE_TYPE.MOVE_FALL:
{
while (ShapeCanPlace(s))
{
pt.Y++;
s.Position = pt;
} nextShape.Draw(g, true);
pt.Y--;
s.Position = pt;
nextShape.Copy(s);
nextShape.Draw(g);
PlaceShape();
return true;
//break;
}
case MOVE_TYPE.MOVE_DOWN:
pt.Y++;
break;
case MOVE_TYPE.MOVE_LEFT:
pt.X--;
break;
case MOVE_TYPE.MOVE_RIGHT:
pt.X++;
break;
case MOVE_TYPE.MOVE_ROATE:
s.Roate();
break;
default:
break;
} s.Position = pt;
if (ShapeCanPlace(s))
{
nextShape.Draw(g, true);
nextShape.Copy(s);
nextShape.Draw(g);
}
else
{
if (m == MOVE_TYPE.MOVE_DOWN)
{
PlaceShape();
return true;
}
}
return false;
} public bool ShapeCanPlace(Shape s)
{
for (int i=0; i<4; i++)
{
Point pt = s.Position;
Point ptOff = s.GetBlock(i).Position;
pt.Offset(ptOff.X, ptOff.Y);
if (PositionHasBlock(pt))
return false;
}
return true;
} public bool PositionHasBlock(Point pt)
{
if (pt.Y < 0) return false;
Rectangle rc = new Rectangle(0, 0, maxWidth, maxHeight);
if (!rc.Contains(pt))
{
return true;
}
for (int i=0; i<blockList.Count; i++)
{
if (((Block)(blockList[i])).Position == pt)
{
return true;
}
}
return false;
} public void PlaceShape()
{
for (int i=0; i<4; i++)
{
Point pt = nextShape.Position;
Point ptOff = nextShape.GetBlock(i).Position;
pt.Offset(ptOff.X, ptOff.Y);
nextShape.GetBlock(i).Position = pt;
blockList.Add(nextShape.GetBlock(i));
}
} public void Reset()
{
blockList.Clear();
} public int ClearLines()
{
int count = 0;
for (int i=0; i<maxHeight; i++)
{
bool clear = true;
for (int j=0; j<maxWidth; j++)
{
if (!PositionHasBlock(new Point(j, i)))
{
clear = false;
break;
}
}
if (clear)
{
for (int n=blockList.Count-1; n>=0; n--)
{
if (((Block)(blockList[n])).Position.Y == i)
{
blockList.RemoveAt(n);
}
else if (((Block)(blockList[n])).Position.Y < i)
{
Point pt = ((Block)(blockList[n])).Position;
pt.Y++;
((Block)(blockList[n])).Position = pt;
}
}
count++;
}
}
return count;
} public void DrawNextShape(Graphics g)
{
nextShape.Draw(g);
}
}
{
public Block()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Block(int index, Point pt)
{
colorIndex = index;
ptPosition = pt;
}
public Block(int index, int x, int y)
{
colorIndex = index;
ptPosition.X = x;
ptPosition.Y = y;
} private int colorIndex; // 颜色序号
private Point ptPosition; // 位置 private static int size = 20; // 方块大小
private static int COLOR_CHANGE = 60; private static Color[] clrDefine
= new Color[] {
Color.FromArgb(51, 204, 102), // 绿 Default color, or extend block color
Color.FromArgb(200, 200, 102), // 黄
Color.FromArgb(0, 143, 224), // 蓝
Color.FromArgb(153, 153, 204), // 青
Color.FromArgb(204, 204, 204), // 灰
Color.FromArgb(232, 123, 20), // 橙
Color.FromArgb(220, 39, 75) // 红 sample block color
}; // 颜色 public int ColorIndex
{
get
{
return colorIndex;
}
set
{
colorIndex = value;
}
} public Point Position
{
get
{
return ptPosition;
}
set
{
ptPosition = value;
}
} public void Draw(Graphics g, Point ptStart, bool clear)
{
if (clear)
{
g.FillRectangle(new SolidBrush(Color.White), ptStart.X + (ptPosition.X * size),
ptStart.Y + (ptPosition.Y * size), size, size);
}
else
{
g.FillRectangle(new SolidBrush(clrDefine[colorIndex]), ptStart.X + (ptPosition.X * size),
ptStart.Y + (ptPosition.Y * size), size, size);
g.DrawLine(new Pen(GetLightColor(colorIndex), 1), ptStart.X + (ptPosition.X * size), ptStart.Y + (ptPosition.Y * size), ptStart.X + (ptPosition.X * size) + size - 1, ptStart.Y + (ptPosition.Y * size));
g.DrawLine(new Pen(GetLightColor(colorIndex), 1), ptStart.X + (ptPosition.X * size), ptStart.Y + (ptPosition.Y * size), ptStart.X + (ptPosition.X * size), ptStart.Y + (ptPosition.Y * size) + size - 1);
g.DrawLine(new Pen(GetDarkColor(colorIndex), 1), ptStart.X + (ptPosition.X * size) + size - 1, ptStart.Y + (ptPosition.Y * size) + size - 1, ptStart.X + (ptPosition.X * size) + size - 1, ptStart.Y + (ptPosition.Y * size));
g.DrawLine(new Pen(GetDarkColor(colorIndex), 1), ptStart.X + (ptPosition.X * size) + size - 1, ptStart.Y + (ptPosition.Y * size) + size - 1, ptStart.X + (ptPosition.X * size), ptStart.Y + (ptPosition.Y * size) + size - 1);
//g.DrawRectangle(new Pen(Color.Black, 1), ptStart.X + (ptPosition.X * size),
// ptStart.Y + (ptPosition.Y * size), size-1, size-1);
}
} public void Draw(Graphics g)
{
Draw(g, new Point(0, 0), false);
} public static int Size
{
get
{
return size;
}
set
{
size = value;
}
} public static Color GetColor(int index)
{
return clrDefine[index];
} Color GetDarkColor(int index)
{
int r = clrDefine[index].R;
int g = clrDefine[index].G;
int b = clrDefine[index].B; r = r-COLOR_CHANGE<0 ? 0 : r-COLOR_CHANGE;
g = g-COLOR_CHANGE<0 ? 0 : g-COLOR_CHANGE;
b = b-COLOR_CHANGE<0 ? 0 : b-COLOR_CHANGE; Color c = Color.FromArgb(r, g, b);
return c;
} Color GetLightColor(int index)
{
int r = clrDefine[index].R;
int g = clrDefine[index].G;
int b = clrDefine[index].B; r = r+COLOR_CHANGE>255 ? 255 : r+COLOR_CHANGE;
g = g+COLOR_CHANGE>255 ? 255 : g+COLOR_CHANGE;
b = b+COLOR_CHANGE>255 ? 255 : b+COLOR_CHANGE; Color c = Color.FromArgb(r, g, b);
return c;
}
}