Example #1
0
        public void Start()//游戏开始
        {
            BlockGroup bGroup = new BlockGroup();

            runBlock      = bGroup.GetABlock();
            runBlock.XPos = _width / 2;
            runBlock.YPos = 2;
            runBlock.Paint(gpPalette);


            for (int i = 0; i < runBlock.Length; i++)//寻找Y的最大值
            {
                if (runBlock[i].Y > runBlock.YPos)
                {
                    runBlock.YPos = runBlock[i].Y;
                }
            }

            gpPalette.Clear(disapperColor);       //清空画板
            runBlock.Paint(gpPalette);            //画运行砖块
            Thread.Sleep(20);
            readyBlock      = bGroup.GetABlock(); //去另一个砖块赋给readyBlock
            readyBlock.XPos = 4;                  //5*5的矩阵中心点为2
            readyBlock.YPos = 3;
            gpReady.Clear(disapperColor);         //清空画板
            readyBlock.Paint(gpReady);

            timerBlock           = new System.Timers.Timer(timeSpan);
            timerBlock.Elapsed  += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            timerBlock.AutoReset = true;
            timerBlock.Start();
        }
Example #2
0
        public void CheckAndOverBlock() //检查砖块是否到底,如果到底的话则把它当做固定的砖块coorarr,产生新的砖块
        {
            bool over = false;          //设置一个当前运行砖块是否到底的标志

            for (int i = 0; i < runBlock.Length; i++)
            {
                int x = runBlock.XPos + runBlock[i].X;
                int y = runBlock.YPos - runBlock[i].Y;
                if (y == _heihgt - 1)//如果到底,则当前砖块结束
                {
                    over = true;
                    break;
                }
                if (!coorArr[x, y + 1].IsEmpty)//如果下面有其它砖块,则当前砖块结束
                {
                    over = true;
                    break;
                }
            }
            if (over)
            {
                for (int i = 0; i < runBlock.Length; i++)//把当前砖块归入coordinateArr
                {
                    coorArr[runBlock.XPos + runBlock[i].X, runBlock.YPos - runBlock[i].Y] = runBlock.BlockColor;
                }
                //检查是否有满行的情况,如果有,则去掉满行
                CheckAndDelFullRow();
                //产生新的砖块
                runBlock      = readyBlock; //新的砖块为准备好的砖块
                runBlock.XPos = _width / 2; //确定当前运行砖块的出生位置
                int y = 0;
                for (int i = 0; i < runBlock.Length; i++)
                {
                    if (runBlock[i].Y > y)
                    {
                        y = runBlock[i].Y;
                    }
                }
                runBlock.YPos = y;
                //检查新产生的砖块所占用的地方是否已经有砖块,如果有,则游戏结束
                for (int i = 1; i < runBlock.Length; i++)
                {
                    if (!coorArr[runBlock.XPos + runBlock[i].X, runBlock.YPos - runBlock[i].Y].IsEmpty)
                    {
                        //游戏结束
                        StringFormat drawFormat = new StringFormat();
                        drawFormat.Alignment = StringAlignment.Center;
                        gpPalette.DrawString("GAME OVER", new Font("Arial Black", 25f), new SolidBrush(Color.White), new RectangleF(0, _heihgt * recPix / 2 - 100, _width * recPix, 100), drawFormat);
                        timerBlock.Stop();//关闭定时器
                        return;
                    }
                }
                runBlock.Paint(gpPalette);
                //获取新的准备砖块
                BlockGroup bGroup = new BlockGroup();
                readyBlock      = bGroup.GetABlock();
                readyBlock.XPos = 2;
                readyBlock.YPos = 2;
                gpReady.Clear(Color.Black);
                readyBlock.Paint(gpReady);
            }
        }