zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    SAP MM 有了采购订单历史的PO行项目里的采购附加费不允许再改了?
    风之语.人在职场也需要'备胎'
    两万字,任正非采访全记录
    SAP 如何得到交货单上的序列号清单?
    【2019年版】如何向SAP公司提交Message?
    工作上996,生活上669,并不是什么难事儿!
    风之语.甲骨文裁员之我见
    天河2号-保持使用yhrun/srun时连接不中断 (screen 命令教程 )
    PXE 和 计算机网络启动
    Gmail 设置,时区
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5306983.html
Copyright © 2011-2022 走看看