搜索
首页后端开发C#.Net教程C#实现贪吃蛇游戏的示例代码分析
C#实现贪吃蛇游戏的示例代码分析Jun 04, 2017 am 09:34 AM
c#贪吃蛇

这篇文章主要为大家分析了C#贪吃蛇游戏的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天无聊突发奇想做个贪吃蛇,虽然网上很多这东西了,不过自己写的感觉还行吧

贪吃蛇分析

游戏规则:

1、蛇起始长度5,每吃一个食物增加1,最大15过关

2、蛇用蓝色表示,食物用绿色,障碍物用黑色

3、当蛇碰到自己、墙壁、障碍物则游戏失败

4、方向键控制蛇的移动方向,蛇不可反方向移动,如正在向上移动,不能马上向下,只能向左、右、上运动

5、每过关一次速度提升一次

大概思路:

1、地图用网格的形式表示,蛇由方格组成,保存在list

2、1中提到了方格,方格保存的内容有,颜色,坐标,是否可以通过,是否是食物

3、向前移动一次,将前面方格添加进蛇列表中,将列表最后一个移除,若为前方格子为食物,则不移除最后一个

4、使用while循环来做整个移动

5、空格键为加速键,通过修改while循环sleep时间来实现加速

包括了3个类一个主窗体,分别是Node(用来表示方格)、Map(用来表示地图)、Serpent(用来表示蛇),另外一个主窗体。下面依次把代码贴上,基本上每个方法都有注释

代码1:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace EngorgeSerpent
{
 /// <summary>
 /// 节点
 /// </summary>
 class Node
 {
 #region 字段
 private int x;
 private int y;
 private int width = 10;
 private bool isFood = false;
 private bool isPass = true;//是否可通过
 private Color bgColor = Color.FromArgb(224, 224, 224);
 private Color foodColor = Color.Green;
 private Color hinderColor = Color.Black;
 private Color thisColor;
 private Color serpentColor = Color.Chocolate;

 #endregion
 /// <summary>
 /// 设置食物参数
 /// </summary>
 /// <param name="_isFood"></param>
 public void SetFood(bool _isFood)
 {
  IsFood = _isFood;
  if (_isFood)
  {
  ThisColor = FoodColor;

  }
  else
  {
  ThisColor = BgColor;
  }
 }

 /// <summary>
 /// 设置障碍物参数
 /// </summary>
 /// <param name="_isHinder">是否为障碍物</param>
 public void SetHinder(bool _isHinder)
 {
  IsPass =! _isHinder;
  if (_isHinder)
  {
  ThisColor = HinderColor;
  }
  else
  {
  ThisColor = BgColor;
  }
 }

 /// <summary>
 /// 设置蛇颜色
 /// </summary>
 /// <param name="_isSerpent"></param>
 public void SetSerpent(bool _isSerpent)
 {
  IsPass = !_isSerpent;
  if (_isSerpent)
  {
  ThisColor = SerpentColor;
  }
  else
  {
  ThisColor = BgColor;
  }
 }
 #region 构造函数
 public Node()
 {
  thisColor = bgColor;
 }

 /// <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 = bgColor;
  X = _x;
  Y = _y;
  Width = _width;
  IsFood = _isFood;
  IsPass = _isPass;
 }

 /// <summary>
 /// 有参构造方法
 /// </summary>
 /// <param name="_x">相对x坐标</param>
 /// <param name="_y">相对y坐标</param>
 /// <param name="_width">边长</param>
 public Node(int _x, int _y, int _width)
 {
  X = _x;
  Y = _y;
  Width = _width;
 }

 /// <summary>
 /// 有参构造方法
 /// </summary>
 /// <param name="_x">相对x坐标</param>
 /// <param name="_y">相对y坐标</param>
 public Node(int _x, int _y)
 {
  X = _x;
  Y = _y;
 }
 #endregion

 #region 属性
 /// <summary>
 /// 蛇颜色
 /// </summary>
 public Color SerpentColor
 {
  get { return serpentColor; }
 }

 /// <summary>
 /// 背景色
 /// </summary>
 public Color BgColor
 {
  get { return bgColor; }
 }

 /// <summary>
 /// 食物颜色
 /// </summary>
 public Color FoodColor
 {
  get { return foodColor; }
 }

 /// <summary>
 /// 障碍物颜色
 /// </summary>
 public Color HinderColor
 {
  get { return hinderColor; }
 }

 /// <summary>
 /// 当前颜色
 /// </summary>
 public Color ThisColor
 {
  get { return thisColor; }
  set { thisColor = value; }
 }

 /// <summary>
 /// 获取或设置相对横坐标
 /// </summary>
 public int X
 {
  get { return x; }
  set { x = value; }
 }

 /// <summary>
 /// 获取或设置相对纵坐标
 /// </summary>
 public int Y
 {
  get { return y; }
  set { y = value; }
 }

 /// <summary>
 /// 获取或设置节点边长
 /// </summary>
 public int Width
 {
  get { return width; }
  set { width = value; }
 }

 /// <summary>
 /// 获取或设置是否为食物
 /// </summary>
 public bool IsFood
 {
  get { return isFood; }
  set { isFood = value; }
 }

 /// <summary>
 /// 获取或设置是否可以通过
 /// </summary>
 public bool IsPass
 {
  get { return isPass; }
  set { isPass = value; }
 }
 #endregion
 }
}

