VS2013程序设计系列-C#:[1]C#贪吃蛇游戏
1、新建项目,选择c#语言,Windows程序
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/51f9aa3ea8db574a92e88428a7f7dfb2dd191772.jpg)
2、右边的窗口中有 form1.cs ,它有两种查看方式,可以使用 设计的方式,也可以使用代码的方式,代码的方式查看如下。
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/dd58d02c5b1b1ede8406b78d981fceecd2d90f72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/d2987775f2c4ec99219913b5c3fe1e425c6b0772.jpg)
3、设置主窗体的属性,属性工具栏也在右边,设置的属性如图所示。
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/5c2a1ad149299a88b9c9ff5167eeadbcbf2f7f72.jpg)
4、切换到设计模式下面 打开工具箱,拖一个 pictureBox控件到主窗口。同时设置pictureBox的属性,把属性名称设置为pBox
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/4759c1dae43b3b8688fb58e3185653bbf9207572.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/f9617afb960b312197bb1317dee983aee9d76d72.jpg)
5、向主窗口添加菜单,拖动菜单控件到主窗体,并设置三个子菜单item,并设置三个子菜单的属性名称。
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/54a89daee8d7592a7b35f7cc9f31dfb6336c6772.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/def72c6c576699cf9d8938d7a885e036e3915e72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/22c4fe36e29147e8963a2ec6b603bbea3f865872.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/baab2086304861437b0e5f858febf6a75e0f5372.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/f7e6410f822b74eeafe85cacda2c8cf1d9a74a72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/d9e638334884cde3f0f6415af07f860e7d754272.jpg)
6、向主窗体添加定时器,并设置定时器的间隔时间等属性
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/7a3e980e7c75e5f43d2ff9b9b1ceaad7736bbc72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/ef4c24ceaad7726bca387253bf0f64781523b972.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/64a62a0f647814237563618aaac2bbd6e0d0b272.jpg)
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#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/ba97ffd06de89a6154ff562745e8b004551bad72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/40d2d0e8b004541b06d5e592869a310e1699a672.jpg)
8、Snake的基本功能在Snake类中都实现了,下面的工作就是在界面类中调用类的函数,完成程序的最后步骤。先添加响应函数,总共有下面的响应函数3个menuitem的响应函数,前面已吲溘活口经添加;1个定时器的回调函数;2个键盘响应函数;keyup和keydown如图所示:
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/d04eec260d9a310ecc43fe9e31b842406bfea272.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/16d8f72abab84240b524d4a07ac595ee40c19e72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/6bbfdd14f1c595eea21ba09327530688902c9a72.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/4080a927ac53068876f6b27e57e8904801fc9672.jpg)
![VS2013程序设计系列-C#:[1]C#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/07c98f2ca5cadce87a921748fcf7980e5e209572.jpg)
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#贪吃蛇游戏](https://exp-picture.cdn.bcebos.com/994f412043715fdbc7faae8e468920c5270f8c72.jpg)