学习C#从俄罗斯方块开始(二)俄罗斯方块的规则和算法的简单实现

本文详细介绍了如何使用C#语言实现一个俄罗斯方块游戏的全过程,包括游戏规则分析、算法设计、开发步骤和具体代码实现。通过本教程,读者可以掌握项目开发的基本经验,了解如何将需求转化为计算机可执行的代码。

 根据上节我们规划好的开发过程,这节我们来实现一个简单的俄罗斯方块程序。当然完成俄罗斯方块的Demo是我们这节课的最终目标。在实现这个目标的同时,我还要跟大家一起去分析俄罗斯方块的需求、算法、具体开发的步骤。并且最终选择C#语言来实现这个demo。下面废话少说,翠花,上内容!

 


第一步、   思考俄罗斯方块的游戏规则:

      任何时候我们去开发或者设计一个项目,首先需要思考的就是需求。

      俄罗斯方块中,我们要做一个俄罗斯方块的游戏,那么俄罗斯方块游戏的游戏规则就是我们的需求。所以我们必须深入了解俄罗斯方块游戏的游戏规则。俄罗斯方块游戏可能产生下面五种形状的方块。这些方块有我们的方向键的控制可以产生旋转,左右移动和加速前进等,往下掉,只到遇到下面已经有方块挡住停下来。如果一行完全排列满,则会消掉,如果排列到了顶部,则会失败。

 

第二步、把我们的“需求”从计算机的角度去分析:

     我们对任何一个项目的最初的需求是从使用者的角度去描述这个项目的,这种描述基本上涵盖了使用者可能用到的功能和要求。我们在做一个项目,特别是为一个非计算机专业做一个项目的时候,客户的描述,或者业务人员承接到项目的时候,他们和客户的沟通,基本上是基于这种初级的需求的,这种初级的需求的有点是:客户和业务人员比较容易理解,沟通起来非常的方便。但是他的缺点是非计算机专业用语,不够规范和严谨。不能拿这个文档和需求来直接要求开发人员。需要一个对市场(客户)比较了解,同时又是资深的程序员把这个需求编程一个专业开发人员相对比较容易理解的需求。甚至直接编程开发的具体思路。然后有开发人员去实现。我们的小游戏实在是太小了,不可能分出来业务人员、项目规划人员、项目架构、程序开发。。。。所以这一切也都有我们开发者一个人独揽了。不过麻雀虽小,五脏俱全哦。我们不是说了嘛,通过学习俄罗斯方块掌握项目开发的经验呢。所以希望各位看客认真的看这些个步骤,如果我哪里说错了,欢迎拍砖。

     a、关于俄罗斯方块游戏中方块的产生。我们假设产生四种方块。实际上俄罗斯方块产生的方块要多余四个。但是咱们这节课是做一个Demo,所以先做四个,具体的四个请见图片1。(因为优快云不能上传图片,我上传到下载中去了。大家可以下载使用,期待csdn可以正常的上传图片)。

    b、俄罗斯方块当需要变化方块的时候,每个方块需要顺时针旋转90°,具体可能产生的形状也见图一。

从数学,或者计算机的角度来看:我们根据图1来看,我们可以把所有的方块看成一个4*4的二维数组。把有“砖”的地方表示为1,没有“砖”的地方表示为0,所以所有的的方块都可以表示为图二的样式。

    c、我们也可以把背景看成是14*20的二维数组。

       那么俄罗斯方块的需求,站在程序员的角度上就可以变成:我们随机从方块的4个4*4的矩阵中挑选出来一个,并且随机的挑选一个他的初始化状态(关于状态变化,我们同样可以把他们表示在一个4*4的矩阵中)。然后这个被挑选的矩阵,在一个14*20的矩阵中不断的按照一种速度进行往下运动。我们同时可以使用方向键,对这个举证进行控制,使得它可以左右运动,同时可以循环的变化他的状态。如果这个矩阵在运动的方向上遇到了数值为1的时候则停止运动,在左右运动上表现为不能移动,在往下运动的时候则表现为这个方块运动的结束。把这个矩阵的数值复制到背景矩阵中去。这个时候检查背景矩阵,如果背景矩阵中有一个行全部为1,那么在y轴上比该行小的所有行向下移动一行,用户得分增加100。同理,检查所有的行,并且做同样动作。检查完成后,进入下个方块的随机挑选,下落。当某个方块下落完成的时候。他的y坐标在背景中为0的时候。游戏结束。作为游戏的界面,我们需要在游戏的状态发生改变的时候,把背景矩阵和运动矩阵都绘制出来。数值为0的地方不绘图,数值为1的地方绘制图片。

---------------------------------------------------------------------------

到此为止。我们的游戏已经用人类语言。从计算机的角度来说分析完成了。剩下的就是使用计算机语言表达出来了,不管你是学习的C#,C语言,C++,Delphi,Java,python,Js,VB,……(汇编我不懂,暂时不包括吧)。只要你能理解了上述的“思路”。那么你可以用任何语言来写出来。我真希望用所有的语言都给大家写写,把IT老祖宗的那句:“编程靠的是思想(想法,思路),语言不重要。"的真理来验证下,可怜优快云的blog写起来太麻烦了,而且我老婆催着我赶紧回家过年的,所以这次就暂时使用C#来给大家实现下。我们就从一个C#盲来开始看看需要如何学习,如何实现。

 

       首先、确定我们要用的语言是C#,矩阵的运算需要多是多维数组,数组的运算多需要循环。另外对多种情况的判断需要if,switch等分支判断语句,我们的方块定时运动,需要用到timer控件。所以在开始咱们的项目前,请确保,你已经对C#的语法有了基本的了解,尤其对数组的操作、if语句,for循环语句,switch语句要会用。同时对基本的button、timer控件进行基本的了解。如果你在使用其他语言编程,找你所使用的语言中的类似的东西。其他的东西至少这个项目中不会经常用到。即时用到,查查即可。这个可能就是所谓的“语言没有思想重要吧”。如果你还不懂,去看看<C#入门经典吧>.

        其次、俄罗斯方块需要绘制图形,这个需要你对C# GDI+有所了解。在其他语言中可能是GDI或者API了,这个你只需要google下即可。如果GDI+编程你还不懂,那么来下载个教程吧。我已经给大家上传到:http://download.youkuaiyun.com/source/2057311

       上面的两个条件如果你已经具有了。那么开始我们的C#俄罗斯方块的代码之旅途吧。

       不过在代码开始之前,还是先看看我们的界面吧。请看界面图;

       下面我们来看代码。首先定义我们的砖块和背景吧。

