首页

C# 贪吃蛇游戏(小游戏源码)

c#

2020-9-7

没注释

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tcs_zy
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

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

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Add_food();

            button1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            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; };
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            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;

            }
        }
    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
tcs-zy/.vs/tcs-zy/v15/.suo , 39936
tcs-zy/.vs/tcs-zy/v15/Server/sqlite3/db.lock , 0
tcs-zy/.vs/tcs-zy/v15/Server/sqlite3/storage.ide , 589824
tcs-zy/.vs/tcs-zy/v15/Server/sqlite3/storage.ide-shm , 32768
tcs-zy/.vs/tcs-zy/v15/Server/sqlite3/storage.ide-wal , 4157112
tcs-zy/tcs-zy.sln , 1116
tcs-zy/tcs-zy/App.config , 187
tcs-zy/tcs-zy/bin/Debug/tcs-zy.exe , 14336
tcs-zy/tcs-zy/bin/Debug/tcs-zy.exe.config , 187
tcs-zy/tcs-zy/bin/Debug/tcs-zy.pdb , 34304
tcs-zy/tcs-zy/FrmMain.cs , 11549
tcs-zy/tcs-zy/FrmMain.Designer.cs , 3802
tcs-zy/tcs-zy/FrmMain.resx , 6011
tcs-zy/tcs-zy/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 827
tcs-zy/tcs-zy/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 7024
tcs-zy/tcs-zy/obj/Debug/tcs_zy.FrmMain.resources , 180
tcs-zy/tcs-zy/obj/Debug/tcs_zy.Properties.Resources.resources , 180
tcs-zy/tcs-zy/obj/Debug/tcs-zy.csproj.CoreCompileInputs.cache , 42
tcs-zy/tcs-zy/obj/Debug/tcs-zy.csproj.FileListAbsolute.txt , 517
tcs-zy/tcs-zy/obj/Debug/tcs-zy.csproj.GenerateResource.cache , 1074
tcs-zy/tcs-zy/obj/Debug/tcs-zy.csprojAssemblyReference.cache , 11466
tcs-zy/tcs-zy/obj/Debug/tcs-zy.exe , 14336
tcs-zy/tcs-zy/obj/Debug/tcs-zy.pdb , 34304
tcs-zy/tcs-zy/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
tcs-zy/tcs-zy/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
tcs-zy/tcs-zy/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
tcs-zy/tcs-zy/Program.cs , 520
tcs-zy/tcs-zy/Properties/AssemblyInfo.cs , 1310
tcs-zy/tcs-zy/Properties/Resources.Designer.cs , 2825
tcs-zy/tcs-zy/Properties/Resources.resx , 5612
tcs-zy/tcs-zy/Properties/Settings.Designer.cs , 1093
tcs-zy/tcs-zy/Properties/Settings.settings , 249
tcs-zy/tcs-zy/tcs-zy.csproj , 3537
没有账号? 忘记密码?

社交账号快速登录