C#贪吃蛇游戏源码

Draw.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Curriculum_design
{
    interface Draw
    {
        //画蛇
        void DrawSnake();
    }
}

Food.cs

using System.Drawing;
using System.Threading.Tasks;
namespace Curriculum_design
{
  public  class Food
    {
        //食物坐标
        private Point foodPoint;
        //食物颜色
        private Color foodColor = Color.Green;
        //食物大小
        private Size size = new Size(10, 10);

        public Point FoodPoint { get => foodPoint; set => foodPoint = value; }
        public Color FoodColor { get => foodColor; set => foodColor = value; }
        public Size Size { get => size; set => size = value; }
    }
}

Snake.cs

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

namespace Curriculum_design
{
    class Snake : Draw
    {
        //蛇身
        private Label _body;
        //蛇的颜色
        private Color _color = Color.SkyBlue;
        //移动方向默认向西
        private Way way = Way.WEST;
        //移动速度
        private int speed = 100;  
        //蛇
        private ArrayList snake;

        //判断食物是否在蛇身体里
        private bool hasFood = false;

        //颜色属性
        public Color Color { get => _color; set => _color = value; }
        //方向属性
        public Way SnakeWay
        {
            set
            {
                this.way = value;
            }
            get
            {
                return way;
            }
        }
        //速度属性
        public int Speed { get => speed; set => speed = value; }
     
        public void DrawSnake()
        {
            //设置身体
            snake = new ArrayList();
            for (int i = 0; i < 5; i++)
            {
                _body = new Label();
                _body.BackColor = _color;
                _body.Size = new Size(10, 10);
                _body.BorderStyle = BorderStyle.FixedSingle;
                _body.Location = new Point(200 + i * 10, 150);
                snake.Add(_body);
            }
        }
        //返回蛇体
        public ArrayList GetSnake()
        {
            return snake;
        }
        /// <summary>
        /// 蛇的移动
        /// </summary>
        /// <param name="control"></param>
        public void Move(Control control)
        {
            if (!this.hasFood)
            {
                //Controls.Remove 可以实现一个一个的删除控件
                control.Controls.Remove(control.GetChildAtPoint(((Label)snake[snake.Count - 1]).Location));
                //删掉末尾的身体
                snake.RemoveAt(snake.Count - 1);
            }
            Label temp = new Label();
            CopyBody(temp, (Label)snake[0]);
            switch (this.way)
            {
                //运动方向的判断
                case Way.WEST:
                    {
                        temp.Left -= 10;
                        snake.Insert(0, temp);
                        break;
                    }

                case Way.EAST:
                    {
                        temp.Left += 10;
                        snake.Insert(0, temp);
                        break;
                    }
                case Way.NORTH:
                    {
                        temp.Top -= 10;
                        snake.Insert(0, temp);
                        break;
                    }
                case Way.SOUTH:
                    {
                        temp.Top += 10;
                        snake.Insert(0, temp);
                        break;
                    }
            }
            //添加身体
            control.Controls.Add((Label)snake[0]);
            if (this.hasFood)
            {
                this.hasFood = false;
            }
        }

        /// <summary>
        /// 蛇吃东西
        /// </summary>
        /// <param name="food">食物</param>
        /// <returns></returns>
        public bool Eat(Point food)
        {
            if (((Label)snake[0]).Left == food.X && ((Label)snake[0]).Top == food.Y)
            {
                //吃到东西
                this.hasFood = true;
                return true;
            }
            return false;
        }
        //copy蛇身
        void CopyBody(Label x, Label y)
        {
            x.Location = y.Location;
            x.BackColor = y.BackColor;
            x.Size = y.Size;
            x.BorderStyle = y.BorderStyle;
        }
    }
}
 

initialForm.cs设计

initialForm.cs

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 tanchishe1
{
    public partial class initialForm : Form
    {
       
        public initialForm()
        {
            InitializeComponent();
        }

        //打开游戏窗口
        private void StartGame_Click(object sender, EventArgs e)
        {
            MainForm mainForm = new MainForm(); 
            mainForm.Show();
        }
        //关闭游戏窗口
        private void CloseGame_Click(object sender, EventArgs e)
        {
            MainForm.ActiveForm.Close();
          }

    }

}

