贪吃蛇游戏设计C#主窗口设计

C#贪吃蛇游戏实现分析
转载 &更新时间:日 16:49:12 & 作者:冰封一夏
这篇文章主要为大家分析了C#贪吃蛇游戏的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
今天无聊突发奇想做个贪吃蛇,虽然网上很多这东西了,不过自己写的感觉还行吧
贪吃蛇分析
游戏规则:
1、蛇起始长度5,每吃一个食物增加1,最大15过关
2、蛇用蓝色表示,食物用绿色,障碍物用黑色
3、当蛇碰到自己、墙壁、障碍物则游戏失败
4、方向键控制蛇的移动方向,蛇不可反方向移动,如正在向上移动,不能马上向下,只能向左、右、上运动
5、每过关一次速度提升一次
大概思路:
1、地图用网格的形式表示,蛇由方格组成,保存在list中
2、1中提到了方格,方格保存的内容有,颜色,坐标,是否可以通过,是否是食物
3、向前移动一次,将前面方格添加进蛇列表中,将列表最后一个移除,若为前方格子为食物,则不移除最后一个
4、使用while死循环来做整个移动
5、空格键为加速键,通过修改while循环sleep时间来实现加速
包括了3个类一个主窗体,分别是Node(用来表示方格)、Map(用来表示地图)、Serpent(用来表示蛇),另外一个主窗体。下面依次把代码贴上,基本上每个方法都有注释
using System.Collections.G
using System.T
using System.D
namespace EngorgeSerpent
/// &summary&
/// &/summary&
class Node
#region 字段
private int width = 10;
private bool isFood =
private bool isPass =//是否可通过
private Color bgColor = Color.FromArgb(224, 224, 224);
private Color foodColor = Color.G
private Color hinderColor = Color.B
private Color thisC
private Color serpentColor = Color.C
#endregion
/// &summary&
/// 设置食物参数
/// &/summary&
/// &param name="_isFood"&&/param&
public void SetFood(bool _isFood)
IsFood = _isF
if (_isFood)
ThisColor = FoodC
ThisColor = BgC
/// &summary&
/// 设置障碍物参数
/// &/summary&
/// &param name="_isHinder"&是否为障碍物&/param&
public void SetHinder(bool _isHinder)
IsPass =! _isH
if (_isHinder)
ThisColor = HinderC
ThisColor = BgC
/// &summary&
/// 设置蛇颜色
/// &/summary&
/// &param name="_isSerpent"&&/param&
public void SetSerpent(bool _isSerpent)
IsPass = !_isS
if (_isSerpent)
ThisColor = SerpentC
ThisColor = BgC
#region 构造函数
public Node()
thisColor = bgC
/// &summary&
/// 有参构造方法
/// &/summary&
/// &param name="_x"&相对x坐标&/param&
/// &param name="_y"&相对y坐标&/param&
/// &param name="_width"&边长&/param&
/// &param name="_isFood"&是否是食物&/param&
/// &param name="_isPass"&是否可通过&/param&
public Node(int _x, int _y, int _width, bool _isFood, bool _isPass)
thisColor = bgC
IsFood = _isF
IsPass = _isP
/// &summary&
/// 有参构造方法
/// &/summary&
/// &param name="_x"&相对x坐标&/param&
/// &param name="_y"&相对y坐标&/param&
/// &param name="_width"&边长&/param&
public Node(int _x, int _y, int _width)
/// &summary&
/// 有参构造方法
/// &/summary&
/// &param name="_x"&相对x坐标&/param&
/// &param name="_y"&相对y坐标&/param&
public Node(int _x, int _y)
#endregion
#region 属性
/// &summary&
/// 蛇颜色
/// &/summary&
public Color SerpentColor
get { return serpentC }
/// &summary&
/// 背景色
/// &/summary&
public Color BgColor
get { return bgC }
/// &summary&
/// 食物颜色
/// &/summary&
public Color FoodColor
get { return foodC }
/// &summary&
/// 障碍物颜色
/// &/summary&
public Color HinderColor
get { return hinderC }
/// &summary&
/// 当前颜色
/// &/summary&
public Color ThisColor
get { return thisC }
set { thisColor = }
/// &summary&
/// 获取或设置相对横坐标
/// &/summary&
public int X
set { x = }
/// &summary&
/// 获取或设置相对纵坐标
/// &/summary&
public int Y
set { y = }
/// &summary&
/// 获取或设置节点边长
/// &/summary&
public int Width
set { width = }
/// &summary&
/// 获取或设置是否为食物
/// &/summary&
public bool IsFood
get { return isF }
set { isFood = }
/// &summary&
/// 获取或设置是否可以通过
/// &/summary&
public bool IsPass
get { return isP }
set { isPass = }
#endregion
using System.Collections.G
using System.T
using System.D
namespace EngorgeSerpent
/// &summary&
/// &/summary&
/// &summary&
/// 节点数组
/// &/summary&
private List&List&Node&& _
private int RowC
private int ComsC
private Color bgColor = Color.FromArgb(224, 224, 224);
private System.Windows.Forms.Control MapP
/// &summary&
/// 地图背景色 和node中背景色一致
/// &/summary&
public Color BgColor
get { return bgC }
/// &summary&
/// 构造方法
/// &/summary&
/// &param name="rows"&行数&/param&
/// &param name="coms"&列数&/param&
public Map(int rows, int coms, System.Windows.Forms.Control c)
RowCount =
ComsCount =
MapPanel =
g = c.CreateGraphics();
_nodes = new List&List&Node&&();
for (int i = 0; i & i++)//行
List&Node& index = new List&Node&();
for (int j = 0; j & j++)
Node node = new Node(j, i);
index.Add(node);
_nodes.Add(index);
/// &summary&
/// 构造方法
/// &/summary&
/// &param name="rows"&行数&/param&
/// &param name="coms"&列数&/param&
/// &param name="width"&节点宽度&/param&
public Map(int rows, int coms, int width, System.Windows.Forms.Control c)
RowCount =
ComsCount =
MapPanel =
g = c.CreateGraphics();
_nodes = new List&List&Node&&();
for (int i = 0; i & i++)//行
List&Node& index = new List&Node&();
for (int j = 0; j & j++)
Node node = new Node(j, i, width);
index.Add(node);
_nodes.Add(index);
/// &summary&
/// 重新加载地图
/// &/summary&
public void ResetMap()
for (int i = 0; i & ComsC i++)//行
for (int j = 0; j & RowC j++)
Node node = GetNode(i, j);
node.IsPass =
node.IsFood =
/// &summary&
/// 获得节点
/// &/summary&
/// &param name="x"&&/param&
/// &param name="y"&&/param&
/// &returns&&/returns&
public Node GetNode(int x, int y)
return _nodes[y][x];
/// &summary&
/// 设置食物
/// &/summary&
public void SetFood()
SolidBrush brush =
int _x, _y;
Random r = new Random();
while (true)
_x = r.Next(0, RowCount);
_y = r.Next(0, ComsCount);
if (_nodes[_x][_y].IsPass)
Node nodeindex = _nodes[_x][_y];
nodeindex.SetFood(true);
brush = new SolidBrush(nodeindex.FoodColor);
RectangleF[] rects = { new RectangleF(nodeindex.X * nodeindex.Width, nodeindex.Y * nodeindex.Width, nodeindex.Width, nodeindex.Width) };
g.FillRectangles(brush, rects);
/// &summary&
/// 设置障碍物
/// &/summary&
/// &param name="list"&&/param&
public void SetHinder(List&Node& list)
SolidBrush brush =
RectangleF[] rects = new RectangleF[list.Count];
for (int i = 0; i & list.C i++)
Node _node = list[i];
_node.SetHinder(true);
_node.IsPass =
if (brush == null)
brush = new SolidBrush(_node.HinderColor);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[i] =
g.FillRectangles(brush, rects);
/// &summary&
/// 设置边界
/// &/summary&
public void SetBorder()
//通过计算得出边界的个数是2(x+y-2)个方格
SolidBrush brush =
int borders = 2 * (ComsCount + RowCount - 2);
RectangleF[] rects = new RectangleF[borders];
int indexcount = 0;
//添加顶部方格进rects列表中
for (int i = 0; i & RowC i++)
Node _node = _nodes[i][0];
_node.SetHinder(true);
if (brush == null)
brush = new SolidBrush(_node.HinderColor);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
//添加底部方格进rects列表中
for (int i = 0; i & RowC i++)
Node _node = _nodes[i][ComsCount - 1];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
//添加左侧方格进列表,因为左侧最上面以及最下面的两个方格已经添加进去,这里不需要重复添加
for (int i = 1; i & ComsCount - 1; i++)
Node _node = _nodes[0][i];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
//添加右侧方格进列表,因为右侧最上面以及最下面两个方格已经添加进去,这里不需要重复添加
for (int i = 1; i & ComsCount - 1; i++)
Node _node = _nodes[RowCount - 1][i];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
g.FillRectangles(brush, rects);
using System.Collections.G
using System.T
using System.D
namespace EngorgeSerpent
/// &summary&
/// &/summary&
class Serpent
private List&Node& serpentList = new List&Node&();
private Direction direction = Direction.R//运动方向
private int maxCount = 15;
private int minCount = 5;
private System.Windows.Forms.Control MapP
/// &summary&
/// 设置蛇长度数据
/// &/summary&
/// &param name="maxLength"&最大长度&/param&
/// &param name="minLength"&最小长度&/param&
public Serpent(int maxLength, int minLength, System.Windows.Forms.Control c)
maxCount = maxL
minCount = minL
MapPanel =
g = MapPanel.CreateGraphics();
/// &summary&
/// 初始化蛇
/// &/summary&
public void InitializeSerpent()
SolidBrush brush =
RectangleF[] rects = new RectangleF[minCount];
for (int i = 1; i & minC i++)
Node indexnode = new Node(i, 1);
indexnode.SetSerpent(true);//设置蛇颜色
serpentList.Insert(0, indexnode);
if (brush == null)
brush = new SolidBrush(indexnode.SerpentColor);
rects[i] = new RectangleF(indexnode.X * indexnode.Width, indexnode.Y * indexnode.Width, indexnode.Width, indexnode.Width);
g.FillRectangles(brush, rects);
/// &summary&
/// 插入一个
/// &/summary&
/// &param name="node"&&/param&
public void InsertNode(Node node)
serpentList.Insert(0, node);
node.SetSerpent(true);
SolidBrush brush = new SolidBrush(node.SerpentColor);
RectangleF rect = new RectangleF(node.X * node.Width, node.Y * node.Width, node.Width, node.Width);
g.FillRectangle(brush, rect);
/// &summary&
/// 移除尾巴
/// &/summary&
/// &param name="node"&&/param&
public void RemoveNode()
Node node = serpentList[serpentList.Count - 1];
serpentList.Remove(node);
node.SetSerpent(false);
SolidBrush brush = new SolidBrush(node.BgColor);
RectangleF rect = new RectangleF(node.X * node.Width, node.Y * node.Width, node.Width, node.Width);
g.FillRectangle(brush, rect);
/// &summary&
/// 获取蛇头
/// &/summary&
/// &returns&蛇头方格&/returns&
public Node GetSerpentHead()
return serpentList[0];
/// &summary&
/// 蛇是否最长
/// &/summary&
/// &returns&&/returns&
public bool IsMax()
if (serpentList.Count == maxCount)
/// &summary&
/// 蛇运动方向
/// &/summary&
public Direction Direction
set { direction = }
/// &summary&
/// 运动方向
/// &/summary&
public enum Direction
using System.Collections.G
using System.ComponentM
using System.D
using System.D
using System.T
using System.Windows.F
using System.T
namespace EngorgeSerpent
public partial class MainForm : Form
public MainForm()
InitializeComponent();
List&List&Node&& maplist = new List&List&Node&&();
int level = 1;
/// &summary&
/// 运行线程
/// &/summary&
Thread Work_Thread =
/// &summary&
/// 运行线程监控值
/// &/summary&
bool IsWork =
int sleepTime = 1000;
private void MainForm_Load(object sender, EventArgs e)
g = this.panel1.CreateGraphics();
map = new Map(40, 30, this.panel1);//这里可以对画布进行下大小设置 此处偷懒省略
LoadMapList();//加载障碍物列表
/// &summary&
/// 默认将地图设置为30*40
/// &/summary&
private void SetMap()
map.ResetMap();
g.Clear(map.BgColor);
map.SetBorder();//设置边界
List&Node& hiderList = GetHider();//获取障碍物列表
map.SetHinder(hiderList);//设置障碍物
SetSerpent();//初始化蛇
/// &summary&
/// 设置蛇
/// &/summary&
private void SetSerpent()
serpent = new Serpent(15, 5, this.panel1);
serpent.InitializeSerpent();//初始化蛇
/// &summary&
/// 获取地图障碍物列表 以增加不同级别难度
/// &/summary&
private void LoadMapList()
//目前分为5个级别
//第一级别
List&Node& hiderList1 = new List&Node&();
for (int i = 15; i & 25; i++)
hiderList1.Add(map.GetNode(i, 15));
hiderList1.Add(map.GetNode(15, i));
maplist.Add(hiderList1);
//第二级别
List&Node& hiderList2 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList2.Add(map.GetNode(i, 15));
hiderList2.Add(map.GetNode(15, i));
maplist.Add(hiderList2);
//第三级别
List&Node& hiderList3 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList3.Add(map.GetNode(i, 15));
hiderList3.Add(map.GetNode(15, i));
hiderList3.Add(map.GetNode(i, 25));
maplist.Add(hiderList3);
//第四级别
List&Node& hiderList4 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList4.Add(map.GetNode(i, 25));
hiderList4.Add(map.GetNode(i, 15));
hiderList4.Add(map.GetNode(15, i));
hiderList4.Add(map.GetNode(i, 7));
maplist.Add(hiderList4);
//第五级别
List&Node& hiderList5 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList5.Add(map.GetNode(i, 25));
hiderList5.Add(map.GetNode(i, 15));
hiderList5.Add(map.GetNode(15, i));
hiderList5.Add(map.GetNode(i, 7));
hiderList5.Add(map.GetNode(i, 35));
for (int i = 12; i & 20; i++)
hiderList5.Add(map.GetNode(7, i));
hiderList5.Add(map.GetNode(25, i));
maplist.Add(hiderList5);
/// &summary&
/// 获取障碍物列表
/// &/summary&
/// &returns&&/returns&
private List&Node& GetHider()
//这里可以添加多个地图,当级别改变时需要重新加载
return maplist[level - 1];
/// &summary&
/// 重置地图
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="e"&&/param&
private void btnResetMap_Click(object sender, EventArgs e)
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
//map.ResetMap();
/// &summary&
/// &/summary&
private void Work()
map.SetFood();//设置食物
while (IsWork)
Node node_
Node serpentHead = serpent.GetSerpentHead();
switch (serpent.Direction)
case Direction.Left:
node_index = map.GetNode(serpentHead.X - 1, serpentHead.Y);
case Direction.Right:
node_index = map.GetNode(serpentHead.X + 1, serpentHead.Y);
case Direction.Up:
node_index = map.GetNode(serpentHead.X, serpentHead.Y - 1);
node_index = map.GetNode(serpentHead.X, serpentHead.Y + 1);
SerpentState index_move = SerpentMove(node_index);
if (index_move == SerpentState.Error)//游戏结束
//map.ResetMap();
MessageBox.Show("游戏结束!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
sleepTime = 1000;
level = 1;
thissleeptime = sleepT
lblLevel.BeginInvoke(new MethodInvoker(delegate()
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
lblLevel.Text = "1";
lblCount.Text = "5";
else if (index_move == SerpentState.NextLevel)
this.lblCount.BeginInvoke(new MethodInvoker(delegate()
level += 1;
lblLevel.Text = level.ToString();
lblCount.Text = "5";
sleepTime = sleepTime / 2;
thissleeptime = sleepT
SetMap();//重置地图
Thread.Sleep(thissleeptime);
map.ResetMap();
/// &summary&
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="e"&&/param&
private void button2_Click(object sender, EventArgs e)
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
//map.ResetMap();
thissleeptime = sleepT
this.panel1.Focus();
this.btnStop.Enabled =
this.button3.Enabled =
button2.Enabled =
Work_Thread = new Thread(new ThreadStart(Work));
Work_Thread.IsBackground =
Work_Thread.Start();
private void MainForm_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Right)
if (serpent.Direction != Direction.Left)
serpent.Direction = Direction.R
else if (e.KeyCode == Keys.Left)
if (serpent.Direction != Direction.Right)
serpent.Direction = Direction.L
else if (e.KeyCode == Keys.Up)
if (serpent.Direction != Direction.Down)
serpent.Direction = Direction.Up;
else if (e.KeyCode == Keys.Down)
if (serpent.Direction != Direction.Up)
serpent.Direction = Direction.D
else if (e.KeyCode == Keys.Space)
thissleeptime = sleepTime / 2;
else if (e.KeyCode == Keys.Escape)
if (IsWork)
this.button3.Text = "继续";
/// &summary&
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="e"&&/param&
private void button3_Click(object sender, EventArgs e)
if (!IsWork)
this.button3.Text = "暂停";
Work_Thread = new Thread(new ThreadStart(Work));
Work_Thread.IsBackground =
Work_Thread.Start();
this.button3.Text = "继续";
/// &summary&
/// &/summary&
/// &param name="sender"&&/param&
/// &param name="e"&&/param&
private void button4_Click(object sender, EventArgs e)
this.Close();
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
Application.Exit();
System.Diagnostics.Process.GetCurrentProcess().Kill();
private void btnStop_Click(object sender, EventArgs e)
// map.ResetMap();
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
Work_Thread.Abort();
/// &summary&
/// &/summary&
/// &param name="node"&将要移动到的节点&/param&
/// &returns&返回状态&/returns&
private SerpentState SerpentMove(Node node)
if (!node.IsPass)
return SerpentState.E
serpent.InsertNode(node);
if (!node.IsFood)
//不是食物,则移除最后一个节点
serpent.RemoveNode();
lblCount.BeginInvoke(new MethodInvoker(delegate()
this.lblCount.Text = (Convert.ToInt32(this.lblCount.Text.Trim()) + 1).ToString();
map.SetFood();//设置食物
if (serpent.IsMax())
return SerpentState.NextL
return SerpentState.M
private void MainForm_KeyUp(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Space)
thissleeptime = sleepT
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
int index = 1;
int index_count = Convert.ToInt32(comboBox1.Text);
for (int i = 1; i & index_ i++)
index = index * 2;
level = index_
sleepTime = 1000 /
thissleeptime = sleepT
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
lblCount.Text = "5";
lblLevel.Text = index_count.ToString();
serpent.Direction = Direction.R
private void checkBox1_Click(object sender, EventArgs e)
comboBox1.Enabled = this.checkBox1.C
public enum SerpentState
NextLevel,
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具C#窗体贪吃蛇的游戏界面是怎么做的?就是贪吃蛇那个游戏的每个小方框是怎么做出来的?求高手_百度知道
C#窗体贪吃蛇的游戏界面是怎么做的?就是贪吃蛇那个游戏的每个小方框是怎么做出来的?求高手
我们界面期末作业做一个作品出来,我就想做一个贪吃蛇游戏,现在才开始制作,不知道那个贪吃蛇的游戏界面是怎么制作的,求高手,谢谢啦
我有更好的答案
直接在窗体上调用GDI函数画x*y个rectangle就行了.蛇可以用不同的颜色来画.与俄罗斯方块是差不多的.
采纳率:34%
winform界面呗 .. 里面应该是Bitmap类呗 然后你再查查
去这个地址,可以下一个贪吃蛇的做法
为您推荐:
其他类似问题
贪吃蛇的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。您的位置: >
贪吃蛇小游戏,C#编写的游戏实例源码下载
贪吃蛇小游戏,C#编写的游戏实例源码
运行环境:windows/vs2008
更新时间: 20:12:53
论坛转帖:
有无插件:无插件
分&享&到:
  贪吃蛇小游戏,使用C#编写的实例源码,界面窗体做的不错,同时它还具备基本的游戏设置,比如蛇体颜色设置、食物颜色设置、游戏难度设置,分菜鸟、大鸟、老鸟、自虐四个级别等。游戏操作方面:ASDW为四方向控制键,空格暂停游戏。
点击链接开始下载豆丁微信公众号
君,已阅读到文档的结尾了呢~~
用C#做的贪吃蛇游戏的设计论文设计论文,小游戏的,贪吃蛇,游戏的,贪食蛇,贪吃蛇吧
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
用C#做的贪吃蛇游戏的设计论文
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='http://www.docin.com/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 贪吃蛇游戏设计报告 的文章

 

随机推荐