代码2:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace EngorgeSerpent
{
 /// <summary>
 /// 地图
 /// </summary>
 class Map
 {
 /// <summary>
 /// 节点数组
 /// </summary>
 private List<List<Node>> _nodes;
 private int RowCount;
 private int ComsCount;
 private Color bgColor = Color.FromArgb(224, 224, 224);
 private System.Windows.Forms.Control MapPanel;
 Graphics g;
 /// <summary>
 /// 地图背景色 和node中背景色一致
 /// </summary>
 public Color BgColor
 {
  get { return bgColor; }
 }
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="rows">行数</param>
 /// <param name="coms">列数</param>
 public Map(int rows, int coms, System.Windows.Forms.Control c)
 {
  RowCount = rows;
  ComsCount = coms;
  MapPanel = c;
  g = c.CreateGraphics();
  _nodes = new List<List<Node>>();
  for (int i = 0; i < rows; i++)//行
  {
  List<Node> index = new List<Node>();
  for (int j = 0; j < coms; 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 = rows;
  ComsCount = coms;
  MapPanel = c;
  g = c.CreateGraphics();
  _nodes = new List<List<Node>>();
  for (int i = 0; i < coms; i++)//行
  {
  List<Node> index = new List<Node>();
  for (int j = 0; j < rows; j++)
  {
   Node node = new Node(j, i, width);
   index.Add(node);
  }
  _nodes.Add(index);
  }
 }

 /// <summary>
 /// 重新加载地图
 /// </summary>
 public void ResetMap()
 {
  for (int i = 0; i < ComsCount; i++)//行
  {
  for (int j = 0; j < RowCount; j++)
  {
   Node node = GetNode(i, j);
   node.IsPass = true;
   node.IsFood = false;  
  }
  }
 }
 /// <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 = null;
  int _x, _y;
  Random r = new Random();
  while (true)
  {
  _x = r.Next(0, RowCount);
  _y = r.Next(0, ComsCount);
  if (_nodes[_x][_y].IsPass)
  {
   break;
  }
  }
  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 = null;
  RectangleF[] rects = new RectangleF[list.Count];
  for (int i = 0; i < list.Count; i++)
  {
  Node _node = list[i];
  _node.SetHinder(true);
  _node.IsPass = false;
  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] = r;
  }
  g.FillRectangles(brush, rects);
 }

 /// <summary>
 /// 设置边界
 /// </summary>
 public void SetBorder()
 {
  //通过计算得出边界的个数是2(x+y-2)个方格

  SolidBrush brush = null;
  int borders = 2 * (ComsCount + RowCount - 2);
  RectangleF[] rects = new RectangleF[borders];
  int indexcount = 0;
  //添加顶部方格进rects列表中
  for (int i = 0; i < RowCount; 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] = r;
  indexcount++;
  }
  //添加底部方格进rects列表中
  for (int i = 0; i < RowCount; 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] = r;
  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] = r;
  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] = r;
  indexcount++;
  }
  g.FillRectangles(brush, rects);
 }
 }
}