mainForm.cs设计

mainForm.cs

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

namespace tanchishe1
{
    public enum Way
    {
        EAST,
        SOUTH,
        WEST,
        NORTH
    }
    public partial class MainForm : Form
    {
        Snake snake = new Snake();
        Food foods = new Food();
        private Thread game;
        private Boolean flag = false;
        //创建委托
        private delegate void DrawDele();
        private DrawDele drawDelegate;
        public MainForm()
        {
            this.KeyPreview = true;
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //画蛇
            snake.DrawSnake();
            game = new Thread(new ThreadStart(StartGame));
            game.Start();

            this.button2.Enabled = false;
            
            this.Focus();

        }
        private void StartGame()
        {
            drawDelegate = new DrawDele(PutFood);
            this.Invoke(drawDelegate, null);
            while (true)
            {
                //为当前线程指定时间
                Thread.Sleep(snake.Speed);
                if (this.IsGameOver())
                {
                    MessageBox.Show("GAME OVER");
                    //获取当前线程的执行状态
                    if (game.IsAlive)
                    {
                        //终止线程
                        game.Abort();
                    }
                }
                if (snake.Eat(foods.FoodPoint))
                {                                  
                    //消除原本食物
                    drawDelegate = new DrawDele(KillFood);
                    this.Invoke(drawDelegate, null);

                    //重新放置食物
                    drawDelegate = new DrawDele(PutFood);
                    this.Invoke(drawDelegate, null);
                }
                
                
                drawDelegate = new DrawDele(MoveSnake);
                this.Invoke(drawDelegate, null);

            }
        }
        //键盘控制移动方向
        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyData == Keys.A)
                this.snake.SnakeWay = (this.snake.SnakeWay == Way.EAST) ? Way.EAST : Way.WEST;
            else if (e.KeyData == Keys.D)
                this.snake.SnakeWay = (this.snake.SnakeWay == Way.WEST) ? Way.WEST : Way.EAST;
            else if (e.KeyData == Keys.W)
                this.snake.SnakeWay = (this.snake.SnakeWay == Way.SOUTH) ? Way.SOUTH : Way.NORTH;
            else if (e.KeyData == Keys.S)
                this.snake.SnakeWay = (this.snake.SnakeWay == Way.NORTH) ? Way.NORTH : Way.SOUTH;

        }
        /// <summary>
        /// 放置食物
        /// </summary>
        private void PutFood()
        {
            Random rand = new Random();
            //返回一个小于指定数的随机数
            int x = rand.Next(510);
            int y = rand.Next(230);
            //食物设置
            Label food = new Label();
            food.Size = foods.Size;
            food.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            food.BackColor = foods.FoodColor;
            if (x < 10)
            {
                x += 10;
            }
            if (y < 10)
            {
                y += 10;
            }
            food.Location = new Point(x % 10 == 0 ? x : x + (10 - x % 10), y % 10 == 0 ? y : y + (10 - y % 10));
            foods.FoodPoint = new Point(food.Left, food.Top);
            panel1.Controls.Add(food);
        }

        /// <summary>
        /// 清除食物
        /// </summary>
        private void KillFood()
        {
            ClearFood(foods.FoodPoint);
        }
        private void ClearFood(Point food)
        {
            panel1.Controls.Remove(panel1.GetChildAtPoint(food));
        }

        /// <summary>
        /// 判断游戏是否结束
        /// </summary>
        /// <returns></returns>
        private bool IsGameOver()
        {
            ArrayList temp = snake.GetSnake();
            Label head = (Label)temp[0];
            foreach (Label lbl in temp.GetRange(1, temp.Count - 1))
            {
                //判断是否碰撞身体
                if (lbl.Left == head.Left && lbl.Top == head.Top)
                {
                    return true;
                }
            }
            //判断是否碰撞墙壁
            if (((Label)snake.GetSnake()[0]).Left == 0 || ((Label)snake.GetSnake()[0]).Left == 520 ||
                ((Label)snake.GetSnake()[0]).Top == 0 || ((Label)this.snake.GetSnake()[0]).Top == 240)
            {
                return true;
            }
            return false;
        }
        //蛇移动
        private void MoveSnake()
        {
            snake.Move(panel1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("通过按下 上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到" +
                "一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙," +
                "不能咬到自己的身体,更不能咬自己的尾巴,达到了1000分,就能过关。");
        }
        private delegate void SetTextCallback(string text);
        //在给textBox1.text赋值的地方调用以下方法即可
       
        //设置颜色
        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            snake.Color = Color.Red;
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            snake.Color = Color.Green;
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            snake.Color = Color.Blue;
        }

        //暂停游戏
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            flag = true;
            game.Suspend();
        }

        //继续游戏
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                game.Resume();
            }
            else
            {
                MessageBox.Show("游戏未暂停!!!");
            }
        }
        //简单模式
        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            snake.Speed = 500;
        }
        //普通模式
        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            snake.Speed = 100;
        }
        //困难模式
        private void toolStripMenuItem12_Click(object sender, EventArgs e)
        {
            snake.Speed = 50;

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }


    }
}
 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
