VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

2025-12-01 07:23:14

1、新建项目,选择c#语言,Windows程序

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

2、右边的窗口中有 form1.cs ,它有两种查看方式,可以使用 设计的方式,也可以使用代码的方式,代码的方式查看如下。

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

3、设置主窗体的属性,属性工具栏也在右边,设置的属性如图所示。

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

4、切换到设计模式下面 打开工具箱,拖一个 pictureBox控件到主窗口。同时设置pictureBox的属性,把属性名称设置为pBox

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

5、向主窗口添加菜单,拖动菜单控件到主窗体,并设置三个子菜单item,并设置三个子菜单的属性名称。

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

6、向主窗体添加定时器,并设置定时器的间隔时间等属性

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

7、到目前为止界面基本上就画完了,下面的工作就是要实现 Snake 类,这个类的目的就是完成贪吃蛇的核心功能。首先创建一个Snake类。并完成相应的函数。添加新类可以使用Shift + Alt +c 添加。代码如下,(看不清的话我会将源代码放到网上,下载链接在下面)

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Snake{    class Point    {        int x, y;        public int Y        {            get { return y; }            set { y = value; }        }        public int X        {            get { return x; }            set { x = value; }        }        public Point() { x = y = 0; }        public Point(int _x, int _y)        {            x = _x;            y = _y;        }    }    class Snake    {        private List<Point> sQ = new List<Point>();        private Point food;        private Random rand;        internal Point Food        {            get { return food; }            set { food = value; }        }        internal List<Point> SQ        {            get { return sQ; }        }        private int direct;  // 0 down 1 left 2 up 3 right        private int xNum;        private int yNum;        private Point head;        internal Point Head        {            get { return head; }            set { head = value; }        }        public int Direct        {            get { return direct; }            set { direct = value; }        }        public void initArr(int[,] arr, int x, int y, int _d = 0)        {            for (int i = 0; i < x; i++)            {                for (int j = 0; j < y; j++)                {                    arr[i, j] = _d;                }            }        }        public Snake()        {            head = new Point(4,1);            xNum = yNum = 10;            direct = 0;            rand = new Random();            food = new Point();            generateFood();        }        public void generateFood()        {            food.X = rand.Next(10);            food.Y = rand.Next(10);        }        public void left(){            if(direct!=3)direct = 1;        }        public void right(){            if (direct != 1) direct = 3;        }        public void up(){            if (direct != 0) direct = 2;        }        public void down()        {            if (direct != 2) direct = 0;        }        public void next()        {            Point p = new Point(head.X,head.Y);            switch (direct)            {                case 0:                    p.X+=1;                    break;                case 1:                    p.Y-=1;                    break;                case 2:                    p.X-=1;                    break;                case 3:                    p.Y+=1;                    break;            }            if (p.X == food.X && p.Y == food.Y)            {                generateFood();                eat(p.X, p.Y);                head = p ;                return;            }            sQ.Insert(0, p);            sQ.RemoveAt(sQ.Count - 1);            head = p;        }        public void addHead()        {            sQ.Insert(0, head);        }        public bool isOver()        {            Point p = head;            bool res = false;            switch (direct)            {                case 0:                    if (p.X == xNum - 1) res = true ;                    break;                case 1:                    if (p.Y == 0) res = true;                                        break;                case 2:                    if (p.X == 0) res = true;                    break;                case 3:                    if (p.Y == yNum - 1) res = true;                    break;            }            return res;        }        public void eat(int x, int y)        {            Point p = new Point(x, y);            head = p;            sQ.Insert(0, p);        }    }}

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

8、Snake的基本功能在Snake类中都实现了,下面的工作就是在界面类中调用类的函数,完成程序的最后步骤。先添加响应函数,总共有下面的响应函数

3个menuitem的响应函数,前面已经添加;

1个定时器的回调函数;

2个键盘响应函数;keyup和keydown

如图所示:

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

9、添加完成响应函数之后是没有实现的,需要自己实现函数体,代码都在 Form1.cs中修改。

下面就是我实现的代码:格式很乱!!没办法(看不清的话我会将源代码放到网上,下载链接在下面)

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Snake{        public partial class Form1 : Form    {        Snake snake;        Point food;                int[,] data;        int unitWidth, unitHeight;        int xNum, yNum;        Pen bgPen;        Pen greenPen;        Pen whitePen;        Pen yellowPen;        Pen redPen;        public Form1()        {            InitializeComponent();            food = new Point();            xNum = 10;            yNum = 10;            data = new int[xNum, yNum];            bgPen = new Pen(Color.Black);            whitePen = new Pen(Color.White);            greenPen = new Pen(Color.Green);            yellowPen = new Pen(Color.Yellow);            redPen = new Pen(Color.Red);            snake = new Snake() ;            this.pBox.Image = new System.Drawing.Bitmap(this.pBox.Size.Width, this.pBox.Size.Height);            this.unitWidth = this.pBox.Image.Width / this.yNum - 1;            this.unitHeight = this.pBox.Image.Height / this.xNum - 1;            reDrawPBox();            this.timer1.Enabled = false;            initFace();        }        void initFace()        {            snake.addHead();        }                void gameOver()        {            Graphics g = Graphics.FromImage(this.pBox.Image);            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 20);            g.DrawString("GAME OVER", font, this.redPen.Brush, this.pBox.Image.Width / 2 - 80, this.pBox.Image.Height / 3);            this.pBox.Refresh();            font.Dispose();            g.Dispose();        }        void drawMemory()        {            snake.initArr(data, xNum, yNum);            for (int i = 0; i < snake.SQ.Count; i++)            {                data[snake.SQ.ElementAt(i).X, snake.SQ.ElementAt(i).Y] = 2;            }            data[snake.Head.X, snake.Head.Y] = 1 ;            data[snake.Food.X, snake.Food.Y] = 3; // 食物        }                void reDrawPBox()        {            Graphics g = Graphics.FromImage(pBox.Image);            Pen otPen = null;            g.FillRectangle(bgPen.Brush, 0, 0, this.pBox.Image.Width, this.pBox.Image.Height);            for (int i = 0; i < this.xNum; i++)            {                for (int j = 0; j < this.yNum; j++)                {                    if (data[i, j] == 0) otPen = whitePen;                    else if(data[i, j] == 1)otPen = redPen ;                    else if (data[i, j] == 2) otPen = greenPen;                    else if (data[i, j] == 3) otPen = yellowPen;                    g.FillRectangle(otPen.Brush,j * (unitWidth + 1), i * (unitHeight + 1), unitWidth, unitHeight);                }            }            pBox.Refresh();            g.Dispose();        }        private void onStartMenuClick(object sender, EventArgs e)        {            snake.SQ.Clear();            snake.addHead();            snake.initArr(data, xNum, yNum);            this.timer1.Enabled = true;        }        private void timer1_Tick(object sender, EventArgs e)        {            if (snake.isOver())            {                gameOver();                this.timer1.Enabled = false;                return;            }            snake.next();            drawMemory();            reDrawPBox();        }        private void onKeyUp(object sender, KeyEventArgs e)        {            if(e.KeyCode == Keys.Up){                snake.up();            }else if(e.KeyCode == Keys.Left){                snake.left();            }else if(e.KeyCode == Keys.Down){                snake.down();            }            else if (e.KeyCode == Keys.Right)            {                snake.right();            }                  }        private void onStopMenuClick(object sender, EventArgs e)        {                        if (this.stopToolStripMenuItem.Text == "继续")            {                this.timer1.Enabled = true;                this.stopToolStripMenuItem.Text = "暂停";            }            else            {                this.timer1.Enabled = false;                this.stopToolStripMenuItem.Text = "继续";            }        }        private void onExitClick(object sender, EventArgs e)        {            Environment.Exit(0);        }                private void onKeyDown(object sender, KeyEventArgs e)        {            if (e.KeyCode == Keys.Up)            {                snake.up();            }            else if (e.KeyCode == Keys.Left)            {                snake.left();            }            else if (e.KeyCode == Keys.Down)            {                snake.down();            }            else if (e.KeyCode == Keys.Right)            {                snake.right();            }            if (snake.isOver())            {                gameOver();                this.timer1.Enabled = false;                return;            }            snake.next();            drawMemory();            reDrawPBox();        }    }    }

10、然后就是调试,运行了,这样就能出现最简单的贪吃蛇游戏了!

VS2013程序设计系列-C#:[1]C#贪吃蛇游戏

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