代码3:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace EngorgeSerpent
{
 /// <summary>
 /// 蛇
 /// </summary>
 class Serpent
 {
 private List<Node> serpentList = new List<Node>();
 private Direction direction = Direction.Right;//运动方向
 private int maxCount = 15;
 private int minCount = 5;
 private System.Windows.Forms.Control MapPanel;
 Graphics g;
 /// <summary>
 /// 设置蛇长度数据
 /// </summary>
 /// <param name="maxLength">最大长度</param>
 /// <param name="minLength">最小长度</param>
 public Serpent(int maxLength, int minLength, System.Windows.Forms.Control c)
 {
  maxCount = maxLength;
  minCount = minLength;
  MapPanel = c;
  g = MapPanel.CreateGraphics();
 }

 /// <summary>
 /// 初始化蛇
 /// </summary>
 public void InitializeSerpent()
 {
  SolidBrush brush = null;
  RectangleF[] rects = new RectangleF[minCount];
  for (int i = 1; i < minCount; 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)
  return true;
  else
  return false;
 }

 /// <summary>
 /// 蛇运动方向
 /// </summary>
 public Direction Direction
 {
  get { return direction; }
  set { direction = value; }
 }

 }
 /// <summary>
 /// 运动方向
 /// </summary>
 public enum Direction
 {
 Left,
 Up,
 Right,
 Down
 }
}