[c-sharp] view plain copy print ?
  1. #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行  
  2.        private int[, , ,] Tricks = {{  
  3.                                      {  
  4.                                          {1,0,0,0},  
  5.                                          {1,0,0,0},  
  6.                                          {1,0,0,0},  
  7.                                          {1,0,0,0}  
  8.                                      },  
  9.                                      {  
  10.                                          {1,1,1,1},  
  11.                                          {0,0,0,0},  
  12.                                          {0,0,0,0},  
  13.                                          {0,0,0,0}  
  14.                                      },  
  15.                                      {  
  16.                                          {1,0,0,0},  
  17.                                          {1,0,0,0},  
  18.                                          {1,0,0,0},  
  19.                                          {1,0,0,0}  
  20.                                      },  
  21.                                      {  
  22.                                          {1,1,1,1},  
  23.                                          {0,0,0,0},  
  24.                                          {0,0,0,0},  
  25.                                          {0,0,0,0}  
  26.                                      }  
  27.                                  },  
  28.                                  {  
  29.                                       {  
  30.                                           {1,1,0,0},  
  31.                                           {1,1,0,0},  
  32.                                           {0,0,0,0},  
  33.                                           {0,0,0,0}  
  34.                                       },  
  35.                                       {  
  36.                                           {1,1,0,0},  
  37.                                           {1,1,0,0},  
  38.                                           {0,0,0,0},  
  39.                                           {0,0,0,0}  
  40.                                       },  
  41.                                       {  
  42.                                           {1,1,0,0},  
  43.                                           {1,1,0,0},  
  44.                                           {0,0,0,0},  
  45.                                           {0,0,0,0}  
  46.                                       },  
  47.                                       {  
  48.                                           {1,1,0,0},  
  49.                                           {1,1,0,0},  
  50.                                           {0,0,0,0},  
  51.                                           {0,0,0,0}  
  52.                                       }  
  53.                                   },  
  54.                                   {  
  55.                                       {  
  56.                                           {1,0,0,0},  
  57.                                           {1,1,0,0},  
  58.                                           {0,1,0,0},  
  59.                                           {0,0,0,0}  
  60.                                       },  
  61.                                       {  
  62.                                           {0,1,1,0},  
  63.                                           {1,1,0,0},  
  64.                                           {0,0,0,0},  
  65.                                           {0,0,0,0}  
  66.                                       },  
  67.                                       {  
  68.                                           {1,0,0,0},  
  69.                                           {1,1,0,0},  
  70.                                           {0,1,0,0},  
  71.                                           {0,0,0,0}  
  72.                                       },  
  73.                                       {  
  74.                                           {0,1,1,0},  
  75.                                           {1,1,0,0},  
  76.                                           {0,0,0,0},  
  77.                                           {0,0,0,0}  
  78.                                       }  
  79.                                   },  
  80.                                   {  
  81.                                       {  
  82.                                           {1,1,0,0},  
  83.                                           {0,1,0,0},  
  84.                                           {0,1,0,0},  
  85.                                           {0,0,0,0}  
  86.                                       },  
  87.                                       {  
  88.                                           {0,0,1,0},  
  89.                                           {1,1,1,0},  
  90.                                           {0,0,0,0},  
  91.                                           {0,0,0,0}  
  92.                                       },  
  93.                                       {  
  94.                                           {1,0,0,0},  
  95.                                           {1,0,0,0},  
  96.                                           {1,1,0,0},  
  97.                                           {0,0,0,0}  
  98.                                       },  
  99.                                       {  
  100.                                           {1,1,1,0},  
  101.                                           {1,0,0,0},  
  102.                                           {0,0,0,0},  
  103.                                           {0,0,0,0}  
  104.                                       }  
  105.                                   }};  
  106.  
  107.        #endregion  
  108.        #region 定义背景  
  109.        private int[,] bgGraoud ={  
  110.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  111.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  112.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  113.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  114.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  115.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  116.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  117.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  118.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  119.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  120.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  121.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  122.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  123.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  124.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  125.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  126.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  127.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  128.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  129.                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0}  
  130.                                 };  
  131.        #endregion  

#region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 private int[, , ,] Tricks = {{ { {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0} }, { {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}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {1,1,0,0}, {0,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,1,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, { {1,0,0,0}, {1,0,0,0}, {1,1,0,0}, {0,0,0,0} }, { {1,1,1,0}, {1,0,0,0}, {0,0,0,0}, {0,0,0,0} } }}; #endregion #region 定义背景 private int[,] bgGraoud ={ {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; #endregion

 

 

砖块定义好了,我们还需要定义几个系统在变化的时候需要的全局变量。我在注视中写清楚了,不絮叨了。

