`
yang7229693
  • 浏览: 25667 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Hello World:我的第一个游戏 俄罗斯方块(C#)

阅读更多
用了一天编写的程序,第一次接触这个,就当hello world程序,把实现的方法什么的写下,希望各位大侠不要鄙视,刚接触这个。

设计思想:
经典的俄罗斯方块有7种基本的形状,每个形状有4个块,图形的变形可以采取固定其中一个方块,其他3个方块按照这个方块的位置来确定,例如
1
2    -->  3 2 1
3 4       4
游戏的游戏区域设置为方块大小的整数倍,向左向右移动也均是一个方块的宽度的距离移动,这样可以保证不错位。
游戏的消行用位数组,例如定义游戏区域宽度为12个的话,设置一个16进制数 0xFFF,通过判断游戏行数组即可判定该行是否被填充满了。如果一行已满的话,将该行消去,将行上面的各行都下降一行。一个图形下落到底部,也是根据行数组来判定是否可以继续下落。用一个timer来控制游戏进行速度,一次性消去不同多的行得分不同,根据总分来判定游戏等级,根据等级来设置游戏速度。

Square类,用来存放方块的基本属性以及一些方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace MyTetris
{
    class Square //Square类。定义了单个方块的属性及方法
    {
        #region
        public Point Location;  //方块位置
        public Size Size;       //方块大小
        public Color ForeColor; //方块前景色
        public Color BackColor; //方块背景色
        #endregion

        /// <summary>
        /// Square类构造函数
        /// </summary>
        /// <param name="InitialSize"></param>
        /// <param name="InitialBackcolor"></param>
        /// <param name="InitialForecolor"></param>
        public Square(Size InitialSize, Color InitialBackcolor, Color InitialForecolor)
        {
            //参数初始化
            Size = InitialSize;
            BackColor = InitialBackcolor;
            ForeColor = InitialForecolor;
        }

        /// <summary>
        /// 显示方块
        /// </summary>
        /// <param name="WinHandle"></param>
        public void Show(System.IntPtr WinHandle) 
        {
            Graphics GameGraphics;
            GraphicsPath graphPath;
            PathGradientBrush brushSquare;
            Color[] surroundColor;
            Rectangle rectSquare;

            GameGraphics = Graphics.FromHwnd(WinHandle); //获取背景图片的句柄

            //创建一个方块
            graphPath = new GraphicsPath();
            rectSquare = new Rectangle(Location.X, Location.Y, Size.Width, Size.Height);
            graphPath.AddRectangle(rectSquare);

            //用笔刷填充方块
            brushSquare = new PathGradientBrush(graphPath);
            brushSquare.CenterColor = ForeColor;
            surroundColor = new Color[] { BackColor };
            brushSquare.SurroundColors = surroundColor;

            //将方块画出
            GameGraphics.FillPath(brushSquare, graphPath);
        }


        /// <summary>
        /// 将方块消失
        /// </summary>
        /// <param name="WinHandle"></param>
        public void Hide(System.IntPtr WinHandle) 
        {
            Graphics GameGraphics;
            Rectangle rectSquare;

            GameGraphics = Graphics.FromHwnd(WinHandle);

            //用一个背景色的方块取代原来的方块来使方块消失
            rectSquare = new Rectangle(Location.X, Location.Y, Size.Width, Size.Height);
            GameGraphics.FillRectangle(new SolidBrush(GameField.BackColor), rectSquare);
        }
    }
}



Block类,图形的一些属性及方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace MyTetris
{
    class Block
    {
        #region
        //俄罗斯方块的七种基本形状
        public enum BlockTypes
        {
            Undefined = 0,
            O = 1,
            I = 2,
            J = 3,
            L = 4,
            T = 5,
            Z = 6,
            S = 7
        };
        //变形的方位
        public enum RotationDirections
        {
            NORTH = 1,
            EAST = 2,
            SOUTH = 3,
            WEST = 4
        };
        //一个图形的四个块
        public Square square1;
        public Square square2;
        public Square square3;
        public Square square4;

        private const int squareSize = GameField.SquareSize;

        public BlockTypes BlockType;

        //private Color[] backColors ={ Color.Empty, Color.Red, Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.White, Color.Black };
        //private Color[] foreColors ={ Color.Empty, Color.Purple, Color.LightBlue, Color.Yellow, Color.Red, Color.LightGreen, Color.Black, Color.White };
        
        private Color[] backColors ={ Color.Empty, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White };
        private Color[] foreColors ={ Color.Empty, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black };
        //默认方向
        public RotationDirections StatusRotation = RotationDirections.NORTH;
        #endregion


        /// <summary>
        /// Block类构造函数
        /// </summary>
        /// <param name="location"></param>
        /// <param name="newBlockTypes"></param>
        public Block(Point location, BlockTypes newBlockTypes)
        {
            if (newBlockTypes == BlockTypes.Undefined)
            {
                BlockType = (BlockTypes)(new Random().Next(7)) + 1;
            }
            else
            {
                BlockType = newBlockTypes;
            }
            //生成四个块
            square1 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
            square2 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
            square3 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
            square4 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);

            #region
            switch (BlockType)
            {
                 //  1 2
                 //  3 4
                case BlockTypes.O:
                    square1.Location = new Point(location.X, location.Y);
                    square2.Location = new Point(location.X + squareSize, location.Y);
                    square3.Location = new Point(location.X, location.Y + squareSize);
                    square4.Location = new Point(location.X + squareSize, location.Y + squareSize);
                    break;
                // 1
                // 2
                // 3
                // 4
                case BlockTypes.I:
                    square1.Location = new Point(location.X, location.Y);
                    square2.Location = new Point(location.X, location.Y + squareSize);
                    square3.Location = new Point(location.X, location.Y + 2*squareSize);
                    square4.Location = new Point(location.X, location.Y + 3 * squareSize);
                    break;
                //   1
                //   2
                //4  3
                case BlockTypes.J:
                    square1.Location = new Point(location.X + squareSize, location.Y);
                    square2.Location = new Point(location.X + squareSize, location.Y + squareSize);
                    square3.Location = new Point(location.X + squareSize, location.Y + 2 * squareSize);
                    square4.Location = new Point(location.X, location.Y + 2 * squareSize);
                    break;
                // 1
                // 2
                // 3  4
                case BlockTypes.L:
                    square1.Location = new Point(location.X, location.Y);
                    square2.Location = new Point(location.X , location.Y + squareSize);
                    square3.Location = new Point(location.X, location.Y + 2 * squareSize);
                    square4.Location = new Point(location.X + squareSize, location.Y + 2 * squareSize);
                    break;
                // 1  2  3
                //    4
                case BlockTypes.T:
                    square1.Location = new Point(location.X, location.Y);
                    square2.Location = new Point(location.X + squareSize, location.Y);
                    square3.Location = new Point(location.X + 2 * squareSize, location.Y);
                    square4.Location = new Point(location.X + squareSize, location.Y + squareSize);
                    break;
                // 1  2
                //    3  4
                case BlockTypes.Z:
                    square1.Location = new Point(location.X, location.Y);
                    square2.Location = new Point(location.X + squareSize, location.Y);
                    square3.Location = new Point(location.X + squareSize, location.Y + squareSize);
                    square4.Location = new Point(location.X + 2 * squareSize, location.Y + squareSize);
                    break;
                //    1  2
                // 3  4
                case BlockTypes.S:
                    square1.Location = new Point(location.X + squareSize, location.Y);
                    square2.Location = new Point(location.X + 2 * squareSize, location.Y);
                    square3.Location = new Point(location.X, location.Y + squareSize);
                    square4.Location = new Point(location.X + squareSize, location.Y + squareSize);
                    break;
            }
            #endregion
        }

        /// <summary>
        /// 向下移
        /// </summary>
        /// <returns></returns>
        public bool Down()
        {
            //四个块的下一格均没有方块
            if (GameField.IsEmpty(square1.Location.X / squareSize, square1.Location.Y / squareSize + 1) &&
                GameField.IsEmpty(square2.Location.X / squareSize, square2.Location.Y / squareSize + 1) &&
                GameField.IsEmpty(square3.Location.X / squareSize, square3.Location.Y / squareSize + 1) &&
                GameField.IsEmpty(square4.Location.X / squareSize, square4.Location.Y / squareSize + 1))
            {
                Hide(GameField.WinHandle);
                //将四个块均向下移一个块的长度
                square1.Location = new Point(square1.Location.X, square1.Location.Y + squareSize);
                square2.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                square3.Location = new Point(square3.Location.X, square3.Location.Y + squareSize);
                square4.Location = new Point(square4.Location.X, square4.Location.Y + squareSize);

                Show(GameField.WinHandle);
                
                return true;
            }
            else
            {
                //图形停止运动
                GameField.StopSquare(square1, square1.Location.X / squareSize, square1.Location.Y / squareSize);
                GameField.StopSquare(square2, square2.Location.X / squareSize, square2.Location.Y / squareSize);
                GameField.StopSquare(square3, square3.Location.X / squareSize, square3.Location.Y / squareSize);
                GameField.StopSquare(square4, square4.Location.X / squareSize, square4.Location.Y / squareSize);
                
                return false;
            }
        }

        /// <summary>
        /// 向右移
        /// </summary>
        /// <returns></returns>
        public bool Right()
        {
            //四个块的右一格均没有方块
            if (GameField.IsEmpty(square1.Location.X / squareSize + 1, square1.Location.Y / squareSize) &&
                GameField.IsEmpty(square2.Location.X / squareSize + 1, square2.Location.Y / squareSize) &&
                GameField.IsEmpty(square3.Location.X / squareSize + 1, square3.Location.Y / squareSize) &&
                GameField.IsEmpty(square4.Location.X / squareSize + 1, square4.Location.Y / squareSize))
            {
                Hide(GameField.WinHandle);
                //四个块向右移一格
                square1.Location = new Point(square1.Location.X + squareSize, square1.Location.Y);
                square2.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                square3.Location = new Point(square3.Location.X + squareSize, square3.Location.Y);
                square4.Location = new Point(square4.Location.X + squareSize, square4.Location.Y);

                Show(GameField.WinHandle);

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 向左移
        /// </summary>
        /// <returns></returns>
        public bool Left()
        {
            //四个块的左边一格均没有方块
            if (GameField.IsEmpty(square1.Location.X / squareSize - 1, square1.Location.Y / squareSize) &&
                GameField.IsEmpty(square2.Location.X / squareSize - 1, square2.Location.Y / squareSize) &&
                GameField.IsEmpty(square3.Location.X / squareSize - 1, square3.Location.Y / squareSize) &&
                GameField.IsEmpty(square4.Location.X / squareSize - 1, square4.Location.Y / squareSize))
            {
                Hide(GameField.WinHandle);
                //四个方块均向左移一格
                square1.Location = new Point(square1.Location.X - squareSize, square1.Location.Y);
                square2.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                square3.Location = new Point(square3.Location.X - squareSize, square3.Location.Y);
                square4.Location = new Point(square4.Location.X - squareSize, square4.Location.Y);

                Show(GameField.WinHandle);

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 图形变形
        /// </summary>
        public void Rotate()
        {
            //四个块的初试位置
            Point OldPosition1 = square1.Location;
            Point OldPosition2 = square2.Location;
            Point OldPosition3 = square3.Location;
            Point OldPosition4 = square4.Location;
            //初始方向
            RotationDirections OldStatusRotation = StatusRotation;

            Hide(GameField.WinHandle);
            #region
            switch (BlockType)
            {
                //只有一种形状
                case BlockTypes.O:
                    break;
                //有两种形状
                case BlockTypes.I:
                    #region
                    switch (StatusRotation)
                    {
                        //  1      
                        //  2
                        //  3  --> 1 2 3 4
                        //  4
                        case RotationDirections.NORTH:
                            StatusRotation = RotationDirections.EAST;

                            square1.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X + 2 * squareSize, square2.Location.Y);
                            break;

                        //            1
                        //            2
                        // 1 2 3 4--> 3
                        //            4
                        case RotationDirections.EAST:
                            StatusRotation = RotationDirections.NORTH;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square4.Location = new Point(square2.Location.X, square2.Location.Y + 2 * squareSize);
                            break;
                    }
                    break;
                    #endregion
                //有四种形状
                case BlockTypes.J:
                    #region
                    switch (StatusRotation)
                    {
                        //  1     4
                        //  2  -->3 2 1 
                        //4 3
                        case RotationDirections.NORTH:
                            StatusRotation = RotationDirections.EAST;

                            square1.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X - squareSize, square2.Location.Y - squareSize);
                            break;
                        // 4         3 4  
                        // 3 2 1 --> 2
                        //           1
                        case RotationDirections.EAST:
                            StatusRotation = RotationDirections.SOUTH;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square4.Location = new Point(square2.Location.X + squareSize, square2.Location.Y - squareSize);
                            break;
                        //  3 4  
                        //  2   -->1 2 3
                        //  1          4
                        case RotationDirections.SOUTH:
                            StatusRotation = RotationDirections.WEST;

                            square1.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X + squareSize, square2.Location.Y + squareSize);
                            break;
                        //              1
                        //  1 2 3  -->  2
                        //      4     4 3
                        case RotationDirections.WEST:
                            StatusRotation = RotationDirections.NORTH;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square4.Location = new Point(square2.Location.X - squareSize, square2.Location.Y + squareSize);
                            break;
                    }
                    break;
                    #endregion
                //有四种形状
                case BlockTypes.L:
                    #region
                    switch (StatusRotation)
                    {
                        // 1
                        // 2   --> 3 2 1
                        // 3 4     4
                        case RotationDirections.NORTH:
                            StatusRotation = RotationDirections.EAST;

                            square1.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X - squareSize, square2.Location.Y + squareSize);
                            break;
                        //           4 3
                        // 3 2 1 -->   2
                        // 4           1
                        case RotationDirections.EAST:
                            StatusRotation = RotationDirections.SOUTH;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square4.Location = new Point(square2.Location.X - squareSize, square2.Location.Y - squareSize);
                            break;
                        // 4 3         4
                        //   2 --> 1 2 3
                        //   1
                        case RotationDirections.SOUTH:
                            StatusRotation = RotationDirections.WEST;

                            square1.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X + squareSize, square2.Location.Y - squareSize);
                            break;
                        //     4     1
                        // 1 2 3 --> 2
                        //           3 4
                        case RotationDirections.WEST:
                            StatusRotation = RotationDirections.NORTH;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square4.Location = new Point(square2.Location.X + squareSize, square2.Location.Y + squareSize);
                            break;
                    }
                    break;
                    #endregion
                //有四种形状
                case BlockTypes.T:
                    #region
                    switch (StatusRotation)
                    {
                        //             1
                        // 1 2 3 --> 4 2
                        //   4         3
                        case RotationDirections.NORTH:
                            StatusRotation = RotationDirections.EAST;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square4.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            break;
                        //   1      4
                        // 4 2 --> 3 2 1
                        //   3
                        case RotationDirections.EAST:
                            StatusRotation = RotationDirections.SOUTH;

                            square1.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            break;
                        //   4       3
                        // 3 2 1 --> 2 4
                        //           1
                        case RotationDirections.SOUTH:
                            StatusRotation = RotationDirections.WEST;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square4.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            break;
                        //    3
                        //  2 4 --> 1 2 3
                        //    1       4
                        case RotationDirections.WEST:
                            StatusRotation = RotationDirections.NORTH;

                            square1.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X + squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            break;
                    }
                    break;
                    #endregion
                //有两种形状
                case BlockTypes.Z:
                    #region
                    switch (StatusRotation)
                    {
                        //             1
                        // 1 2   --> 3 2
                        //   3 4     4 
                        case RotationDirections.NORTH:
                            StatusRotation = RotationDirections.EAST;

                            square1.Location = new Point(square2.Location.X, square2.Location.Y - squareSize);
                            square3.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square4.Location = new Point(square2.Location.X - squareSize, square2.Location.Y + squareSize);
                            break;
                        //   1
                        // 3 2 --> 1 2
                        // 4         3 4
                        case RotationDirections.EAST:
                            StatusRotation = RotationDirections.NORTH;

                            square1.Location = new Point(square2.Location.X - squareSize, square2.Location.Y);
                            square3.Location = new Point(square2.Location.X, square2.Location.Y + squareSize);
                            square4.Location = new Point(square2.Location.X + squareSize, square2.Location.Y + squareSize);
                            break;
                    }
                    break;
                    #endregion
                //有两种形状
                case BlockTypes.S:
                    #region
                    switch (StatusRotation)
                    {
                        //   1 2     3
                        // 3 4   --> 4 1
                        //             2
                        case RotationDirections.NORTH:
                            StatusRotation = RotationDirections.EAST;

                            square2.Location = new Point(square1.Location.X, square1.Location.Y + squareSize);
                            square3.Location = new Point(square1.Location.X - squareSize, square1.Location.Y - squareSize);
                            square4.Location = new Point(square1.Location.X - squareSize, square1.Location.Y);
                            break;
                        // 3         1 2
                        // 4 1 --> 3 4
                        //   2
                        case RotationDirections.EAST:
                            StatusRotation = RotationDirections.NORTH;

                            square2.Location = new Point(square1.Location.X + squareSize, square1.Location.Y);
                            square3.Location = new Point(square1.Location.X - squareSize, square1.Location.Y + squareSize);
                            square4.Location = new Point(square1.Location.X, square1.Location.Y + squareSize);
                            break;
                    }
                    break;
                    #endregion
            }
            #endregion
            //如果当前位置有方块则还原为初试的位置
            if (!(GameField.IsEmpty(square1.Location.X / squareSize, square1.Location.Y / squareSize) &&
                GameField.IsEmpty(square2.Location.X / squareSize, square2.Location.Y / squareSize) &&
                GameField.IsEmpty(square3.Location.X / squareSize, square3.Location.Y / squareSize) &&
                GameField.IsEmpty(square4.Location.X / squareSize, square4.Location.Y / squareSize)))
            {
                StatusRotation = OldStatusRotation;

                square1.Location = OldPosition1;
                square2.Location = OldPosition2;
                square3.Location = OldPosition3;
                square4.Location = OldPosition4;
            }

            Show(GameField.WinHandle);
        }

        /// <summary>
        /// 显示图形
        /// </summary>
        /// <param name="WinHandle"></param>
        public void Show(System.IntPtr WinHandle)
        {
            square1.Show(WinHandle);
            square2.Show(WinHandle);
            square3.Show(WinHandle);
            square4.Show(WinHandle);
        }

        /// <summary>
        /// 图形消失s
        /// </summary>
        /// <param name="WinHandle"></param>
        public void Hide(System.IntPtr WinHandle)
        {
            square1.Hide(WinHandle);
            square2.Hide(WinHandle);
            square3.Hide(WinHandle);
            square4.Hide(WinHandle);
        }

        /// <summary>
        /// 返回方块1、2、3、4的最小竖坐标
        /// </summary>
        /// <returns></returns>
        public int Top()
        {
            return Math.Min(square1.Location.Y,Math.Min(square2.Location.Y,Math.Min(square3.Location.Y,square4.Location.Y)));
        }



    }
}


GameField类用来存放游戏的一些属性及方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MyTetris
{
    class GameField
    {
        #region
        public static System.IntPtr WinHandle;
        public static Color BackColor;
        public const int SquareSize = 20;

        public const int Width = 12; //宽度,16个square宽
        public const int Height = 20;//长度,29个square长

        private static Square[,] arrGameField = new Square[Width, Height]; //方块数组,存放游戏信息

        public static int[] arrBitGameField = new int[Height]; //位数组,判定某行是否可以被消除

        private const int bitEmpty = 0x0;  //0000 0000 0000
        private const int bitFull = 0xFFF; //1111 1111 1111
        #endregion

        /// <summary>
        /// 判定x,y处是否为空
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static bool IsEmpty(int x, int y)
        {
            //判定是否在游戏区域范围内
            if ((y < 0 || y >= Height) || (x < 0 || x >= Width))
            {
                return false;
            }
            //是否位于边界之上
            else if ((arrBitGameField[y] & (1 << x)) != 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 某行是否被完全
        /// </summary>
        /// <returns></returns>
        public static int CheckLines()
        {
            int CheckLines_result = 0;

            int y = Height - 1;

            while (y >= 0)
            {
                //当为空行时,停止循环
                if (arrBitGameField[y] == bitEmpty)
                {
                    y = 0;
                }
                //当是满行时
                if (arrBitGameField[y] == bitFull)
                {
                    CheckLines_result++;

                    //将满行上面的行往下降
                    for (int index = y; index >= 0; index--)
                    {
                        //如果上面的行数大于1
                        if (index > 0)
                        {
                            arrBitGameField[index] = arrBitGameField[index - 1];

                            for (int x = 0; x < Width; x++)
                            {
                                arrGameField[x, index] = arrGameField[x, index - 1];

                                if (arrGameField[x, index] != null)
                                {
                                    arrGameField[x, index].Location = new Point(arrGameField[x, index].Location.X, arrGameField[x, index].Location.Y + SquareSize);
                                }
                            }
                        }
                        //消除该行
                        else
                        {
                            arrBitGameField[index] = bitEmpty;
                            for (int x = 0; x < Width; x++)
                            {
                                arrGameField[x, index] = null;
                            }
                        }
                    }

                }
                else
                {
                    y--;
                }
            }
            return CheckLines_result;
        }

        /// <summary>
        /// 停止图形下落
        /// </summary>
        /// <param name="square"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void StopSquare(Square square, int x, int y)
        {
            arrBitGameField[y] = arrBitGameField[y] | (1 << x);
            arrGameField[x, y] = square;
        }

        /// <summary>
        /// 重新绘制整个游戏区域
        /// </summary>
        public static void Redraw()
        {
            for (int y = Height - 1; y >= 0; y--)
            {
                if (arrBitGameField[y] != bitEmpty)
                {
                    for (int x = Width - 1; x >= 0; x--)
                    {
                        if (arrGameField[x, y] != null)
                        {
                            arrGameField[x, y].Show(WinHandle);
                        }
                    }
                }
            }
        }

    }
}



timer的tick事件
private void tmrGameClock_Tick(object sender, EventArgs e)
        {
            //被消的行数
            int erasedLines = 0;

            if (stillProcessind)
            {
                return;
            }
            stillProcessind = true;

            if (!CurrentBlock.Down())
            {
                if(CurrentBlock.Top()==0)
                {
                    tmrGameClock.Enabled = true;
                    cmdStart.Enabled = true;

                    //MessageBox.Show("Game Over", ".NETTrix", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    labFailed.Visible = true;
                    stillProcessind = false;
                    tmrGameClock.Enabled = false;
                    return;
                }
                erasedLines = GameField.CheckLines();

                if (erasedLines > 0)
                {
                    //一次消除的行数所得的分数
                    switch (erasedLines)
                    {
                        case 1: score += 10 * erasedLines; break;
                        case 2: score += 15 * erasedLines; break;
                        case 3: score += 20 * erasedLines; break;
                        case 4: score += 25 * erasedLines; break;
                    }
                    labScore.Text = score.ToString();

                    //按照总分来定游戏级别
                    if (score < 200)
                        level = 1;
                    else if (score < 400)
                        level = 2;
                    else if (score < 800)
                        level = 3;
                    else if (score < 1000)
                        level = 4;
                    else if (score < 1200)
                        level = 5;
                    else
                        level = 6;
                    labLevel.Text = level.ToString();

                    //根据游戏级别来设置游戏速度
                    tmrGameClock.Interval = 400 - (level - 1) * 50;

                    picBackground.Invalidate();
                    Application.DoEvents();
                    GameField.Redraw();
                }

                CurrentBlock = new Block(new Point(GameField.SquareSize * 6, 0), NextBlock.BlockType);
                CurrentBlock.Show(picBackground.Handle);

                NextBlock.Hide(picNextBlock.Handle);
                NextBlock = new Block(new Point(20, 10), Block.BlockTypes.Undefined);
                NextBlock.Show(picNextBlock.Handle);

            }
            stillProcessind = false;
        }


附件中是工程文件,vs2005下编译通过
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics