C#制作贪吃蛇游戏(下)

2025-10-28 08:55:24

1、双击窗体(注意不是双击控件),加入代码:

        public class Di

        {

            public static int D;

            public static int s = 2;

        }

        void Add_food()

        {

            Label label = new Label();

            label.Name = "Lab" + Di.s;

            label.BackColor = System.Drawing.Color.Red;

            label.Size = new System.Drawing.Size(10, 10);

            label.Margin = new System.Windows.Forms.Padding(0);

            label.ForeColor = System.Drawing.Color.Red;

            label.AutoSize = false;

            label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            Random rd = new Random();

        tag1:

            label.Location = new System.Drawing.Point(rd.Next(0, 30) * 10, rd.Next(0, 30) * 10);

            for (int i = 1; i < Di.s; i++)

            {

                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (label.Location == lo.Location) goto tag1; 

            }

            this.Controls.Add(label);

            label.SendToBack();

            panel1.SendToBack();

        }

如图所示:

C#制作贪吃蛇游戏(下)

2、双击开始按钮,加入代码:

            Add_food();

            button1.Enabled = false;

如图所示:

C#制作贪吃蛇游戏(下)

3、双击timer1控件,加入代码:

            if (Di.D == 1) { System.Windows.Forms.SendKeys.Send("{DOWN}"); return; };

            if (Di.D == 2) { System.Windows.Forms.SendKeys.Send("{UP}"); return; };

            if (Di.D == 3) { System.Windows.Forms.SendKeys.Send("{LEFT}"); return; };

            if (Di.D == 4) { System.Windows.Forms.SendKeys.Send("{RIGHT}"); return; };

如图所示:

C#制作贪吃蛇游戏(下)

4、单击Form1,在属性中单击事件,找到KeyDown,双击,加入代码:

            if (e.KeyCode == Keys.Down && Di.D != 2)

            {

                timer1.Enabled = false;

                Di.D = 1;

                Label lb = (Label)this.Controls.Find("Lab" + Di.s, true)[0];

                Label lbl = (Label)this.Controls.Find("Lab" + (Di.s - 1), true)[0];

                Point[,] Lo = new Point[101, 101];

                for (int i = 1; i <= Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);

                }

                Label le = new Label();

                le.Visible = false;

                if (e.KeyCode == Keys.Down && Di.s > 2 && lbl.BackColor != System.Drawing.Color.Red && Lab1.Location.Y != 290)

                {

                    for (int i = 2; i < Di.s; i++)

                    {

                        Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];

                    }

                }

                if (lb.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y + 10))

                {

                    lb.BackColor = System.Drawing.Color.Black;

                    le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);

                    Lab1.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y + 10);

                    lb.Location = le.Location;

                    this.Controls.Remove(le);

                    Di.s++;

                    Add_food();

                    timer1.Enabled = true;

                    return;

                }

                for (int i = 1; i < Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y + 10)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;

                }

                if (e.KeyCode == Keys.Down && Lab1.Location.Y == 290) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; };

                Lab1.Top = Lab1.Top + 10;

                timer1.Enabled = true;

            }

            if (e.KeyCode == Keys.Up && Di.D != 1)

            {

                timer1.Enabled = false;

                Di.D = 2;

                Label lb = (Label)this.Controls.Find("Lab" + Di.s, true)[0];

                Label lbl = (Label)this.Controls.Find("Lab" + (Di.s - 1), true)[0];

                Point[,] Lo = new Point[101, 101];

                for (int i = 1; i <= Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);

                }

                Label le = new Label();

                le.Visible = false;

                if (e.KeyCode == Keys.Up && Di.s > 2 && lbl.BackColor != System.Drawing.Color.Red && Lab1.Location.Y != 0)

                {

                    for (int i = 2; i < Di.s; i++)

                    {

                        Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];

                    }

                }

                if (lb.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y - 10))

                {

                    lb.BackColor = System.Drawing.Color.Black;

                    le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);

                    Lab1.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y - 10);

                    lb.Location = le.Location;

                    this.Controls.Remove(le);

                    Di.s++;

                    Add_food();

                    timer1.Enabled = true;

                    return;

                }

                for (int i = 1; i < Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y - 10)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;

                }

                if (e.KeyCode == Keys.Up && Lab1.Location.Y == 0) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; };

                Lab1.Top = Lab1.Top - 10;

                timer1.Enabled = true;

            }

            if (e.KeyCode == Keys.Left && Di.D != 4)

            {

                timer1.Enabled = false;

                Di.D = 3;

                Label lb = (Label)this.Controls.Find("Lab" + Di.s, true)[0];

                Label lbl = (Label)this.Controls.Find("Lab" + (Di.s - 1), true)[0];

                Point[,] Lo = new Point[101, 101];

                for (int i = 1; i <= Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);

                }

                Label le = new Label();

                le.Visible = false;

                if (e.KeyCode == Keys.Left && Di.s > 2 && lbl.BackColor != System.Drawing.Color.Red && Lab1.Location.X != 0)

                {

                    for (int i = 2; i < Di.s; i++)

                    {

                        Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];

                    }

                }

                if (lb.Location == new System.Drawing.Point(Lab1.Location.X - 10, Lab1.Location.Y))

                {

                    lb.BackColor = System.Drawing.Color.Black;

                    le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);

                    Lab1.Location = new System.Drawing.Point(Lab1.Location.X - 10, Lab1.Location.Y);

                    lb.Location = le.Location;

                    this.Controls.Remove(le);

                    Di.s++;

                    Add_food();

                    timer1.Enabled = true;

                    return;

                }

                for (int i = 1; i < Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(Lab1.Location.X - 10, Lab1.Location.Y)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;

                }

                if (e.KeyCode == Keys.Left && Lab1.Location.X == 0) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; };

                Lab1.Left = Lab1.Left - 10;

                timer1.Enabled = true;

            }

            if (e.KeyCode == Keys.Right && Di.D != 3)

            {

                timer1.Enabled = false;

                Di.D = 4;

                Label lb = (Label)this.Controls.Find("Lab" + Di.s, true)[0];

                Label lbl = (Label)this.Controls.Find("Lab" + (Di.s - 1), true)[0];

                Point[,] Lo = new Point[101, 101];

                for (int i = 1; i <= Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);

                }

                Label le = new Label();

                le.Visible = false;

                if (e.KeyCode == Keys.Right && Di.s > 2 && lbl.BackColor != System.Drawing.Color.Red && Lab1.Location.X != 290)

                {

                    for (int i = 2; i < Di.s; i++)

                    {

                        Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];

                    }

                }

                if (lb.Location == new System.Drawing.Point(Lab1.Location.X + 10, Lab1.Location.Y))

                {

                    lb.BackColor = System.Drawing.Color.Black;

                    le.Location = new System.Drawing.Point(Lab1.Location.X, Lab1.Location.Y);

                    Lab1.Location = new System.Drawing.Point(Lab1.Location.X + 10, Lab1.Location.Y);

                    lb.Location = le.Location;

                    this.Controls.Remove(le);

                    Di.s++;

                    Add_food();

                    timer1.Enabled = true;

                    return;

                }

                for (int i = 1; i < Di.s; i++)

                {

                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(Lab1.Location.X + 10, Lab1.Location.Y)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;

                }

                if (e.KeyCode == Keys.Right && Lab1.Location.X == 290) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; };

                Lab1.Left = Lab1.Left + 10;

                timer1.Enabled = true;

            }

如图所示:

C#制作贪吃蛇游戏(下)

5、至此,所有工作完成,按F5即可查看效果,如下图所示:

C#制作贪吃蛇游戏(下)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