Пример #1
0
        // rotate the block
        public void Rotate(Blocks nowBlock, PictureBox[,] allBlocks)
        {
            if (nowBlock != null && nowBlock.GetBlocksType() != "O")
            {
                Point                center            = ConvertIndexToPoint(nowBlock.GetCenter().Name);
                Point[]              AllBlocksIndex    = nowBlock.GetAllCubesPosition();
                PictureBox[]         AllBlocksPosition = nowBlock.GetAllCubes();
                System.Drawing.Color nowColor          = AllBlocksPosition[0].BackColor;
                Panel                panelOnShow       = tv.GetPanel1();
                if (nowBlock.GetNowState() != States.Stop)
                {
                    for (int i = 0; i < AllBlocksIndex.Length; i++)
                    {
                        int x = AllBlocksIndex[i].Gety() - center.Gety();
                        int y = AllBlocksIndex[i].Getx() - center.Getx();
                        // if result calculated is out of range then throw exception
                        if (center.Getx() - x < 0 || center.Getx() - x > 8 || center.Gety() + y < 0 || center.Gety() + y > 12)
                        {
                            throw new Exception();
                        }
                    }
                    // calculate new position of block rotated, and change the back color of picturebox in origin position
                    for (int i = 0; i < AllBlocksIndex.Length; i++)
                    {
                        int x = AllBlocksIndex[i].Gety() - center.Gety();
                        int y = AllBlocksIndex[i].Getx() - center.Getx();
                        AllBlocksIndex[i].Setx(center.Getx() - x);
                        AllBlocksIndex[i].Sety(center.Gety() + y);
                        AllBlocksPosition[i].BackColor = panelOnShow.BackColor;
                    }
                    // store new index of cubes in picturebox metrix
                    nowBlock.SetAllCubesPosition(AllBlocksIndex);
                    // change the back color of picturebox in new position to block color
                    for (int i = 0; i < AllBlocksPosition.Length; i++)
                    {
                        // if out of range then throw exception
                        if (i < 0 || i >= AllBlocksPosition.Length || i >= AllBlocksIndex.Length)
                        {
                            throw new Exception();
                        }
                        AllBlocksPosition[i]           = allBlocks[AllBlocksIndex[i].Gety(), AllBlocksIndex[i].Getx()];
                        AllBlocksPosition[i].BackColor = nowColor;
                    }

                    //store new position of picture box in picturebox metrix
                    nowBlock.SetAllCubes(AllBlocksPosition);
                    //modify the center of block
                    nowBlock.SetCenter(AllBlocksPosition[1]);
                }
            }
        }
Пример #2
0
 // move the block left
 public void MoveLeft(Blocks nowBlock)
 {
     if (nowBlock != null)
     {
         PictureBox[,] allBlocks = tv.GetAllBlocks();
         Point[]              nowBlockIndex    = nowBlock.GetAllCubesPosition();
         PictureBox[]         nowBlockPosition = nowBlock.GetAllCubes();
         System.Drawing.Color nowColor         = nowBlockPosition[0].BackColor;
         Panel panelOnShow = tv.GetPanel1();
         if (nowBlock.GetNowState() != States.Stop)
         {
             for (int i = 0; i < nowBlockIndex.Length; i++)
             {
                 //avoid to move out of range
                 int x = nowBlockIndex[i].Getx();
                 if (x <= 0)
                 {
                     throw new Exception();
                 }
                 int y = nowBlockIndex[i].Gety();
                 //avoid to touch the existed block
                 if (allBlocks[y, x - 1].BackColor != panelOnShow.BackColor && allBlocks[y, x - 1].BackColor != allBlocks[y, x].BackColor && !nowBlockPosition.Contains <PictureBox>(allBlocks[y, x - 1]))
                 {
                     throw new Exception();
                 }
             }
             //set the color of origin block to background color
             //compute the new position of block
             for (int i = 0; i < nowBlockIndex.Length; i++)
             {
                 nowBlockPosition[i].BackColor = panelOnShow.BackColor;
                 nowBlockIndex[i].Setx(nowBlockIndex[i].Getx() - 1);
             }
             //modify position of block
             for (int i = 0; i < nowBlockPosition.Length; i++)
             {
                 if (nowBlockIndex[i].Gety() < 0 || nowBlockIndex[i].Gety() > 12 || nowBlockIndex[i].Getx() < 0 || nowBlockIndex[i].Getx() > 8)
                 {
                     throw new Exception();
                 }
                 nowBlockPosition[i]           = allBlocks[nowBlockIndex[i].Gety(), nowBlockIndex[i].Getx()];
                 nowBlockPosition[i].BackColor = nowColor;
             }
             // set new info. in every store unit
             nowBlock.SetAllCubes(nowBlockPosition);
             nowBlock.SetCenter(nowBlockPosition[1]);
         }
     }
 }