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

  • 相关阅读:
    User Profile Service 应用程序的新增功能
    内容管理互操作性服务 (CMIS) 连接器概述 (SharePoint Server 2010)
    AD二次开发提示“出现了一个操作错误”的解决办法
    Excel2010与MOSS2010数据同步配置
    InfoPath2010表单IE浏览器2个“微型内嵌工具”的使用和介绍
    洗礼灵魂,修炼python(1)python简介
    洗礼灵魂,修炼python(2)python安装和配置
    C语言标准定义的32个关键字
    【学习笔记】.NET知识体系结构
    【读】自定义博客园皮肤:暗色流体响应式布局
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5306983.html
Copyright © 2011-2022 走看看