GDI学习之俄罗斯方块

做个玩玩

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},  
                                          {1,1,1,1},  
                                          {0,0,0,0},  
                                          {0,0,0,0}  
                                      },  
                                      {  
                                          {0,1,0,0},  
                                          {0,1,0,0},  
                                          {0,1,0,0},  
                                          {0,1,0,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,1,1,0},  
                                           {1,1,0,0},  
                                           {0,0,0,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,1,0,0},  
                                           {0,1,1,0},  
                                           {0,0,1,0},  
                                           {0,0,0,0}  
                                       },  
                                       {  
                                           {0,1,1,0},  
                                           {1,1,0,0},  
                                           {0,0,0,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}  
                                       }  
                                   }};

        #endregion

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

        private int[,] CurrentTrick = new int[4, 4]; //当前的砖块  
        //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态  CurrentColor当前颜色
        private int CurrentTrickNum, CurrentStatusNum, CurrentColor;
        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);
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    CurrentTrick[y, x] = 0;
                }
            }
            Draw();
        }

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

        private void Draw()
        {
            Graphics g = Graphics.FromImage(myImage);
            g.Clear(Color.Black);
            //绘制当前的图片  
            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 * 32, y * 32, 32, 32);
                        g.DrawRectangle(Pens.White, x * 32, y * 32, 32, 32);
                    }
                }
            }
            Graphics gg = panel1.CreateGraphics();
            gg.DrawImage(myImage, 0, 0);
        }

        //随机生成方块
        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];
                }
            }
        }

        //控制方向
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                ChangeTricks();
                Draw();
            }
        }

        //变换方块
        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];
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BeginTricks();
            Draw();
        }
View Code

原文地址:https://www.cnblogs.com/margin-gu/p/5306983.html