Пример #1
0
        /// <summary>
        /// 更新tetris
        /// </summary>
        /// <param name="element"></param>
        private void updateTetris(TetrisElement element)
        {
            Point location = element.location;

            Point[] content = element.content;
            int     minX    = element.getMinX(element.style);
            int     maxX    = element.getMaxX(element.style);
            int     minY    = element.getMinY(element.style);
            int     maxY    = element.getMaxY(element.style);

            foreach (Point p in content)
            {
                if (location.Y + p.Y < 20 && location.Y + p.Y >= 0 && location.X + p.X >= 0 && location.X + p.X < 10)
                {
                    this.tetris[location.X + p.X, location.Y + p.Y] = 1;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 静态函数,生成Tetris元素对象
        /// </summary>
        /// <returns></returns>
        public static TetrisElement generate()
        {
            Random r = new Random(0);
            //随机生成形状
            int tstyle = getRandom();

            tstyle = tstyle % 7;
            TetrisStyle style = TetrisStyle.I;

            style = (TetrisStyle)Enum.Parse(typeof(TetrisStyle), tstyle.ToString());
            //随机生成起始坐标
            int x = getRandom();

            x = x % 10;
            int y = 0;
            //根据形状生成位置信息
            TetrisElement element = new TetrisElement(style);

            //内容由四个点组成,顺序:先上后下,先左后右
            Point[] content = element.getContent(style);
            //获取最小坐标和最大坐标,防止越界
            int minX = element.getMinX(style);
            int minY = element.getMinY(style);
            int maxX = element.getMaxX(style);
            int maxY = element.getMaxY(style);

            //修正起始坐标
            x = (x <= minX) ? minX : x;
            x = (x >= maxX) ? maxX : x;
            y = minY;
            Point location = new Point(x, y);

            element.location = location;
            element.content  = content;
            return(element);
        }
Пример #3
0
        /// <summary>
        /// 判断是否可以向右移动
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        private bool checkRight(TetrisElement element)
        {
            Point location = element.location;

            Point[] content = element.content;
            int     maxY    = element.getMaxY(element.style);
            int     maxX    = element.getMaxX(element.style);
            int     minX    = element.getMinX(element.style);
            int     minY    = element.getMinY(element.style);
            bool    flag    = false;

            if (location.Y >= 0)
            {
                switch (element.style)
                {
                case TetrisStyle.I:
                    if (location.X <= maxX)
                    {
                        //I形状,如果没有到最左边,且左边一排元素为空,则返回true
                        if (location.X + content[0].X + 1 < 10 && this.tetris[location.X + content[0].X + 1, location.Y + content[0].Y] == 0 && this.tetris[location.X + content[1].X + 1, location.Y + content[1].Y] == 0 && this.tetris[location.X + content[2].X + 1, location.Y + content[2].Y] == 0 && this.tetris[location.X + content[3].X + 1, location.Y + content[3].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;

                case TetrisStyle.J:
                    if (location.X <= maxX)
                    {
                        //J形状,如果没有到底,且第1,2,3个元素的左面都为空,则返回true
                        if (location.X + content[0].X + 1 < 10 && this.tetris[location.X + content[0].X + 1, location.Y + content[0].Y] == 0 && this.tetris[location.X + content[1].X + 1, location.Y + content[1].Y] == 0 && this.tetris[location.X + content[2].X + 1, location.Y + content[2].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;

                case TetrisStyle.L:
                    //L形状,如果没有到底,且第1,2,4个元素的左面都为空,则返回true
                    if (location.X <= maxX)
                    {
                        if (location.X + content[0].X + 1 < 10 && this.tetris[location.X + content[0].X + 1, location.Y + content[0].Y] == 0 && this.tetris[location.X + content[1].X + 1, location.Y + content[1].Y] == 0 && this.tetris[location.X + content[3].X + 1, location.Y + content[3].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;

                case TetrisStyle.O:
                case TetrisStyle.Z:
                    //O形状,如果没有到底,且第2,4个元素的左面都为空,则返回true
                    if (location.X <= maxX)
                    {
                        if (location.X + content[1].X + 1 < 10 && this.tetris[location.X + content[1].X + 1, location.Y + content[1].Y] == 0 && this.tetris[location.X + content[3].X + 1, location.Y + content[3].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;

                case TetrisStyle.S:
                    //S形状,如果没有到底,且第1,3个元素的左面都为空,则返回true
                    if (location.X <= maxX)
                    {
                        if (location.X + content[0].X + 1 < 10 && this.tetris[location.X + content[0].X + 1, location.Y + content[0].Y] == 0 && this.tetris[location.X + content[2].X + 1, location.Y + content[2].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;

                case TetrisStyle.T:
                    //T形状,如果没有到底,且第3,4个元素的左面都为空,则返回true
                    if (location.X <= maxX)
                    {
                        if (location.X + content[3].X + 1 < 10 && this.tetris[location.X + content[2].X + 1, location.Y + content[2].Y] == 0 && this.tetris[location.X + content[3].X + 1, location.Y + content[3].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;

                default:
                    if (location.X <= maxX)
                    {
                        //I形状,如果没有到最左边,且左边一排元素为空,则返回true
                        if (location.X + content[0].X + 1 < 10 && this.tetris[location.X + content[0].X + 1, location.Y + content[0].Y] == 0 && this.tetris[location.X + content[1].X + 1, location.Y + content[1].Y] == 0 && this.tetris[location.X + content[2].X + 1, location.Y + content[2].Y] == 0 && this.tetris[location.X + content[3].X + 1, location.Y + content[3].Y] == 0)
                        {
                            flag = true;
                        }
                    }
                    break;
                }
            }
            return(flag);
        }
Пример #4
0
        /// <summary>
        /// 判断默认情况:下方是否有元素
        /// </summary>
        /// <returns></returns>
        private bool checkDefault(TetrisElement element)
        {
            Point location = element.location;

            Point[] content = element.content;
            int     maxY    = element.getMaxY(element.style);
            int     maxX    = element.getMaxX(element.style);
            int     minX    = element.getMinX(element.style);
            int     minY    = element.getMinY(element.style);
            bool    flag    = false;

            switch (element.style)
            {
            case TetrisStyle.I:
                if (location.Y > minY)
                {
                    //I形状,如果没有到底,且最下面一个元素为空,则返回true
                    if (location.Y + content[3].Y + 1 < 20 && this.tetris[location.X, location.Y + content[3].Y + 1] == 0)
                    {
                        flag = true;
                    }
                }
                else
                {
                    flag = true;
                }

                break;

            case TetrisStyle.J:
            case TetrisStyle.L:
            case TetrisStyle.O:
                if (location.Y > minY)
                {
                    //J,L,O形状,如果没有到底,且第3,4个元素的下面都为空,则返回true
                    if (location.Y + content[3].Y + 1 < 20 && this.tetris[location.X + content[2].X, location.Y + content[2].Y + 1] == 0 && this.tetris[location.X + content[3].X, location.Y + content[3].Y + 1] == 0)
                    {
                        flag = true;
                    }
                }
                else
                {
                    flag = true;
                }
                break;

            case TetrisStyle.S:
            case TetrisStyle.T:
            case TetrisStyle.Z:
                if (location.Y > minY)
                {
                    //S,T,Z形状如果没有到底,且第1,3,4个元素的下面都为空,则返回true
                    if (location.Y + content[3].Y + 1 < 20 && this.tetris[location.X + content[0].X, location.Y + content[0].Y + 1] == 0 && this.tetris[location.X + content[2].X, location.Y + content[2].Y + 1] == 0 && this.tetris[location.X + content[3].X, location.Y + content[3].Y + 1] == 0)
                    {
                        flag = true;
                    }
                }
                else
                {
                    flag = true;
                }
                break;

            default:
                if (location.Y > minY)
                {
                    //默认I形状,如果没有到底,且最下面一个元素为空,则返回true
                    if (location.Y + content[3].Y + 1 < 20 && this.tetris[location.X, location.Y + content[3].Y + 1] == 0)
                    {
                        flag = true;
                    }
                }
                else
                {
                    flag = true;
                }
                break;
            }
            return(flag);
        }