游戏名称:贪吃蛇 游戏级别:10级 注意:因为文件太大,无法上传,所以将音乐和图片文件另外发布,请下载(免费),下载完解压缩后,将音乐和图片放在Debug根目录下即可。 游戏说明: 关于控制蛇运动方向: 用键盘上的↑、↓、←、→控制蛇的运动方向。 当蛇向一个方向运动时,它的反向键被锁定。 不能通过连续摁某个方向键而加快蛇的运动。 若在游戏中,蛇头碰了墙或是自己的身体,则游戏结束。 关于蛇吃食物: 蛇每吃一个食物增长一节,并且得分加10分,总共为100节。 蛇每增长10节游戏上升一个级别。 关于游戏结束: 如果在游戏过程中,蛇头碰墙或是碰到了自己的身体,则游戏结束。 如果在游戏过程中,用户选择退出游戏,则会提醒用户游戏正在进行中,是否要退出。 选择退出,则游戏结束。 选择取消,则游戏继续。 如果玩家成功过关,则游戏结束。 在上述任何一种情况下,系统都会询问用户:是否保存游戏成绩? 选择确定,如果姓名为空,则默认以“匿名玩家”记录。 选择取消,则不记录。 关于蛇运动的速度: 玩家可以通过:选项->速度 打开窗口。 系统默认的方式是变速游戏。 玩家可以自行选择变速游戏或是均速游戏。 变速游戏:速度分1-10个级别。 并且根据关卡的高低决定速度的快慢。 均速游戏:用户可以自行选择所要速度的快慢。 1表示最慢,10表示最快。 关于音乐的播放: 当打开游戏界面时,音乐会自动播放,默认的音乐是:Remeber。 若玩家要修改或关闭音乐,可以通过:选项->音乐 或是 F5快捷键 打开窗口。 若玩家不想播放音乐,请点击关闭。 若玩家要播放选中的音乐,请点击确定。 关于界面的选择: 玩家可以根据爱好选择想要的图片,总共有8张背景图片供选择。 玩家选择图片后,可以在对话框的右边预览到图片。 点击确定则显示选中的图片,点击取消则显示原先图片。 当游戏开始后,此功能键不能用。 关于查看记录: 玩家可以通过:关于->查看记录 或 F6快捷键 打开窗口。 窗口中显示了以往玩家的前三名成绩。 关于游戏说明: 玩家可以通过:关于->游戏说明 或 F7快捷键 打开窗口。 窗口中显示了本游戏的说明。 关于游戏的快捷键: 开始:F2 暂停:F3 退出:F4 音乐:F5 查看记录:F6 游戏说明:F7

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值