[c-sharp] view plain copy print ?
  1. private int[,] CurrentTrick = new int[4, 4]; //当前的砖块  
  2.         //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态, CurrentX当前x, CurrentY当前y, Sorce分数  
  3.         private int CurrentTrickNum, CurrentStatusNum, CurrentX, CurrentY, Sorce;  
  4.         private int TricksNum = 4;  
  5.         private int StatusNum = 4;  
  6.         private Image myImage;  
  7.         private Random rand = new Random();  

private int[,] CurrentTrick = new int[4, 4]; //当前的砖块 //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态, CurrentX当前x, CurrentY当前y, Sorce分数 private int CurrentTrickNum, CurrentStatusNum, CurrentX, CurrentY, Sorce; private int TricksNum = 4; private int StatusNum = 4; private Image myImage; private Random rand = new Random();

 

定义好了变量,我们来想想我们的几个函数吧。首先是变化砖块,变化砖块其实就是变化砖块的状态,把砖块数组中的状态位进行循环变化即可:

[c-sharp] view plain copy print ?
  1. /// <summary>  
  2.         /// 变化方块  
  3.         /// </summary>  
  4.         private void ChangeTricks()  
  5.         {  
  6.             if (CurrentStatusNum < 3)  
  7.             {  
  8.                 CurrentStatusNum++;  
  9.             }  
  10.             else  
  11.             {  
  12.                 CurrentStatusNum = 0;  
  13.             }  
  14.             for (int y = 0; y < 4; y++)  
  15.             {  
  16.                 for (int x = 0; x < 4; x++)  
  17.                 {  
  18.                     CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];  
  19.                 }  
  20.             }  
  21.         }  

/// <summary> /// 变化方块 /// </summary> private void ChangeTricks() { if (CurrentStatusNum < 3) { CurrentStatusNum++; } else { CurrentStatusNum = 0; } for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x]; } } }

 

再接着是检测砖块是否可以向下移动、向左移动、向右移动。思路很简单。看看他的要运动的方向的背景下个位置是不是为1.如果不是。那么就返回true.否则是false:

[c-sharp] view plain copy print ?
  1. /// <summary>  
  2.         /// 检测是否可以向下了  
  3.         /// </summary>  
  4.         /// <returns></returns>  
  5.         private bool CheckIsDown()  
  6.         {  
  7.             for (int y = 0; y < 4; y++)  
  8.             {  
  9.                 for (int x = 0; x < 4; x++)  
  10.                 {  
  11.                     if (CurrentTrick[y, x] == 1)  
  12.                     {  
  13.                         //超过了背景  
  14.                         if (y + CurrentY + 1 >= 20)  
  15.                         {  
  16.                             return false;  
  17.                         }  
  18.                         if (x + CurrentX >= 14)  
  19.                         {  
  20.                             CurrentX = 13 - x;  
  21.                         }  
  22.                         if (bgGraoud[y + CurrentY + 1, x + CurrentX] == 1)  
  23.                         {  
  24.                             return false;  
  25.                         }  
  26.                     }  
  27.                 }  
  28.             }  
  29.             return true;  
  30.         }  
  31.         /// <summary>  
  32.         /// 检测是否可以左移  
  33.         /// </summary>  
  34.         /// <returns></returns>  
  35.         private bool CheckIsLeft()  
  36.         {  
  37.             for (int y = 0; y < 4; y++)  
  38.             {  
  39.                 for (int x = 0; x < 4; x++)  
  40.                 {  
  41.                     if (CurrentTrick[y, x] == 1)  
  42.                     {  
  43.                         if (x + CurrentX - 1 < 0)  
  44.                         {  
  45.                             return false;  
  46.                         }  
  47.                         if (bgGraoud[y + CurrentY, x + CurrentX - 1] == 1)  
  48.                         {  
  49.                             return false;  
  50.                         }  
  51.                     }  
  52.                 }  
  53.             }  
  54.             return true;  
  55.         }  
  56.         /// <summary>  
  57.         /// 检测是否可以右移  
  58.         /// </summary>  
  59.         /// <returns></returns>  
  60.         private bool CheckIsRight()  
  61.         {  
  62.             for (int y = 0; y < 4; y++)  
  63.             {  
  64.                 for (int x = 0; x < 4; x++)  
  65.                 {  
  66.                     if (CurrentTrick[y, x] == 1)  
  67.                     {  
  68.                         if (x + CurrentX + 1 >= 14)  
  69.                         {  
  70.                             return false;  
  71.                         }  
  72.                         if (bgGraoud[y + CurrentY, x + CurrentX+1] == 1)  
  73.                         {  
  74.                             return false;  
  75.                         }  
  76.                     }  
  77.                 }  
  78.             }  
  79.             return true;  
  80.         }  

/// <summary> /// 检测是否可以向下了 /// </summary> /// <returns></returns> private bool CheckIsDown() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { //超过了背景 if (y + CurrentY + 1 >= 20) { return false; } if (x + CurrentX >= 14) { CurrentX = 13 - x; } if (bgGraoud[y + CurrentY + 1, x + CurrentX] == 1) { return false; } } } } return true; } /// <summary> /// 检测是否可以左移 /// </summary> /// <returns></returns> private bool CheckIsLeft() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { if (x + CurrentX - 1 < 0) { return false; } if (bgGraoud[y + CurrentY, x + CurrentX - 1] == 1) { return false; } } } } return true; } /// <summary> /// 检测是否可以右移 /// </summary> /// <returns></returns> private bool CheckIsRight() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { if (x + CurrentX + 1 >= 14) { return false; } if (bgGraoud[y + CurrentY, x + CurrentX+1] == 1) { return false; } } } } return true; }

 

