基本信息
源码名称:C# 贪吃蛇 小游戏源码
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2018-03-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

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

namespace Snake
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool gb = false;
        int xold, yold;//蛇头原始位置
        public string direction = "";//默认移动方向
        public string newdirection = "";//新移动方向
        List<Label> list = new List<Label>();//蛇身
        Label lblx;//蛇食
        /// <summary>
        /// 移动蛇头
        /// </summary>
        public void MoveHead()
        {
            while (gb==false)
            {
                xold = this.lbltou.Left;
                yold = this.lbltou.Top;

                switch (direction)
                {
                    //向上移动
                    case "Up":
                        this.lbltou.Top -= 15;
                        if (this.lbltou.Top <= 0)
                        {
                            this.lbltou.Top = 585;
                        }
                        newdirection = "Up";
                        MoveBody();
                        break;
                    //向下移动
                    case "Down":
                        this.lbltou.Top  = 15;
                        if (this.lbltou.Top >= 585)
                        {
                            this.lbltou.Top = 0;
                        }
                        newdirection = "Down";
                        MoveBody();
                        break;
                    //向右移动
                    case "Right":
                        this.lbltou.Left  = 15;
                        if (this.lbltou.Left >= 585)
                        {
                            this.lbltou.Left = 0;
                        }
                        newdirection = "Right";
                        MoveBody();
                        break;
                    //向左移动
                    case "Left":
                        this.lbltou.Left -= 15;
                        if (this.lbltou.Left <= 0)
                        {
                            this.lbltou.Left = 585;
                        }
                        newdirection = "Left";
                        MoveBody();
                        break;
                }


                if (this.lbltou.Location == this.lblx.Location)
                {
                    //吃掉蛇食追加蛇身
                    lblx.BackColor = Color.Cyan;
                    this.list.Add(lblx);
                }

                if (一般ToolStripMenuItem.Checked == true)
                {
                    Thread.Sleep(100);
                }
                else if (简单ToolStripMenuItem.Checked == true)
                {
                    Thread.Sleep(500);
                }
                else if (困难ToolStripMenuItem.Checked==true)
                {
                    Thread.Sleep(1);
                }
                
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            list.Add(lbltou);
            list.Add(label1);
            list.Add(label2);
            list.Add(label3);
            list.Add(label4);

            Thread thread = new Thread(new ThreadStart(MoveHead));
            thread.Start();
            //生成蛇食
            CreateFood();
        }
        /// <summary>
        /// 生成蛇食
        /// </summary>
        private void CreateFood()
        {
            //蛇食的特征
            lblx = new Label();
            lblx.Width = 15;
            lblx.Height = 15;
            lblx.BackColor = Color.Red;
            
            
            lblx.BorderStyle = BorderStyle.FixedSingle;
            //随机生成蛇食位置
            Random rd = new Random();
            int x, y;
            bool bOK = true;
            while (bOK)
            {
                x = rd.Next(10, 550);
                y = rd.Next(10, 550);

                if (x % 15 == 0 && y % 15 == 0)
                {
                    lblx.Top = y;
                    lblx.Left = x;
                    bOK = false;
                }
            }
            
            lblx.Move  = lblx_Move;
            this.Controls.Add(lblx);
        }

        void lblx_Move(object sender, EventArgs e)
        {
            lblx.Move -= lblx_Move;
            CreateFood();
        }
        /// <summary>
        /// 移动蛇身
        /// </summary>
        private void MoveBody()
        {
            int x, y;
            for (int i = 1; i < list.Count; i  )
            {
                x = list[i].Left;
                y = list[i].Top;
                list[i].Left = xold;
                list[i].Top = yold;
                xold = x;
                yold = y;
                if (lbltou.Location == list[i].Location)//判断蛇头是否触碰蛇身,是将结束游戏
                {
                    MessageBox.Show("Game Over");
                    direction = "";
                    gb = true;
                    Application.Exit();
                    return;
                }
            }
        }


        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            direction = e.KeyCode.ToString();
            //进行循环判断来保证不能反方向移动
            if (newdirection=="Up"&&direction=="Down")
            {
                direction = "Up";
            }
            if (newdirection == "Down" && direction == "Up")
            {
                direction = "Down";
            }
            if (newdirection == "Right" && direction == "Left")
            {
                direction = "Right";
            }
            if (newdirection == "Left" && direction == "Right")
            {
                direction = "Left";
            }
        }

        //设定难度
        private void 简单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            一般ToolStripMenuItem.Checked = false;
            困难ToolStripMenuItem.Checked = false;
            简单ToolStripMenuItem.Checked = true;
        }

        private void 一般ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            一般ToolStripMenuItem.Checked = true;
            困难ToolStripMenuItem.Checked = false;
            简单ToolStripMenuItem.Checked = false;
        }

        private void 困难ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            一般ToolStripMenuItem.Checked = false;
            困难ToolStripMenuItem.Checked = true;
            简单ToolStripMenuItem.Checked = false;
        }
    }
}