C#winform 经典小游戏贪吃蛇V2.0(二)

关于V2.0

经过上一篇文章
贪吃蛇V1.0
我们的蛇已经能跑能吃,但是还是存在着许多的问题,

  • 没有死亡判定;
  • 不能调节游戏难度;
  • 没有开始界面;
  • 游戏界面太单调。

这里列出来的只是一部分的问题,也是我们这个版本主要解决的问题!

首先针对游戏界面的问题

由于原先的界面为单调的纯白,这样不仅加大了游戏难度,玩家的体验也极差,
所以我在游戏的界面添加了网格线,让玩家可以更加精准的找到食物和蛇相对应的行列。
原来界面:
这里写图片描述
更改后的界面:
这里写图片描述
是不是感觉好多了?
在添加网格线的过程中我遇到了一些问题:
解决方法

网格线的绘制,放在窗体的Paint事件中,代码如下:

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int col = 50;
            int row = 50;
            int drawRow = 0;
            int drawCol = 0;
            Pen black = new Pen(Color.Gray, 1);
            //black.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
            Graphics g = this.CreateGraphics();
            for (int i = 0; i <= row; i++)
            {
                g.DrawLine(black, 0, drawCol, 500, drawCol);
                drawCol += 10;
            }
            // 画垂直线
            for (int j = 0; j <= col; j++)
            {
                g.DrawLine(black, drawRow, 0, drawRow, 500);
                drawRow += 10;
            }
        }

蛇的死亡事件

思路为在每一次移动,进行一次Snake_over判定
代码如下:
遍历蛇体判定蛇头Snake_Boby[0]的坐标,是否和蛇体位置重合
然后在Timer_TickForm1_KeyDownSnake_move(x, y)后添加Snake_over()

        /// <summary>
        /// 蛇触碰身体死亡事件
        /// </summary>
        public void Snake_over()
        {
            int x, y;
            //记录snake head位置
            x = Snake_Boby[0].Left;
            y = Snake_Boby[0].Top;
            //遍历看是否和snake body重合
            foreach (Label lb in this.Controls)
            {
                //将food排除
                if (lb.Tag.ToString() != "food".ToString())
                {
                    //出现重合
                    if ((lb.Left == x && lb.Top == y) && lb.Tag.ToString()!="0")
                    {
                        this.Close();
                        MessageBox.Show("GAME OVER !", "提示!");

                    }
                }
            }
        }

增加游戏开始界面Game_Begin

Game_Begin界面如图
这里写图片描述
控件如下:

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton radioButton4;
        private System.Windows.Forms.RadioButton radioButton3;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;

后台代码:
设置一个成员变量用来存储时间间隔,设置游戏难度

        /// <summary>
        /// 用来设置游戏难度
        /// </summary>
        public static int time_interval = 100;

页面跳转和难度选择

        private void button1_Click(object sender, EventArgs e)
        {

            Form1 form = new Form1();
            if (radioButton1.Checked)
            {
                time_interval = 200;
            }
            else if (radioButton3.Checked)
            {
                time_interval = 50;
            }
            else if (radioButton4.Checked)
            {
                time_interval = 20;
            }
            else
            {
                time_interval = 100;
            }
            form.Show();

        }

将程序初始化界面设为Game_Begin界面
在program.cs中改为:

            Application.Run(new Game_Begin());

然后在游戏界面Form1的Load事件

添加:

            timer.Interval = Game_Begin.time_interval;

Timer_Tick的事件间隔timer.Interval改为Game_Begin界面我们获取RadioButton的值

最后,看看V2.0的效果:

这里写图片描述
Git地址

好了贪吃蛇的更新就到这了,如果以后有兴趣再做V3.0啦!(ง •_•)ง

猜你喜欢

转载自blog.csdn.net/define_lin/article/details/80632313