下面是绘制函数:没啥思路,循环画出即可

 

 

[c-sharp] view plain copy print ?
  1. private void Draw()  
  2.         {  
  3.             Graphics g = Graphics.FromImage(myImage);  
  4.             g.Clear(this.BackColor);  
  5.             for (int bgy = 0; bgy < 20; bgy++)  
  6.             {  
  7.                 for (int bgx = 0; bgx < 14; bgx++)  
  8.                 {  
  9.                     if (bgGraoud[bgy, bgx] == 1)  
  10.                     {  
  11.                          
  12.                         g.FillRectangle(new SolidBrush(Color.Blue), bgx * 20, bgy * 20, 20, 20);  
  13.                     }  
  14.                 }  
  15.             }  
  16.             //绘制当前的图片  
  17.             for (int y = 0; y < 4; y++)  
  18.             {  
  19.                 for (int x = 0; x < 4; x++)  
  20.                 {  
  21.                     if (CurrentTrick[y, x] == 1)  
  22.                     {  
  23.                         g.FillRectangle(new SolidBrush(Color.Blue), (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20);  
  24.                     }  
  25.                 }  
  26.             }  
  27.             Graphics gg = panel1.CreateGraphics();  
  28.            
  29.             gg.DrawImage(myImage, 0, 0);  
  30.         }  

private void Draw() { Graphics g = Graphics.FromImage(myImage); g.Clear(this.BackColor); for (int bgy = 0; bgy < 20; bgy++) { for (int bgx = 0; bgx < 14; bgx++) { if (bgGraoud[bgy, bgx] == 1) { g.FillRectangle(new SolidBrush(Color.Blue), bgx * 20, bgy * 20, 20, 20); } } } //绘制当前的图片 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { g.FillRectangle(new SolidBrush(Color.Blue), (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20); } } } Graphics gg = panel1.CreateGraphics(); gg.DrawImage(myImage, 0, 0); }

 

下面的一个函数是比较重要的。向下运动函数。因为基本上向下运动的函数决定了下落的位置:基本思路其实很简单的。先检测是否可以向下下落,如果可以下落就下落了,如果不可以下落,那说明到底了。首先检测当前的坐标是不是为0,如果是,那么说明游戏结束了。否则。检测下。是否有满行的。如果有的,行平移然后并且可以增加积分,这一切完成以后,然后开始下个方块。

[c-sharp] view plain copy print ?
  1. /// <summary>  
  2.         /// 下落方块  
  3.         /// </summary>  
  4.         private void DownTricks()  
  5.         {  
  6.             if (CheckIsDown())  
  7.             {  
  8.                 CurrentY++;  
  9.             }  
  10.             else  
  11.             {  
  12.                 if (CurrentY == 0)  
  13.                 {  
  14.                     timer1.Stop();  
  15.                     MessageBox.Show("哈哈,你玩玩了");  
  16.                       
  17.                     return;  
  18.                 }  
  19.                 //下落完成,修改背景  
  20.                 for (int y = 0; y < 4; y++)  
  21.                 {  
  22.                     for (int x = 0; x < 4; x++)  
  23.                     {  
  24.                         if (CurrentTrick[y, x] == 1)  
  25.                         {  
  26.                             bgGraoud[CurrentY + y, CurrentX + x] = CurrentTrick[y, x];  
  27.                         }  
  28.                     }  
  29.                 }  
  30.                 CheckSore();  
  31.                 BeginTricks();  
  32.                   
  33.             }  
  34.             Draw();  
  35.         }  

/// <summary> /// 下落方块 /// </summary> private void DownTricks() { if (CheckIsDown()) { CurrentY++; } else { if (CurrentY == 0) { timer1.Stop(); MessageBox.Show("哈哈,你玩玩了"); return; } //下落完成,修改背景 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { bgGraoud[CurrentY + y, CurrentX + x] = CurrentTrick[y, x]; } } } CheckSore(); BeginTricks(); } Draw(); }

 

好像忘记了如何开始向下落的哦。看代码:太容易了,不解释了。

[c-sharp] view plain copy print ?
  1. private void BeginTricks()  
  2.         {  
  3.             //随机生成砖码和状态码  
  4.             int i = rand.Next(0, TricksNum);  
  5.             int j = rand.Next(0, StatusNum);  
  6.             CurrentTrickNum = i;  
  7.             CurrentStatusNum = j;  
  8.             //分配数组  
  9.             for (int y = 0; y < 4; y++)  
  10.             {  
  11.                 for (int x = 0; x < 4; x++)  
  12.                 {  
  13.                     CurrentTrick[y,x] = Tricks[i,j,y,x];  
  14.                 }  
  15.             }  
  16.             CurrentX = 0;  
  17.             CurrentY = 0;  
  18.             timer1.Start();  
  19.         }  

private void BeginTricks() { //随机生成砖码和状态码 int i = rand.Next(0, TricksNum); int j = rand.Next(0, StatusNum); CurrentTrickNum = i; CurrentStatusNum = j; //分配数组 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y,x] = Tricks[i,j,y,x]; } } CurrentX = 0; CurrentY = 0; timer1.Start(); }

 

 

下面我们看看是如何检测方块的背景中是否有满行的,如果满行了把他上面的平移下去。这个是很多初学者感觉到难以解决的地方,其实就是循环。这个循环还有得优化,但是我想想还是把他放在下一个篇的优化篇上讲解吧。看代码,我们说思路。其实思路很简单的,循环看看是否有满行的,如果存在一个为0,说明没满行,继续下个循环,如果没有0,则说明满行了,增加分数,然后比这个y小的矩阵向下平移,当前的坐标=(x,y-1)即可。然后继续循环本行(因为本很是上一行又下来的)。

 

[c-sharp] view plain copy print ?
  1. private void CheckSore()  
  2.         {  
  3.             for (int y = 19; y > -1; y--)  
  4.             {  
  5.                 bool isFull = true;  
  6.                 for (int x = 13; x > -1; x--)  
  7.                 {  
  8.                     if (bgGraoud[y, x] == 0)  
  9.                     {  
  10.                         isFull = false;  
  11.                         break;  
  12.                     }  
  13.                 }  
  14.                 if (isFull)  
  15.                 {  
  16.                     //增加积分  
  17.                     Sorce = Sorce + 100;  
  18.                     for (int yy = y; yy > 0; yy--)  
  19.                     {  
  20.                         for (int xx = 0; xx < 14; xx++)  
  21.                         {  
  22.                             int temp = bgGraoud[yy - 1, xx];  
  23.                             bgGraoud[yy, xx] = temp;  
  24.                         }  
  25.                     }  
  26.                     y++;  
  27.                     label1.Text = Sorce.ToString(); ;  
  28.                     Draw();  
  29.                 }  
  30.                  
  31.             }  
  32.         }  

private void CheckSore() { for (int y = 19; y > -1; y--) { bool isFull = true; for (int x = 13; x > -1; x--) { if (bgGraoud[y, x] == 0) { isFull = false; break; } } if (isFull) { //增加积分 Sorce = Sorce + 100; for (int yy = y; yy > 0; yy--) { for (int xx = 0; xx < 14; xx++) { int temp = bgGraoud[yy - 1, xx]; bgGraoud[yy, xx] = temp; } } y++; label1.Text = Sorce.ToString(); ; Draw(); } } }

 

还有几个函数大家都可以看懂的,我就不讲解了。我把所有的代码都贴一遍。另外把所有的图片和源文件打包后上传到csdn,请大家下载。

代码下载地址:http://download.youkuaiyun.com/source/2057359

 

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace Tetris  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行  
  19.         private int[, , ,] Tricks = {{  
  20.                                       {  
  21.                                           {1,0,0,0},  
  22.                                           {1,0,0,0},  
  23.                                           {1,0,0,0},  
  24.                                           {1,0,0,0}  
  25.                                       },  
  26.                                       {  
  27.                                           {1,1,1,1},  
  28.                                           {0,0,0,0},  
  29.                                           {0,0,0,0},  
  30.                                           {0,0,0,0}  
  31.                                       },  
  32.                                       {  
  33.                                           {1,0,0,0},  
  34.                                           {1,0,0,0},  
  35.                                           {1,0,0,0},  
  36.                                           {1,0,0,0}  
  37.                                       },  
  38.                                       {  
  39.                                           {1,1,1,1},  
  40.                                           {0,0,0,0},  
  41.                                           {0,0,0,0},  
  42.                                           {0,0,0,0}  
  43.                                       }  
  44.                                   },  
  45.                                   {  
  46.                                        {  
  47.                                            {1,1,0,0},  
  48.                                            {1,1,0,0},  
  49.                                            {0,0,0,0},  
  50.                                            {0,0,0,0}  
  51.                                        },  
  52.                                        {  
  53.                                            {1,1,0,0},  
  54.                                            {1,1,0,0},  
  55.                                            {0,0,0,0},  
  56.                                            {0,0,0,0}  
  57.                                        },  
  58.                                        {  
  59.                                            {1,1,0,0},  
  60.                                            {1,1,0,0},  
  61.                                            {0,0,0,0},  
  62.                                            {0,0,0,0}  
  63.                                        },  
  64.                                        {  
  65.                                            {1,1,0,0},  
  66.                                            {1,1,0,0},  
  67.                                            {0,0,0,0},  
  68.                                            {0,0,0,0}  
  69.                                        }  
  70.                                    },  
  71.                                    {  
  72.                                        {  
  73.                                            {1,0,0,0},  
  74.                                            {1,1,0,0},  
  75.                                            {0,1,0,0},  
  76.                                            {0,0,0,0}  
  77.                                        },  
  78.                                        {  
  79.                                            {0,1,1,0},  
  80.                                            {1,1,0,0},  
  81.                                            {0,0,0,0},  
  82.                                            {0,0,0,0}  
  83.                                        },  
  84.                                        {  
  85.                                            {1,0,0,0},  
  86.                                            {1,1,0,0},  
  87.                                            {0,1,0,0},  
  88.                                            {0,0,0,0}  
  89.                                        },  
  90.                                        {  
  91.                                            {0,1,1,0},  
  92.                                            {1,1,0,0},  
  93.                                            {0,0,0,0},  
  94.                                            {0,0,0,0}  
  95.                                        }  
  96.                                    },  
  97.                                    {  
  98.                                        {  
  99.                                            {1,1,0,0},  
  100.                                            {0,1,0,0},  
  101.                                            {0,1,0,0},  
  102.                                            {0,0,0,0}  
  103.                                        },  
  104.                                        {  
  105.                                            {0,0,1,0},  
  106.                                            {1,1,1,0},  
  107.                                            {0,0,0,0},  
  108.                                            {0,0,0,0}  
  109.                                        },  
  110.                                        {  
  111.                                            {1,0,0,0},  
  112.                                            {1,0,0,0},  
  113.                                            {1,1,0,0},  
  114.                                            {0,0,0,0}  
  115.                                        },  
  116.                                        {  
  117.                                            {1,1,1,0},  
  118.                                            {1,0,0,0},  
  119.                                            {0,0,0,0},  
  120.                                            {0,0,0,0}  
  121.                                        }  
  122.                                    }};  
  123.  
  124.         #endregion  
  125.         #region 定义背景  
  126.         private int[,] bgGraoud ={  
  127.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  128.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  129.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  130.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  131.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  132.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  133.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  134.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  135.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  136.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  137.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  138.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  139.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  140.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  141.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  142.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  143.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  144.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  145.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
  146.                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0}  
  147.                                  };  
  148.         #endregion  
  149.         private int[,] CurrentTrick = new int[4, 4]; //当前的砖块  
  150.         //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态, CurrentX当前x, CurrentY当前y, Sorce分数  
  151.         private int CurrentTrickNum, CurrentStatusNum, CurrentX, CurrentY, Sorce;  
  152.         private int TricksNum = 4;  
  153.         private int StatusNum = 4;  
  154.         private Image myImage;  
  155.         private Random rand = new Random();  
  156.         private void Form1_Load(object sender, EventArgs e)  
  157.         {  
  158.             //初始化  
  159.             myImage = new Bitmap(panel1.Width, panel1.Height);  
  160.             Sorce = 0;  
  161.         }  
  162.         protected override void OnPaint(PaintEventArgs e)  
  163.         {  
  164.             Draw();  
  165.             base.OnPaint(e);  
  166.         }  
  167.           
  168.         private void button1_Click(object sender, EventArgs e)  
  169.         {  
  170.             for (int y = 0; y < 20; y++)  
  171.             {  
  172.                 for (int x = 0; x < 14; x++)  
  173.                 {  
  174.                     bgGraoud[y, x] = 0;  
  175.                 }  
  176.             }  
  177.             timer1.Interval = 1000;  
  178.             BeginTricks();  
  179.             this.Focus();  
  180.         }  
  181.         /// <summary>  
  182.         /// 随机生成方块和状态  
  183.         /// </summary>  
  184.         private void BeginTricks()  
  185.         {  
  186.             //随机生成砖码和状态码  
  187.             int i = rand.Next(0, TricksNum);  
  188.             int j = rand.Next(0, StatusNum);  
  189.             CurrentTrickNum = i;  
  190.             CurrentStatusNum = j;  
  191.             //分配数组  
  192.             for (int y = 0; y < 4; y++)  
  193.             {  
  194.                 for (int x = 0; x < 4; x++)  
  195.                 {  
  196.                     CurrentTrick[y,x] = Tricks[i,j,y,x];  
  197.                 }  
  198.             }  
  199.             CurrentX = 0;  
  200.             CurrentY = 0;  
  201.             timer1.Start();  
  202.         }  
  203.         /// <summary>  
  204.         /// 变化方块  
  205.         /// </summary>  
  206.         private void ChangeTricks()  
  207.         {  
  208.             if (CurrentStatusNum < 3)  
  209.             {  
  210.                 CurrentStatusNum++;  
  211.             }  
  212.             else  
  213.             {  
  214.                 CurrentStatusNum = 0;  
  215.             }  
  216.             for (int y = 0; y < 4; y++)  
  217.             {  
  218.                 for (int x = 0; x < 4; x++)  
  219.                 {  
  220.                     CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];  
  221.                 }  
  222.             }  
  223.         }  
  224.         /// <summary>  
  225.         /// 下落方块  
  226.         /// </summary>  
  227.         private void DownTricks()  
  228.         {  
  229.             if (CheckIsDown())  
  230.             {  
  231.                 CurrentY++;  
  232.             }  
  233.             else  
  234.             {  
  235.                 if (CurrentY == 0)  
  236.                 {  
  237.                     timer1.Stop();  
  238.                     MessageBox.Show("哈哈,你玩玩了");  
  239.                       
  240.                     return;  
  241.                 }  
  242.                 //下落完成,修改背景  
  243.                 for (int y = 0; y < 4; y++)  
  244.                 {  
  245.                     for (int x = 0; x < 4; x++)  
  246.                     {  
  247.                         if (CurrentTrick[y, x] == 1)  
  248.                         {  
  249.                             bgGraoud[CurrentY + y, CurrentX + x] = CurrentTrick[y, x];  
  250.                         }  
  251.                     }  
  252.                 }  
  253.                 CheckSore();  
  254.                 BeginTricks();  
  255.                   
  256.             }  
  257.             Draw();  
  258.         }  
  259.         /// <summary>  
  260.         /// 检测是否可以向下了  
  261.         /// </summary>  
  262.         /// <returns></returns>  
  263.         private bool CheckIsDown()  
  264.         {  
  265.             for (int y = 0; y < 4; y++)  
  266.             {  
  267.                 for (int x = 0; x < 4; x++)  
  268.                 {  
  269.                     if (CurrentTrick[y, x] == 1)  
  270.                     {  
  271.                         //超过了背景  
  272.                         if (y + CurrentY + 1 >= 20)  
  273.                         {  
  274.                             return false;  
  275.                         }  
  276.                         if (x + CurrentX >= 14)  
  277.                         {  
  278.                             CurrentX = 13 - x;  
  279.                         }  
  280.                         if (bgGraoud[y + CurrentY + 1, x + CurrentX] == 1)  
  281.                         {  
  282.                             return false;  
  283.                         }  
  284.                     }  
  285.                 }  
  286.             }  
  287.             return true;  
  288.         }  
  289.         /// <summary>  
  290.         /// 检测是否可以左移  
  291.         /// </summary>  
  292.         /// <returns></returns>  
  293.         private bool CheckIsLeft()  
  294.         {  
  295.             for (int y = 0; y < 4; y++)  
  296.             {  
  297.                 for (int x = 0; x < 4; x++)  
  298.                 {  
  299.                     if (CurrentTrick[y, x] == 1)  
  300.                     {  
  301.                         if (x + CurrentX - 1 < 0)  
  302.                         {  
  303.                             return false;  
  304.                         }  
  305.                         if (bgGraoud[y + CurrentY, x + CurrentX - 1] == 1)  
  306.                         {  
  307.                             return false;  
  308.                         }  
  309.                     }  
  310.                 }  
  311.             }  
  312.             return true;  
  313.         }  
  314.         /// <summary>  
  315.         /// 检测是否可以右移  
  316.         /// </summary>  
  317.         /// <returns></returns>  
  318.         private bool CheckIsRight()  
  319.         {  
  320.             for (int y = 0; y < 4; y++)  
  321.             {  
  322.                 for (int x = 0; x < 4; x++)  
  323.                 {  
  324.                     if (CurrentTrick[y, x] == 1)  
  325.                     {  
  326.                         if (x + CurrentX + 1 >= 14)  
  327.                         {  
  328.                             return false;  
  329.                         }  
  330.                         if (bgGraoud[y + CurrentY, x + CurrentX+1] == 1)  
  331.                         {  
  332.                             return false;  
  333.                         }  
  334.                     }  
  335.                 }  
  336.             }  
  337.             return true;  
  338.         }  
  339.   
  340.         private void Draw()  
  341.         {  
  342.             Graphics g = Graphics.FromImage(myImage);  
  343.             g.Clear(this.BackColor);  
  344.             for (int bgy = 0; bgy < 20; bgy++)  
  345.             {  
  346.                 for (int bgx = 0; bgx < 14; bgx++)  
  347.                 {  
  348.                     if (bgGraoud[bgy, bgx] == 1)  
  349.                     {  
  350.                          
  351.                         g.FillRectangle(new SolidBrush(Color.Blue), bgx * 20, bgy * 20, 20, 20);  
  352.                     }  
  353.                 }  
  354.             }  
  355.             //绘制当前的图片  
  356.             for (int y = 0; y < 4; y++)  
  357.             {  
  358.                 for (int x = 0; x < 4; x++)  
  359.                 {  
  360.                     if (CurrentTrick[y, x] == 1)  
  361.                     {  
  362.                         g.FillRectangle(new SolidBrush(Color.Blue), (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20);  
  363.                     }  
  364.                 }  
  365.             }  
  366.             Graphics gg = panel1.CreateGraphics();  
  367.            
  368.             gg.DrawImage(myImage, 0, 0);  
  369.         }  
  370.   
  371.   
  372.         private void timer1_Tick(object sender, EventArgs e)  
  373.         {  
  374.             DownTricks();  
  375.         }  
  376.   
  377.         private void Form1_KeyDown(object sender, KeyEventArgs e)  
  378.         {  
  379.             if (e.KeyCode == Keys.W)  
  380.             {  
  381.                 ChangeTricks();  
  382.                 Draw();  
  383.             }  
  384.             else if (e.KeyCode == Keys.A)  
  385.             {  
  386.                 if (CheckIsLeft())  
  387.                 {  
  388.                     CurrentX--;  
  389.                 }  
  390.                 Draw();  
  391.             }  
  392.             else if (e.KeyCode == Keys.D)  
  393.             {  
  394.                 if (CheckIsRight())  
  395.                 {  
  396.                     CurrentX++;  
  397.                 }  
  398.                 Draw();  
  399.             }  
  400.             else if (e.KeyCode == Keys.S)  
  401.             {  
  402.                 timer1.Stop();  
  403.                 timer1.Interval = 10;  
  404.                 timer1.Start();  
  405.             }  
  406.         }  
  407.   
  408.         private void Form1_KeyUp(object sender, KeyEventArgs e)  
  409.         {  
  410.             if (e.KeyCode == Keys.S)  
  411.             {  
  412.                 timer1.Stop();  
  413.                 timer1.Interval = 1000;  
  414.                 timer1.Start();  
  415.             }  
  416.         }  
  417.   
  418.         private void CheckSore()  
  419.         {  
  420.             for (int y = 19; y > -1; y--)  
  421.             {  
  422.                 bool isFull = true;  
  423.                 for (int x = 13; x > -1; x--)  
  424.                 {  
  425.                     if (bgGraoud[y, x] == 0)  
  426.                     {  
  427.                         isFull = false;  
  428.                         break;  
  429.                     }  
  430.                 }  
  431.                 if (isFull)  
  432.                 {  
  433.                     //增加积分  
  434.                     Sorce = Sorce + 100;  
  435.                     for (int yy = y; yy > 0; yy--)  
  436.                     {  
  437.                         for (int xx = 0; xx < 14; xx++)  
  438.                         {  
  439.                             int temp = bgGraoud[yy - 1, xx];  
  440.                             bgGraoud[yy, xx] = temp;  
  441.                         }  
  442.                     }  
  443.                     y++;  
  444.                     label1.Text = Sorce.ToString(); ;  
  445.                     Draw();  
  446.                 }  
  447.                  
  448.             }  
  449.         }  
  450.   
  451.         private void button2_Click(object sender, EventArgs e)  
  452.         {  
  453.             if (button2.Text == "暂停游戏")  
  454.             {  
  455.                 button2.Text = "开始游戏";  
  456.                 timer1.Stop();  
  457.             }  
  458.             else  
  459.             {  
  460.                 button2.Text = "暂停游戏";  
  461.                 timer1.Start();  
  462.             }  
  463.         }  
  464.     }  
  465. }  

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Tetris { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 private int[, , ,] Tricks = {{ { {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0} }, { {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}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} }, { {1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {1,1,0,0}, {0,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,1,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, { {1,0,0,0}, {1,0,0,0}, {1,1,0,0}, {0,0,0,0} }, { {1,1,1,0}, {1,0,0,0}, {0,0,0,0}, {0,0,0,0} } }}; #endregion #region 定义背景 private int[,] bgGraoud ={ {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; #endregion private int[,] CurrentTrick = new int[4, 4]; //当前的砖块 //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态, CurrentX当前x, CurrentY当前y, Sorce分数 private int CurrentTrickNum, CurrentStatusNum, CurrentX, CurrentY, Sorce; private int TricksNum = 4; private int StatusNum = 4; private Image myImage; private Random rand = new Random(); private void Form1_Load(object sender, EventArgs e) { //初始化 myImage = new Bitmap(panel1.Width, panel1.Height); Sorce = 0; } protected override void OnPaint(PaintEventArgs e) { Draw(); base.OnPaint(e); } private void button1_Click(object sender, EventArgs e) { for (int y = 0; y < 20; y++) { for (int x = 0; x < 14; x++) { bgGraoud[y, x] = 0; } } timer1.Interval = 1000; BeginTricks(); this.Focus(); } /// <summary> /// 随机生成方块和状态 /// </summary> private void BeginTricks() { //随机生成砖码和状态码 int i = rand.Next(0, TricksNum); int j = rand.Next(0, StatusNum); CurrentTrickNum = i; CurrentStatusNum = j; //分配数组 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y,x] = Tricks[i,j,y,x]; } } CurrentX = 0; CurrentY = 0; timer1.Start(); } /// <summary> /// 变化方块 /// </summary> private void ChangeTricks() { if (CurrentStatusNum < 3) { CurrentStatusNum++; } else { CurrentStatusNum = 0; } for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x]; } } } /// <summary> /// 下落方块 /// </summary> private void DownTricks() { if (CheckIsDown()) { CurrentY++; } else { if (CurrentY == 0) { timer1.Stop(); MessageBox.Show("哈哈,你玩玩了"); return; } //下落完成,修改背景 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { bgGraoud[CurrentY + y, CurrentX + x] = CurrentTrick[y, x]; } } } CheckSore(); BeginTricks(); } Draw(); } /// <summary> /// 检测是否可以向下了 /// </summary> /// <returns></returns> private bool CheckIsDown() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { //超过了背景 if (y + CurrentY + 1 >= 20) { return false; } if (x + CurrentX >= 14) { CurrentX = 13 - x; } if (bgGraoud[y + CurrentY + 1, x + CurrentX] == 1) { return false; } } } } return true; } /// <summary> /// 检测是否可以左移 /// </summary> /// <returns></returns> private bool CheckIsLeft() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { if (x + CurrentX - 1 < 0) { return false; } if (bgGraoud[y + CurrentY, x + CurrentX - 1] == 1) { return false; } } } } return true; } /// <summary> /// 检测是否可以右移 /// </summary> /// <returns></returns> private bool CheckIsRight() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { if (x + CurrentX + 1 >= 14) { return false; } if (bgGraoud[y + CurrentY, x + CurrentX+1] == 1) { return false; } } } } return true; } private void Draw() { Graphics g = Graphics.FromImage(myImage); g.Clear(this.BackColor); for (int bgy = 0; bgy < 20; bgy++) { for (int bgx = 0; bgx < 14; bgx++) { if (bgGraoud[bgy, bgx] == 1) { g.FillRectangle(new SolidBrush(Color.Blue), bgx * 20, bgy * 20, 20, 20); } } } //绘制当前的图片 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (CurrentTrick[y, x] == 1) { g.FillRectangle(new SolidBrush(Color.Blue), (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20); } } } Graphics gg = panel1.CreateGraphics(); gg.DrawImage(myImage, 0, 0); } private void timer1_Tick(object sender, EventArgs e) { DownTricks(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.W) { ChangeTricks(); Draw(); } else if (e.KeyCode == Keys.A) { if (CheckIsLeft()) { CurrentX--; } Draw(); } else if (e.KeyCode == Keys.D) { if (CheckIsRight()) { CurrentX++; } Draw(); } else if (e.KeyCode == Keys.S) { timer1.Stop(); timer1.Interval = 10; timer1.Start(); } } private void Form1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.S) { timer1.Stop(); timer1.Interval = 1000; timer1.Start(); } } private void CheckSore() { for (int y = 19; y > -1; y--) { bool isFull = true; for (int x = 13; x > -1; x--) { if (bgGraoud[y, x] == 0) { isFull = false; break; } } if (isFull) { //增加积分 Sorce = Sorce + 100; for (int yy = y; yy > 0; yy--) { for (int xx = 0; xx < 14; xx++) { int temp = bgGraoud[yy - 1, xx]; bgGraoud[yy, xx] = temp; } } y++; label1.Text = Sorce.ToString(); ; Draw(); } } } private void button2_Click(object sender, EventArgs e) { if (button2.Text == "暂停游戏") { button2.Text = "开始游戏"; timer1.Stop(); } else { button2.Text = "暂停游戏"; timer1.Start(); } } } }

 

 本文章来自:http://blog.youkuaiyun.com/aofengdaxia/article/details/5301934

在接下来的内容中,我们把这个DEMO做成一个三维的俄罗斯方块。

详情请打开:http://blog.youkuaiyun.com/aofengdaxia/archive/2010/02/18/5310742.aspx

转载于:https://www.cnblogs.com/xiaorongsheng/archive/2012/10/27/eluosi.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值