本人为一双非大一计科新生,这是我第一篇文章,能力一般,水平有限,能在各位大佬面前弄斧,不胜荣幸。
事情是这样的:
我寒假买了一个3ds掌机,玩了里面很多游戏,其中最令我着迷的就是俄罗斯方块。说实话以前也玩过,但不知怎么就上瘾了,沉迷于刷分,什么马里奥、口袋妖怪在此游戏面前黯然失色。
(目前经典模式是12800分,消了131行,不服来战)
因为寒假学了c#嘛,就想用c#复刻一款俄罗斯方块出来,一来满足我在pc上玩俄罗斯方块的愿望,二来检验一下学习成果。
因为想挑战一下自己,所以没有看任何有关俄罗斯方块算法的东西,所以以下内容均本人原创,算法可能不是最好的,如果有大佬能有更好的办法,欢迎在评论区告诉我
游戏效果视频见下方视频,别忘了一键三连嗷
BV19b4y1p77A
好了屁话不多说了,我们开始吧!
关于画面
是窗体程序嘛,肯定要有画面。我在此之前就学了半个小时gdi绘图,然后就把相关函数写出来了,所以我就不做过多赘述。简单说一下需求,三个函数,首先是画方格函数,这个我gdi看了五分钟就写出来了,最简单
public void DrawBoard()
{
Graphics board = this.CreateGraphics();
Pen pen = new Pen(Brushes.Gray);
//画10x20棋盘
for (byte i = 0; i <= 20; i++)
{
Point p00 = new Point(xOy_x, xOy_y + size * i);
Point p01 = new Point(xOy_x + size * 10, xOy_y + size * i);
board.DrawLine(pen, p00, p01);
}
for (byte i = 0; i <= 10; i++)
{
Point p00 = new Point(xOy_x + size * i, xOy_y);
Point p01 = new Point(xOy_x + size * i, xOy_y + size * 20);
board.DrawLine(pen, p00, p01);
}
}
然后是画下落对象的函数,我这里建了一个Tpoint类,要传入一个Tpoint类数组(就是4个坐标)
public class Tpoint//Tpoint类数组
{
public int x;
public int y;
}
public void MoveBlock(GO type,Tpoint[] point4)
{
for (byte i=0;i<4;i++)
DrawOneMoveBlock(type,(byte)point4[i].x, (byte)point4[i].y);
}
public void DrawOneMoveBlock(GO gameObject, byte x, byte y)
{
if (y >= 4)
{
y -= 4;
Graphics centureBlock = this.CreateGraphics();
Brush brush;
switch(gameObject)
{
case GO.I:brush = Brushes.Cyan;break;
case GO.L:brush = Brushes.Magenta;break;
case GO.O:brush = Brushes.Yellow;break;
case GO.Z:brush = Brushes.DeepSkyBlue;break;
case GO.T:brush = Brushes.DarkOrchid;break;
case GO.tL: brush = Brushes.OrangeRed;break;
case GO.tZ: brush = Brushes.SpringGreen;break;
default:brush = Brushes.Black;break;
}
Pen blockPen = new Pen(brush);
Rectangle rectangle = new Rectangle(xOy_x + x * size , xOy_y + y * size , size , size );
centureBlock.DrawRectangle(blockPen, rectangle);
}
(这里的颜色和掌机里的颜色是对应的)
最后是画固定方块的函数,传入的是一个bool类二维数组
public void StaticBlock(bool[,] array)
{
for (byte i = 0; i < 20; i++)
for (byte j = 0; j < 10; j++)
if (array[i, j])
DrawOneExtenBlock(j, i);
}
public void DrawOneExtenBlock(byte x,byte y)
{
Graphics centureBlock = this.CreateGraphics();
Pen blockPen = new Pen(Brushes.Lime);
Point p1 = new Point(xOy_x + x * size + 1, xOy_y + y * size + 1);
Point p2 = new Point(xOy_x + (x + 1) * size - 1, xOy_y + y * size + 1);
Point p3 = new Point(xOy_x + x * size + 1, xOy_y + (y + 1) * size - 1);
Point p4 = new Point(xOy_x + (x + 1) * size - 1, xOy_y + (y + 1) * size - 1);
centureBlock.DrawLine(blockPen, p1, p4);
centureBlock.DrawLine(blockPen, p2, p3);
Rectangle rectangle = new Rectangle(xOy_x + x * size + 1, xOy_y + y * size + 1, size - 2, size - 2);
centureBlock.DrawRectangle(blockPen, rectangle);
}
固定方块效果