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)
        {
            DownTricks();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (timer1.Enabled == false)
            {
                return;
            }
            if (e.KeyCode == Keys.W)
            {
                ChangeTricks();
                Draw();
            }
            else if (e.KeyCode == Keys.A)
            {
                if (CheckIsLeft())
                {
                    CurrentX--;
                }
                Draw();
            }
            else if (e.KeyCode == Keys.D)
            {
                if (CheckIsRight())
                {
                    CurrentX++;
                }
                Draw();
            }
            else if (e.KeyCode == Keys.S)
            {
                timer1.Stop();
                timer1.Interval = 10;
                timer1.Start();
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (timer1.Enabled == false)
            {
                return;
            }
            if (e.KeyCode == Keys.S)
            {
                timer1.Stop();
                timer1.Interval = 1000;
                timer1.Start();
            }
        }

        private void CheckSore()
        {
            for (int y = 19; y > -1; y--)
            {
                bool isFull = true;
                for (int x = 13; x > -1; x--)
                {
                    if (bgGraoud[y, x] == 0)
                    {
                        isFull = false;
                        break;
                    }
                }
                if (isFull)
                {
                    //增加积分
                    Sorce = Sorce + 100;
                    for (int yy = y; yy > 0; yy--)
                    {
                        for (int xx = 0; xx < 14; xx++)
                        {
                            int temp = bgGraoud[yy - 1, xx];
                            bgGraoud[yy, xx] = temp;
                        }
                    }
                    y++;
                    label1.Text = Sorce.ToString(); ;
                    Draw();
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "暂停游戏")
            {
                button2.Text = "开始游戏";
                timer1.Stop();
            }
            else
            {
                button2.Text = "暂停游戏";
                timer1.Start();
            }
        }

    }
}

时间: 2024-10-09 23:54:22

GDI学习之俄罗斯方块续的相关文章

GDI+学习笔记(六)渐变画刷

画刷,顾名思义,就是像画刷一样,向设备上绘制,还记得小时候常唱的首歌,"我是一个粉刷匠.." 好吧,跑题了. 本系列博客希望尽可能简单的描述每项功能,而不希望把每个参数都介绍的详详细细,如果需要,请查阅msdn,本节讲述的渐变画刷,主要有两种,一种是叫线性画刷(LinearGradientBrush),还有一种叫路径画刷(PathGradientBrush),我希望以一种尽可能简单的方式去描述它,但能力有限,所以有什么意见,希望各位能帮忙提出,谢谢. (一)使用画刷 上一节中,我们实际

Entity Framework 学习第二天 续

今天来写一点不一样的删除,修改,查询 下面只写了几个方法 1 /// <summary> 2 /// 根据删除条件进行删除 3 /// </summary> 4 /// <param name="removeWhere"></param> 5 public void remove(System.Linq.Expressions.Expression<Func<StudentInfo, bool>> removeWh

Entity Framework 学习第一天 续

改写第一天的增删改查方法,观察增删改查的本质 1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity.Infrastructure; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace EFConsole 9 { 10 class Program 11 { 12 publi

GDI+学习笔记(五)绘制一个正方体

本文将介绍如何利用GDI+绘制一个正方体. (一)准备阶段 想象一下,高中的时候,我们在学立体几何的时候是怎样画一个正方体的,我们在一张纸上利用投影的思路将其绘制在一张纸上,对吧,这计算投影的部分,我们暂且忽略.下图是我用windows的画图绘制的一个正方体: 我们计算出这些点在平面上的坐标如下: Point A(100,200); Point B(200,200); Point C(100,300); Point D(200,300); Point E(100+50*1.414, 200-50

GDI+学习笔记(七)保存简单图像

请尊重本人的工作成果,转载请留言,并说明转载地址,谢谢.地址如下: http://blog.csdn.net/fukainankai/article/details/27710883 前几节中,我们利用GDI+在窗口中绘制了各种各样的图形.图像,这一节,我们将会将这些图像保存成简单图像.所谓简单图像,指的是bmp/jpg/png等图像或者单帧的gif图像.保存成多帧的gif图像稍微复杂一点,本节中暂时不做说明.保存成动态的tiff文件也比较简单,但这里也不做说明,下次有机会和gif一起介绍. 另

GDI+学习笔记(九)带插件的排序算法演示器(MFC中的GDI+实例)

带插件的排序算法演示器 本节将通过一个实例来说明GDI+在MFC中的应用.这个算法演示器其实是本人算法系列的一个开端,由于csdn没有树状的目录结构,咱也只好使用链表了不是?好了,废话不多说,开始今天的文章. (一)功能说明 我们初步制定功能如下: (1). 能够通过柱状图,自动展示排序算法的交换比较过程 (2). 能够使用插件的形式进行开发.即,当新完成一个算法后,只需要完成一个插件文件(我们这里使用动态库dll),由主程序加载插件,即可进行执行,而不再需要重新编译主程序. (3). 保证主程

C# GDI+学习笔记1

?前言 本文是学习C# GDI+系列的第一篇文章,简单的介绍了GDI+的一些基本绘图内容,比较粗糙.但本文主要是让大家简单的回顾一下GDI+的基本概念.本篇文章的参考代码请在此下载 . GDIPTest_Article1_161112_2346.rar 如果有什么疑问,或者建议,请留言联系本人. 1 窗口重绘 ?Windows自动处理鼠标拖动窗体.更改窗体大小等用户操作,自动调整窗体所占屏幕区域的大小,并进行重画 当需要重画时,Windows发送消息WM_PAINT 窗体对象的Paint事件用于

JFinal学习 &amp; Gradle配置续

接上一篇对Gradle的学习,再用JFinal项目再建一个. gradle配置如下: group 'com.jfinal' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { // maven库 def cn = "http://maven.aliyun.com/nexus/content/groups/public/" def abroad = "http:

GDI+学习------之渐变色

Graphics graphics(dc.GetSafeHdc()); graphics.Clear(Color::White); //定义三种参与渐变的色彩 Color colors[] = { Color::Red, //红色 Color::Green, //过渡色为绿色 Color::Blue //蓝色 };   //定义三种颜色的位置 float positions[] = { 0.0f, // 由红色起 0.3f, // 绿色始于画刷长度的三分之一 1.0f // 到蓝色止 }; //