• 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

  • 相关阅读:
    Python 的 Pandas 对矩阵的行进行求和
    python 统计字符串中指定字符出现次数的方法
    Python 2 代码转 Python 3的一些转化
    Python 的文件保存路径
    Pycharm 个人认为舒服漂亮又好用的主题风格
    【RS】Collaborative Memory Network for Recommendation Systems
    MATLAB 条形图添加多个图例
    MATLAB 画柱状图(/直方图)修改横坐标名称并使其横着显示
    Nexus3.X 将Maven项目上传至nexus私服
    nexus maven 使用案例
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5306983.html
Copyright © 2020-2023  润新知