代码4:

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 EngorgeSerpent
{
 public partial class MainForm : Form
 {
 public MainForm()
 {
  InitializeComponent();

 }
 List<List<Node>> maplist = new List<List<Node>>();
 Map map;
 Serpent serpent;
 Graphics g;
 int level = 1;
 /// <summary>
 /// 运行线程
 /// </summary>
 Thread Work_Thread = null;
 /// <summary>
 /// 运行线程监控值
 /// </summary>
 bool IsWork = false;
 int sleepTime = 1000;
 int thissleeptime;

 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)
 {
  IsWork = false;
  btnStop.Enabled = false;
  button3.Enabled = false;
  button2.Enabled = true;
  //map.ResetMap();
  SetMap();
 }

 /// <summary>
 /// 运行
 /// </summary>
 private void Work()
 {
  map.SetFood();//设置食物
  while (IsWork)
  {
  Node node_index;
  Node serpentHead = serpent.GetSerpentHead();
  switch (serpent.Direction)
  {
   case Direction.Left:
   node_index = map.GetNode(serpentHead.X - 1, serpentHead.Y);
   break;
   case Direction.Right:
   node_index = map.GetNode(serpentHead.X + 1, serpentHead.Y);
   break;
   case Direction.Up:
   node_index = map.GetNode(serpentHead.X, serpentHead.Y - 1); break;
   default:
   node_index = map.GetNode(serpentHead.X, serpentHead.Y + 1);
   break;
  }
  SerpentState index_move = SerpentMove(node_index);
  if (index_move == SerpentState.Error)//游戏结束
  {
   IsWork = false;
   //map.ResetMap();
   MessageBox.Show("游戏结束!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
   sleepTime = 1000;
   level = 1;
   thissleeptime = sleepTime;
   lblLevel.BeginInvoke(new MethodInvoker(delegate()
   {
   btnStop.Enabled = false;
   button3.Enabled = false;
   button2.Enabled = true;
   lblLevel.Text = "1";
   lblCount.Text = "5";
   }));
  }
  else if (index_move == SerpentState.NextLevel)
  {
   IsWork = false;
   this.lblCount.BeginInvoke(new MethodInvoker(delegate()
   {
   level += 1;
   lblLevel.Text = level.ToString();
   lblCount.Text = "5";
   }));
   sleepTime = sleepTime / 2;
   thissleeptime = sleepTime;
   SetMap();//重置地图
  }
  else
  {

   Thread.Sleep(thissleeptime);
  }
  }
  map.ResetMap();
 }

 /// <summary>
 /// 开始
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button2_Click(object sender, EventArgs e)
 {
  IsWork = false;
  btnStop.Enabled = false;
  button3.Enabled = false;
  button2.Enabled = true;
  //map.ResetMap();
  SetMap();
  thissleeptime = sleepTime;
  this.panel1.Focus();
  IsWork = true;
  this.btnStop.Enabled = true;
  this.button3.Enabled = true;
  button2.Enabled = false;
  Work_Thread = new Thread(new ThreadStart(Work));
  Work_Thread.IsBackground = true;
  Work_Thread.Start();
 }

 private void MainForm_KeyDown(object sender, KeyEventArgs e)
 {

  if (e.KeyCode == Keys.Right)
  {
  if (serpent.Direction != Direction.Left)
   serpent.Direction = Direction.Right;
  }
  else if (e.KeyCode == Keys.Left)
  {
  if (serpent.Direction != Direction.Right)
   serpent.Direction = Direction.Left;
  }
  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.Down;
  }
  else if (e.KeyCode == Keys.Space)
  {
  thissleeptime = sleepTime / 2;
  }
  else if (e.KeyCode == Keys.Escape)
  {
  if (IsWork)
  {
   this.button3.Text = "继续";
   IsWork = false;
  }

  }
 }

 /// <summary>
 /// 暂停
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button3_Click(object sender, EventArgs e)
 {
  if (!IsWork)
  {
  this.button3.Text = "暂停";
  IsWork = true;
  Work_Thread = new Thread(new ThreadStart(Work));
  Work_Thread.IsBackground = true;
  Work_Thread.Start();
  }
  else
  {
  this.button3.Text = "继续";
  IsWork = false;
  }
 }
 /// <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)
 {
  IsWork = false;
  Application.Exit();
  System.Diagnostics.Process.GetCurrentProcess().Kill();
 }



 private void btnStop_Click(object sender, EventArgs e)
 {
  // map.ResetMap();
  btnStop.Enabled = false;
  button3.Enabled = false;
  button2.Enabled = true;
  IsWork = false;
  Work_Thread.Abort();
  SetMap();
 }

 /// <summary>
 /// 移动
 /// </summary>
 /// <param name="node">将要移动到的节点</param>
 /// <returns>返回状态</returns>
 private SerpentState SerpentMove(Node node)
 {
  if (!node.IsPass)
  {
  return SerpentState.Error;
  }
  serpent.InsertNode(node);
  if (!node.IsFood)
  {
  //不是食物,则移除最后一个节点
  serpent.RemoveNode();
  }
  else
  {
  lblCount.BeginInvoke(new MethodInvoker(delegate()
  {
   this.lblCount.Text = (Convert.ToInt32(this.lblCount.Text.Trim()) + 1).ToString();
  }));
  map.SetFood();//设置食物
  }

  if (serpent.IsMax())
  {
  return SerpentState.NextLevel;
  }
  return SerpentState.Moving;
 }

 private void MainForm_KeyUp(object sender, KeyEventArgs e)
 {
  if (e.KeyCode == Keys.Space)
  {
  thissleeptime = sleepTime;
  }
 }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
  int index = 1;
  int index_count = Convert.ToInt32(comboBox1.Text);
  for (int i = 1; i < index_count; i++)
  {
  index = index * 2;
  }
  level = index_count;
  sleepTime = 1000 / index;
  thissleeptime = sleepTime;
  btnStop.Enabled = false;
  button3.Enabled = false;
  button2.Enabled = true;
  IsWork = false;

  SetMap();
  lblCount.Text = "5";
  lblLevel.Text = index_count.ToString();
  serpent.Direction = Direction.Right;

 }

 private void checkBox1_Click(object sender, EventArgs e)
 {
  comboBox1.Enabled = this.checkBox1.Checked;
 }

 }
 public enum SerpentState
 {
 Moving,
 NextLevel,
 Error
 }
}

主界面

以上是C#实现贪吃蛇游戏的示例代码分析的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使用C#编写时间序列预测算法如何使用C#编写时间序列预测算法Sep 19, 2023 pm 02:33 PM

如何使用C#编写时间序列预测算法时间序列预测是一种通过分析过去的数据来预测未来数据趋势的方法。它在很多领域,如金融、销售和天气预报中有广泛的应用。在本文中,我们将介绍如何使用C#编写时间序列预测算法,并附上具体的代码示例。数据准备在进行时间序列预测之前,首先需要准备好数据。一般来说,时间序列数据应该具有足够的长度,并且是按照时间顺序排列的。你可以从数据库或者

