`
yang7229693
  • 浏览: 26051 次
  • 性别: 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下编译通过
分享到:
评论

相关推荐

    C#入门

    C#第一个程序,helloworld using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace helloworld { class helloworld { [STAThread] static void Main(string[] args)...

    C#入门制作第一个程序hello world的演示flash

    本教程将通过"C#入门制作第一个程序Hello World的演示Flash"来引导你步入C#编程的世界。 首先,"Hello, World!"程序在任何编程语言中都是基础,它的主要作用是输出“Hello, World!”字符串,帮助新手了解如何运行一...

    C#编写17种Hello_World程序

    在这一部分,我们介绍了一个非常基础的C#程序,用于输出“HELLOWORLD”。这里没有使用任何命名空间或者复杂的类结构。 ```csharp public class HelloWorld { public static void Main() { System.Console....

    C#输出Hello World (内有两种方法,很详细)

    在学习任何一种编程语言时,第一个程序往往都是输出“Hello World”,这不仅是因为它简单易懂,更因为它能够帮助初学者快速了解该语言的基本语法结构和运行环境。对于C#这种强大的面向对象编程语言而言,“Hello ...

    helloworld入门实例(c#)

    "程序是每个新手程序员的第一个里程碑,它标志着你开始学习新的编程语言。在这个实例中,我们将深入探讨如何使用C#编写"Hello, World!"程序,以及C#语言的一些基本概念。 C#是一种由微软公司开发的面向对象的编程...

    C# Helloworld

    HelloWorld 是学习任何编程语言时最常见的第一个程序。它不仅简单易懂,而且能够帮助初学者熟悉编程环境的搭建和基本语法。本文将深入分析一个用 C# 编写的 HelloWorld 程序,包括程序结构、命名空间、类与方法的...

    一个简单的qt版helloworld程序

    本压缩包包含的“一个简单的qt版helloworld程序”是初学者接触Qt编程的一个基础示例,旨在帮助理解Qt的基本语法和项目构建过程。 首先,让我们来理解一下Qt中的"Hello, World!"程序的基本结构。在Qt中,我们通常会...

    第一课 C# 简单的HelloWorld程序.rar

    "程序是每个编程语言初学者的第一步,它标志着编程之旅的开始。在这里,我们也将从创建这个简单的C#程序开始,逐步深入到C#与Halcon的集成应用。 C#是由微软开发的一种面向对象的编程语言,广泛应用于Windows应用...

    第一个C#程序Hello World

    因为需要进行比对教学,所以这几天闲暇之余我学习了一下C#语言。环境:Windows XP Professional、.NET Framewokd 2.0、 .NET Frameworkd 2.0 SDK(没有安装VS系列IDE),以及记事本。学习思路:按照Java的规范来书写...

    C#-Helloworld

    综上所述,"C#-Helloworld"是一个引导性的学习主题,涵盖了C#语言的基本用法和编程概念,是初学者踏上C#编程之旅的第一步。通过理解和实践这个简单的例子,可以为进一步深入学习C#打下坚实的基础。

    hello world hello world

    ”是初学者编写的第一个程序,它会在执行时打印出这句话,以此来验证编程环境的正确配置。这个例子可能是表示我们要讨论的是关于编程或者软件开发的基础知识。 标签“hello world”进一步强化了这个主题,可能意味...

    Helloworld_helloworld_

    "程序是一个经典的起点,用于教授新手如何在特定的编程语言中编写并运行他们的第一个程序。这个简单的程序通常只包含一行代码,用于在控制台上打印出 "Hello, World!" 这个字符串。在这个场景中,我们看到的"Hello...

    HelloWorld_打印HelloWorld_

    " 是每个初学者的第一个程序,它标志着编程之旅的开始。这个"HelloWorld_"项目正是这样一个入门实例,用于在Visual Studio这个强大的集成开发环境中展示基本的代码编写和运行流程。 首先,让我们了解什么是Visual ...

    openwrt可用helloworld程序

    1. 创建源代码文件:在OpenWrt源码树的`package`目录下,新建一个目录,例如`hello-world`,并在其中创建`src`子目录和`Makefile`文件。`src`目录用于存放源代码,`Makefile`用于指示OpenWrt如何构建和打包这个程序...

    linux环境下用makefile编译简单的helloworld程序

    在这个“linux环境下用makefile编译简单的helloworld程序”的主题中,我们将深入理解如何创建并使用`Makefile`来编译一个基本的C或C++程序,例如“helloworld”。 首先,`helloworld`程序是一个经典的入门示例,...

    Hello_World.zip_hello world _world

    这个简单的程序是许多编程语言教学的第一课,用于向初学者介绍如何在代码中打印出“Hello, World!”这个字符串,以此来验证编译环境的正确配置和基础语法的理解。 在IT领域,“Hello, World!”程序的重要性不言而喻...

    C#的17种Hello World的写法

    在学习任何编程语言时,编写第一个程序“Hello World”都是一个传统而重要的步骤。对于初学者来说,这不仅是一个简单的实践项目,还可以帮助他们熟悉语言的基本语法和结构。下面我们将详细介绍C#中实现Hello World的...

    我的第一个JSP之helloworld

    【标题】"我的第一个JSP之helloworld"揭示了这个主题是关于初学者入门JSP(JavaServer Pages)编程,并通过一个经典的“Hello, World!”示例进行介绍。JSP是一种动态网页技术,允许开发者在HTML中嵌入Java代码,以...

    Hello World.rar_c++ hello world_world

    " 是每个初学者接触的第一个程序,它标志着编程旅程的开始。本主题将深入探讨如何使用C++语言编写这个经典的 "Hello, World!" 程序,以及C++的基本语法和环境配置。 C++ 是一种强类型、面向对象的编程语言,由...

Global site tag (gtag.js) - Google Analytics