GDI学习之俄罗斯方块续

Posted margin_gu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GDI学习之俄罗斯方块续相关的知识,希望对你有一定的参考价值。

这是百度到的代码,我只是增加了方块颜色,并稍微改了下

技术分享
namespace Tetris
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行
        private int[, , ,] Tricks = {{  
                                      {  
                                          {0,1,0,0},  
                                          {0,1,0,0},  
                                          {0,1,0,0},  
                                          {0,1,0,0}  
                                      },  
                                      {  
                                          {0,0,0,0},   
                                          {0,0,0,0}, 
                                          {1,1,1,1},  
                                          {0,0,0,0}  
                                      },  
                                      {  
                                          {0,0,1,0},  
                                          {0,0,1,0},  
                                          {0,0,1,0},  
                                          {0,0,1,0}  
                                      },  
                                      {  
                                          {0,0,0,0},  
                                          {1,1,1,1},  
                                          {0,0,0,0},  
                                          {0,0,0,0}  
                                      }  
                                  },  
                                  {  
                                       {  
                                           {0,0,0,0},  
                                           {0,1,1,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {0,1,1,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {0,1,1,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {0,1,1,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       }  
                                   },  
                                   {  
                                       {  
                                           {0,1,0,0},  
                                           {0,1,1,0},  
                                           {0,0,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {0,0,1,1},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,1,0,0},  
                                           {0,1,1,0},  
                                           {0,0,1,0},  
                                           {0,0,0,0}   
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {0,0,1,1},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       }  
                                   },  
                                   {  
                                       {  
                                           {0,0,1,0},  
                                           {0,1,1,0},  
                                           {0,1,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {1,1,0,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,1,0},  
                                           {0,1,1,0},  
                                           {0,1,0,0}, 
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {1,1,0,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}   
                                       }  
                                   },  
                                   {  
                                       {  
                                           {0,1,1,0},  
                                           {0,0,1,0},  
                                           {0,0,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,1,0},  
                                           {1,1,1,0},  
                                           {0,0,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,1,0,0},  
                                           {0,1,0,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {1,1,1,0},  
                                           {1,0,0,0},  
                                           {0,0,0,0},  
                                           {0,0,0,0}  
                                       }  
                                   },  
                                   {  
                                       {  
                                           {0,1,1,0},  
                                           {0,1,0,0},  
                                           {0,1,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {1,1,1,0},  
                                           {0,0,1,0},  
                                           {0,0,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,1,0},  
                                           {0,0,1,0},  
                                           {0,1,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {1,0,0,0},  
                                           {1,1,1,0},  
                                           {0,0,0,0},  
                                           {0,0,0,0}  
                                       }  
                                   },  
                                   {  
                                       {  
                                           {0,1,0,0},  
                                           {1,1,1,0},  
                                           {0,0,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,1,0,0},  
                                           {1,1,0,0},  
                                           {0,1,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,0,0,0},  
                                           {1,1,1,0},  
                                           {0,1,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,1,0,0},  
                                           {0,1,1,0},  
                                           {0,1,0,0},  
                                           {0,0,0,0}  
                                       }  
                                   }};

        #endregion

        #region 定义背景
        private int[,] bgGraoud ={  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0},  
                                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0}  
                                 };
        #endregion

        private Color[] TrickColor = { Color.Red, Color.Blue, Color.Orange, Color.Green };

        private int[,] CurrentTrick = new int[4, 4]; //当前的砖块  
        //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态, CurrentX当前x, CurrentY当前y, Sorce分数  
        private int CurrentTrickNum, CurrentStatusNum, CurrentColor, CurrentX, CurrentY, Sorce;
        private int TricksNum = 4;
        private int StatusNum = 4;
        private int ColorNum = 4;//颜色种类
        private Image myImage;
        private Random rand = new Random();

        private void Form1_Load(object sender, EventArgs e)
        {

            //初始化
            myImage = new Bitmap(panel1.Width, panel1.Height);
            loadGame();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Draw();
            base.OnPaint(e);
        }

        private void loadGame()
        {
            timer1.Interval = 1000;
            Sorce = 0;
            label1.Text = "分数:" + Sorce;

            for (int y = 0; y < 20; y++)
            {
                for (int x = 0; x < 14; x++)
                {
                    bgGraoud[y, x] = 0;
                }
            }
            BeginTricks();
            button2.Text = "开始游戏";
            this.Focus();
        }

        /// <summary>  
        /// 随机生成方块和状态  
        /// </summary>  
        private void BeginTricks()
        {
            //随机生成砖码和状态码  
            CurrentTrickNum = rand.Next(0, TricksNum);
            CurrentStatusNum = rand.Next(0, StatusNum);
            CurrentColor = rand.Next(0, ColorNum);
            //分配数组  
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];
                }
            }
            CurrentX = 5;
            CurrentY = 0;
            //timer1.Start();
        }
        /// <summary>  
        /// 变化方块  
        /// </summary>  
        private void ChangeTricks()
        {
            if (CurrentStatusNum < 3)
            {
                CurrentStatusNum++;
            }
            else
            {
                CurrentStatusNum = 0;
            }
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];
                }
            }
        }
        /// <summary>  
        /// 下落方块  
        /// </summary>  
        private void DownTricks()
        {
            if (CheckIsDown())
            {
                CurrentY++;
            }
            else
            {
                if (CurrentY == 0)
                {
                    timer1.Stop();
                    MessageBox.Show("哈哈,你玩玩了");
                    loadGame();
                    return;
                }
                //下落完成,修改背景  
                for (int y = 0; y < 4; y++)
                {
                    for (int x = 0; x < 4; x++)
                    {
                        if (CurrentTrick[y, x] == 1)
                        {
                            bgGraoud[CurrentY + y, CurrentX + x] = CurrentColor + 1;
                        }
                    }
                }
                CheckSore();
                BeginTricks();

            }
            Draw();
        }
        /// <summary>  
        /// 检测是否可以向下了  
        /// </summary>  
        /// <returns></returns>  
        private bool CheckIsDown()
        {
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (CurrentTrick[y, x] == 1)
                    {
                        //超过了背景  
                        if (y + CurrentY + 1 >= 20)
                        {
                            return false;
                        }
                        if (x + CurrentX >= 14)
                        {
                            CurrentX = 13 - x;
                        }
                        if (bgGraoud[y + CurrentY + 1, x + CurrentX] > 0)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        /// <summary>  
        /// 检测是否可以左移  
        /// </summary>  
        /// <returns></returns>  
        private bool CheckIsLeft()
        {
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (CurrentTrick[y, x] == 1)
                    {
                        if (x + CurrentX - 1 < 0)
                        {
                            return false;
                        }
                        if (bgGraoud[y + CurrentY, x + CurrentX - 1] > 0)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        /// <summary>  
        /// 检测是否可以右移  
        /// </summary>  
        /// <returns></returns>  
        private bool CheckIsRight()
        {
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (CurrentTrick[y, x] == 1)
                    {
                        if (x + CurrentX + 1 >= 14)
                        {
                            return false;
                        }
                        if (bgGraoud[y + CurrentY, x + CurrentX + 1] > 0)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private void Draw()
        {
            Graphics g = Graphics.FromImage(myImage);
            g.Clear(this.BackColor);
            for (int bgy = 0; bgy < 20; bgy++)
            {
                for (int bgx = 0; bgx < 14; bgx++)
                {
                    if (bgGraoud[bgy, bgx] > 0)
                    {
                        g.FillRectangle(new SolidBrush(TrickColor[bgGraoud[bgy, bgx] - 1]), bgx * 20, bgy * 20, 20, 20);
                        g.DrawRectangle(Pens.White, bgx * 20, bgy * 20, 20, 20);
                    }
                }
            }
            //绘制当前的图片  
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (CurrentTrick[y, x] == 1)
                    {
                        g.FillRectangle(new SolidBrush(TrickColor[CurrentColor]), (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20);
                        g.DrawRectangle(Pens.White, (x + CurrentX) * 20, (y + CurrentY) * 20, 20, 20);
                    }
                }
            }
            Graphics gg = panel1.CreateGraphics();

            gg.DrawImage(myImage, 0, 0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            

以上是关于GDI学习之俄罗斯方块续的主要内容,如果未能解决你的问题,请参考以下文章

win32学习之 --------GDI使用 代码记录

Qt学习之秒表的实现(StopWatch) (转)

java写俄罗斯方块啥水平

Java---俄罗斯方块小游戏

CocosCreator经验总结——Typescript实现俄罗斯方块

java的俄罗斯方块代码