如何使用Redis和C#开发分布式事务功能如何使用Redis和C#开发分布式事务功能Sep 21, 2023 pm 02:55 PM

如何使用Redis和C#开发分布式事务功能引言分布式系统的开发中,事务处理是一项非常重要的功能。事务处理能够保证在分布式系统中的一系列操作要么全部成功,要么全部回滚。Redis是一种高性能的键值存储数据库,而C#是一种广泛应用于开发分布式系统的编程语言。本文将介绍如何使用Redis和C#来实现分布式事务功能,并提供具体代码示例。I.Redis事务Redis

如何实现C#中的人脸识别算法如何实现C#中的人脸识别算法Sep 19, 2023 am 08:57 AM

如何实现C#中的人脸识别算法人脸识别算法是计算机视觉领域中的一个重要研究方向,它可以用于识别和验证人脸,广泛应用于安全监控、人脸支付、人脸解锁等领域。在本文中,我们将介绍如何使用C#来实现人脸识别算法,并提供具体的代码示例。实现人脸识别算法的第一步是获取图像数据。在C#中,我们可以使用EmguCV库(OpenCV的C#封装)来处理图像。首先,我们需要在项目

Redis在C#开发中的应用:如何实现高效的缓存更新Redis在C#开发中的应用:如何实现高效的缓存更新Jul 30, 2023 am 09:46 AM

Redis在C#开发中的应用:如何实现高效的缓存更新引言:在Web开发中,缓存是提高系统性能的常用手段之一。而Redis作为一款高性能的Key-Value存储系统,能够提供快速的缓存操作,为我们的应用带来了不少便利。本文将介绍如何在C#开发中使用Redis,实现高效的缓存更新。Redis的安装与配置在开始之前,我们需要先安装Redis并进行相应的配置。你可以

如何使用C#编写动态规划算法如何使用C#编写动态规划算法Sep 20, 2023 pm 04:03 PM

如何使用C#编写动态规划算法摘要:动态规划是求解最优化问题的一种常用算法,适用于多种场景。本文将介绍如何使用C#编写动态规划算法,并提供具体的代码示例。一、什么是动态规划算法动态规划(DynamicProgramming,简称DP)是一种用来求解具有重叠子问题和最优子结构性质的问题的算法思想。动态规划将问题分解成若干个子问题来求解,通过记录每个子问题的解,

如何实现C#中的图像压缩算法如何实现C#中的图像压缩算法Sep 19, 2023 pm 02:12 PM

如何实现C#中的图像压缩算法摘要:图像压缩是图像处理领域中的一个重要研究方向,本文将介绍在C#中实现图像压缩的算法,并给出相应的代码示例。引言:随着数字图像的广泛应用,图像压缩成为了图像处理中的重要环节。压缩能够减小存储空间和传输带宽,并能提高图像处理的效率。在C#语言中,我们可以通过使用各种图像压缩算法来实现对图像的压缩。本文将介绍两种常见的图像压缩算法:

C#开发中如何处理跨域请求和安全性问题C#开发中如何处理跨域请求和安全性问题Oct 08, 2023 pm 09:21 PM

C#开发中如何处理跨域请求和安全性问题在现代的网络应用开发中,跨域请求和安全性问题是开发人员经常面临的挑战。为了提供更好的用户体验和功能,应用程序经常需要与其他域或服务器进行交互。然而,浏览器的同源策略导致了这些跨域请求被阻止,因此需要采取一些措施来处理跨域请求。同时,为了保证数据的安全性,开发人员还需要考虑一些安全性问题。本文将探讨C#开发中如何处理跨域请

如何实现C#中的遗传算法如何实现C#中的遗传算法Sep 19, 2023 pm 01:07 PM

如何在C#中实现遗传算法引言:遗传算法是一种模拟自然选择和基因遗传机制的优化算法,其主要思想是通过模拟生物进化的过程来搜索最优解。在计算机科学领域,遗传算法被广泛应用于优化问题的解决,例如机器学习、参数优化、组合优化等。本文将介绍如何在C#中实现遗传算法,并提供具体的代码示例。一、遗传算法的基本原理遗传算法通过使用编码表示解空间中的候选解,并利用选择、交叉和

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境