游戏人生Silverlight:贪吃蛇

开发 后端
贪吃蛇的每一段为 16×16 像素,场景为 640×480 像素,也就说网格为 40×30 个,每个网格的边长为 16。食物的出现位置以及贪吃蛇的运动方向的改变都要在相关的网格内进行。

介绍

使用 Silverlight 3.0(c#) 开发一个贪吃蛇游戏

玩法

W/S/A/D 或 ↑/↓/←/→ 控制蛇的移动

截图

思路

1、贪吃蛇的每一段为 16×16 像素,场景为 640×480 像素,也就说网格为 40×30 个,每个网格的边长为 16

2、食物的出现位置以及贪吃蛇的运动方向的改变都要在相关的网格内进行

3、贪吃蛇的运动用即时运算的方法计算,当贪吃蛇运动到网格内(蛇某一段的像素位置%网格的边长<蛇在某时间单位下的移动偏移量)时做如下工作:修正蛇的位置使其正好在网格内,更新蛇的每一段的运动方向,判断是否吃到了食物、是否发生了碰撞等

4、贪吃蛇的每一段的运动方向的修改:蛇头的运动方向根据用户的操作改变,蛇的每一段的运动方向设置为此段的前一段的运动方向(计算时要从尾部向头部逐段计算)(注:运动方向的改变要在蛇移动到网格内时进行。其中如果蛇的某一段移动到了网格内,则表明其它各段都在网格内)

下面我们看看他的关键代码都有哪些。

#p#

关键代码

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Threading;  
  14. namespace YYSnake.Core  
  15. {  
  16.     public partial class Main : UserControl  
  17.     {  
  18.         private int _columns; // 网格列数  
  19.         private int _rows; // 网格行数  
  20.         private Dictionary<Body, CellPoint> _bodies = new Dictionary<Body, CellPoint>(); // 贪吃蛇每一段的集合  
  21.         private Dictionary<Bean, CellPoint> _beans = new Dictionary<Bean, CellPoint>(); // 豆的集合  
  22.         private Dictionary<Skin, CellPoint> _skins = new Dictionary<Skin, CellPoint>(); // 蜕下来的皮的集合  
  23.         private List<CellPoint> _emptyCells = new List<CellPoint>(); // 空网格的集合  
  24.         private bool _enabled = false// 游戏是否运行  
  25.         private double _dt = 0.01// 多少毫秒计算一次  
  26.         private int _decimals = 1// 计算小数时所保留的小数位  
  27.         private double _speed = 80// 蛇的运行速度  
  28.         private Direction _moveDirection = Direction.Up; // 蛇的运行方向  
  29.         private int _selfLength = 5// 蛇的最小长度  
  30.         private int _beansCount = 5// 豆的***出现数量  
  31.         private int _ateCapacity = 10// 食量(超过则蜕皮)  
  32.         private bool _needRaiseAteEvent = false// 在“蛇头所处位置进入了网格点区域内”时是否需要触发吃豆事件  
  33.         private int _needBeansCount = 0// 还需要增加的豆的数量  
  34.         Random _random = new Random();  
  35.         public Main()  
  36.         {  
  37.             InitializeComponent();  
  38.             this.Loaded += new RoutedEventHandler(Main_Loaded);  
  39.         }  
  40.         void Main_Loaded(object sender, RoutedEventArgs e)  
  41.         {  
  42.             this.Width = App.Width; // 640  
  43.             this.Height = App.Height; // 480  
  44.  
  45.             _columns = (int)(Width / App.CellSize); // 40  
  46.             _rows = (int)(Height / App.CellSize); // 30  
  47.  
  48.             // 防止动画飞出去  
  49.             RectangleGeometry rg = new RectangleGeometry();  
  50.             rg.Rect = new Rect(00, App.Width, App.Height);  
  51.             LayoutRoot.Clip = rg;  
  52.             bg.Width = App.Width;  
  53.             bg.Height = App.Height;  
  54.             ripple.RippleBackground = bg;  
  55.             CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);  
  56.         }  
  57.         /// <summary>  
  58.         /// 初始化  
  59.         /// </summary>  
  60.         public void Init()  
  61.         {  
  62.             _enabled = false;  
  63.             canvasBean.Children.Clear();  
  64.             canvasSnake.Children.Clear();  
  65.             canvasSkin.Children.Clear();  
  66.             _beans.Clear();  
  67.             _bodies.Clear();  
  68.             _skins.Clear();  
  69.             _emptyCells.Clear();  
  70.             for (int i = 0; i < _columns; i++)  
  71.             {  
  72.                 for (int j = 0; j < _rows; j++)  
  73.                 {  
  74.                     _emptyCells.Add(new CellPoint(i, j));  
  75.                 }  
  76.             }  
  77.             _moveDirection = Direction.Up;  
  78.  
  79.             InitSnake();  
  80.         }  
  81.  
  82.         /// <summary>  
  83.         /// 蛇的初始化  
  84.         /// </summary>  
  85.         private void InitSnake()  
  86.         {  
  87.             InitHead();  
  88.  
  89.             for (int i = 0; i < _selfLength - 1; i++)  
  90.                 AddTail();  
  91.  
  92.             for (int i = 0; i < _beansCount; i++)  
  93.                 AddBean();  
  94.         }  
  95.         /// <summary>  
  96.         /// 蛇头的初始化  
  97.         /// </summary>  
  98.         private void InitHead()  
  99.         {  
  100.             Body head = new Body(BodyType.Head);  
  101.             head.MoveDirection = _moveDirection;  
  102.             CellPoint point = new CellPoint((int)(_columns / 2), (int)(_rows / 2));  
  103.             head.SetValue(Canvas.LeftProperty, point.X * App.CellSize);  
  104.             head.SetValue(Canvas.TopProperty, point.Y * App.CellSize);  
  105.             _bodies.Add(head, point);  
  106.             canvasSnake.Children.Add(head);  
  107.         }  
  108.         /// <summary>  
  109.         /// 增加一个尾巴  
  110.         /// </summary>  
  111.         private void AddTail()  
  112.         {  
  113.             var prevBody = _bodies.Last().Key;  
  114.             var prevBodyPoint = _bodies.Last().Value;  
  115.             Body tail = new Body(BodyType.Tail);  
  116.             tail.MoveDirection = prevBody.MoveDirection;  
  117.             CellPoint tailPoint = new CellPoint(prevBodyPoint.X, prevBodyPoint.Y);  
  118.             switch (prevBody.MoveDirection)  
  119.             {  
  120.                 case Direction.Up:  
  121.                     tail.SetValue(Canvas.LeftProperty, (double)prevBody.GetValue(Canvas.LeftProperty));  
  122.                     tail.SetValue(Canvas.TopProperty, (double)prevBody.GetValue(Canvas.TopProperty) + App.CellSize);  
  123.                     tailPoint.Y++;  
  124.                     break;  
  125.                 case Direction.Down:  
  126.                     tail.SetValue(Canvas.LeftProperty, (double)prevBody.GetValue(Canvas.LeftProperty));  
  127.                     tail.SetValue(Canvas.TopProperty, (double)prevBody.GetValue(Canvas.TopProperty) - App.CellSize);  
  128.                     tailPoint.Y--;  
  129.                     break;  
  130.                 case Direction.Left:  
  131.                     tail.SetValue(Canvas.LeftProperty, (double)prevBody.GetValue(Canvas.LeftProperty) + App.CellSize);  
  132.                     tail.SetValue(Canvas.TopProperty, (double)prevBody.GetValue(Canvas.TopProperty));  
  133.                     tailPoint.X++;  
  134.                     break;  
  135.                 case Direction.Right:  
  136.                     tail.SetValue(Canvas.LeftProperty, (double)prevBody.GetValue(Canvas.LeftProperty) - App.CellSize);  
  137.                     tail.SetValue(Canvas.TopProperty, (double)prevBody.GetValue(Canvas.TopProperty));  
  138.                     tailPoint.X--;  
  139.                     break;  
  140.             }  
  141.  
  142.             tailPoint = CorrectCellPoint(tailPoint);  
  143.             _bodies.Add(tail, tailPoint);  
  144.             canvasSnake.Children.Add(tail);  
  145.         }  
  146.         /// <summary>  
  147.         /// 增加一个豆  
  148.         /// </summary>  
  149.         private void AddBean()  
  150.         {  
  151.             if (_needBeansCount < _beansCount)  
  152.                 _needBeansCount++;  
  153.         }  
  154.         private DateTime _prevAddBeanDateTime = DateTime.Now;  
  155.         /// <summary>  
  156.         /// 生成豆  
  157.         /// </summary>  
  158.         void UpdateBean()  
  159.         {  
  160.             if (_needBeansCount > 0 && (DateTime.Now - _prevAddBeanDateTime).TotalSeconds > 3)  
  161.             {  
  162.                 List<CellPoint> emptyCells = GetEmptyCells();  
  163.                 if (emptyCells.Count == 0)  
  164.                 {  
  165.                     GameOver(this, EventArgs.Empty);  
  166.                     return;  
  167.                 }  
  168.                 CellPoint point = emptyCells[_random.Next(0, emptyCells.Count)];  
  169.                 Bean bean = new Bean();  
  170.                 bean.SetValue(Canvas.LeftProperty, point.X * App.CellSize);  
  171.                 bean.SetValue(Canvas.TopProperty, point.Y * App.CellSize);  
  172.                 _beans.Add(bean, point);  
  173.                 canvasBean.Children.Add(bean);  
  174.                 bean.ani.Completed += delegate  
  175.                 {  
  176.                     ripple.ShowRipple(new Point(point.X * App.CellSize + App.CellSize / 2, point.Y * App.CellSize + App.CellSize / 2));  
  177.                     player.PlayDrop();  
  178.                 };  
  179.                 _needBeansCount--;  
  180.                 _prevAddBeanDateTime = DateTime.Now;  
  181.             }  
  182.         }  
  183.         private DateTime _prevDateTime = DateTime.Now;  
  184.         private double _leftoverLength = 0d;  
  185.         void CompositionTarget_Rendering(object sender, EventArgs e)  
  186.         {  
  187.             double length = (DateTime.Now - _prevDateTime).TotalSeconds + _leftoverLength;  
  188.             while (length > _dt)  
  189.             {  
  190.                 Update();  
  191.                 length -= _dt;  
  192.             }  
  193.             _leftoverLength = length;  
  194.             _prevDateTime = DateTime.Now;  
  195.         }  
  196.         /// <summary>  
  197.         /// 即时计算  
  198.         /// </summary>  
  199.         private void Update()  
  200.         {  
  201.             if (!_enabled)  
  202.                 return;  
  203.             double offset = Math.Round(_speed * _dt, _decimals);  
  204.             // 蛇头所处位置进入了网格点区域内  
  205.             if (Math.Abs(Math.Round((double)_bodies.First().Key.GetValue(Canvas.TopProperty) % App.CellSize, _decimals)) < offset && Math.Abs(Math.Round((double)_bodies.First().Key.GetValue(Canvas.LeftProperty) % App.CellSize, _decimals)) < offset)  
  206.             {  
  207.                 UpdateDirection();  
  208.                 CorrectPosition();  
  209.                 UpdateBodyCell();  
  210.                 CheckEat();  
  211.                 CheckSkin();  
  212.                 CheckCollision();  
  213.                 UpdateBean();  
  214.                 if (_needRaiseAteEvent)  
  215.                 {  
  216.                     Ate(this.Ate, EventArgs.Empty);  
  217.                     _needRaiseAteEvent = false;  
  218.                 }  
  219.             }  
  220.             UpdatePosition();  
  221.         }  
  222.         /// <summary>  
  223.         /// 蜕皮  
  224.         /// </summary>  
  225.         private void CheckSkin()  
  226.         {  
  227.             if (_bodies.Count >= _ateCapacity + _selfLength)  
  228.                 AddSkin(_ateCapacity);  
  229.         }  
  230.         /// <summary>  
  231.         /// 碰撞检测  
  232.         /// </summary>  
  233.         private void CheckCollision()  
  234.         {  
  235.             if (_skins.Any(p => p.Value == _bodies.First().Value) || _bodies.Where(p => p.Key.BodyType == BodyType.Tail).Any(p => p.Value == _bodies.First().Value))  
  236.             {  
  237.                 _enabled = false;  
  238.                 player.PlayOver();  
  239.  
  240.                 GameOver(this, EventArgs.Empty);  
  241.             }  
  242.         }  
  243.         /// <summary>  
  244.         /// 吃豆  
  245.         /// </summary>  
  246.         private void CheckEat()  
  247.         {  
  248.             // 是否有被吃的豆  
  249.             var bean = _beans.FirstOrDefault(p => p.Value == _bodies.First().Value).Key;  
  250.             if (bean != null)  
  251.             {  
  252.                 _beans.Remove(bean);  
  253.                 canvasBean.Children.Remove(bean);  
  254.                 player.PlayEat();  
  255.                 AddTail();  
  256.                 AddBean();  
  257.                 _needRaiseAteEvent = true;  
  258.             }  
  259.         }  
  260.         /// <summary>  
  261.         /// 更新蛇的每一段的运动方向  
  262.         /// </summary>  
  263.         private void UpdateDirection()  
  264.         {  
  265.             for (int i = _bodies.Count - 1; i > -1; i--)  
  266.             {  
  267.                 if (i == 0)  
  268.                     _bodies.ElementAt(i).Key.MoveDirection = _moveDirection;  
  269.                 else 
  270.                     _bodies.ElementAt(i).Key.MoveDirection = _bodies.ElementAt(i - 1).Key.MoveDirection;  
  271.             }  
  272.         }  
  273.         /// <summary>  
  274.         /// 更新蛇的每一段的位置  
  275.         /// </summary>  
  276.         private void UpdatePosition()  
  277.         {  
  278.             double offset = Math.Round(_speed * _dt, _decimals);  
  279.             foreach (var body in _bodies.Keys)  
  280.             {  
  281.                 if (body.MoveDirection == Direction.Up)  
  282.                     body.SetValue(Canvas.TopProperty, Math.Round((double)body.GetValue(Canvas.TopProperty) - offset, _decimals));  
  283.                 else if (body.MoveDirection == Direction.Down)  
  284.                     body.SetValue(Canvas.TopProperty, Math.Round((double)body.GetValue(Canvas.TopProperty) + offset, _decimals));  
  285.                 else if (body.MoveDirection == Direction.Left)  
  286.                     body.SetValue(Canvas.LeftProperty, Math.Round((double)body.GetValue(Canvas.LeftProperty) - offset, _decimals));  
  287.                 else if (body.MoveDirection == Direction.Right)  
  288.                     body.SetValue(Canvas.LeftProperty, Math.Round((double)body.GetValue(Canvas.LeftProperty) + offset, _decimals));  
  289.             }  
  290.         }  
  291.         /// <summary>  
  292.         /// 蜕指定数量的皮  
  293.         /// </summary>  
  294.         private void AddSkin(int count)  
  295.         {  
  296.             player.PlaySkin();  
  297.             while (count > 0)  
  298.             {  
  299.                 KeyValuePair<Body, CellPoint> body = _bodies.ElementAt(_bodies.Count - 1);  
  300.                 CellPoint skinPoint = body.Value;  
  301.                 Skin skin = new Skin();  
  302.                 skin.SetValue(Canvas.LeftProperty, skinPoint.X * App.CellSize);  
  303.                 skin.SetValue(Canvas.TopProperty, skinPoint.Y * App.CellSize);  
  304.                 _skins.Add(skin, skinPoint);  
  305.                 canvasSkin.Children.Add(skin);  
  306.                 _emptyCells.Remove(skinPoint);  
  307.                 canvasSnake.Children.Remove(body.Key);  
  308.                 _bodies.Remove(body.Key);  
  309.                 count--;  
  310.             }  
  311.         }  
  312.         #region 辅助方法  
  313.         /// <summary>  
  314.         /// 修正指定的位置信息为整数  
  315.         /// </summary>  
  316.         private int CorrectPosition(double position)  
  317.         {  
  318.             double result;  
  319.             double offset = Math.Round(_speed * _dt, _decimals);  
  320.             double temp = Math.Round(position % App.CellSize, _decimals);  
  321.             if (Math.Abs(temp) < offset)  
  322.                 result = Math.Round(position - temp);  
  323.             else 
  324.                 result = Math.Round(position - temp) + Math.Sign(temp) * App.CellSize;  
  325.             return (int)result;  
  326.         }  
  327.         /// <summary>  
  328.         /// 修正蛇的每一段的位置为整数  
  329.         /// </summary>  
  330.         private void CorrectPosition()  
  331.         {  
  332.             foreach (Body body in _bodies.Keys)  
  333.             {  
  334.                 double x = CorrectPosition((double)body.GetValue(Canvas.LeftProperty));  
  335.                 double y = CorrectPosition((double)body.GetValue(Canvas.TopProperty));  
  336.  
  337.                 if (x == App.Width)  
  338.                     x = 0d;  
  339.                 else if (x == -App.CellSize)  
  340.                     x = App.Width - App.CellSize;  
  341.                 else if (y == App.Height)  
  342.                     y = 0d;  
  343.                 else if (y == -App.CellSize)  
  344.                     y = App.Height - App.CellSize;  
  345.                 body.SetValue(Canvas.LeftProperty, x);  
  346.                 body.SetValue(Canvas.TopProperty, y);  
  347.             }  
  348.         }  
  349.         /// <summary>  
  350.         /// 更新蛇的每一段的网格位置信息  
  351.         /// </summary>  
  352.         private void UpdateBodyCell()  
  353.         {  
  354.             for (int i = 0; i < _bodies.Count; i++)  
  355.             {  
  356.                 UpdateBodyCell(_bodies.ElementAt(i).Key);  
  357.             }  
  358.         }  
  359.         /// <summary>  
  360.         /// 更新指定的 Body 的网格位置信息  
  361.         /// </summary>  
  362.         private void UpdateBodyCell(Body body)  
  363.         {  
  364.             CellPoint point = new CellPoint((int)((double)body.GetValue(Canvas.LeftProperty) / App.CellSize), (int)((double)body.GetValue(Canvas.TopProperty) / App.CellSize));  
  365.             if (body.MoveDirection == Direction.Up)  
  366.                 point.Y--;  
  367.             else if (body.MoveDirection == Direction.Down)  
  368.                 point.Y++;  
  369.             else if (body.MoveDirection == Direction.Left)  
  370.                 point.X--;  
  371.             else if (body.MoveDirection == Direction.Right)  
  372.                 point.X++;  
  373.  
  374.             point = CorrectCellPoint(point);  
  375.  
  376.             _bodies[body] = point;  
  377.         }  
  378.         /// <summary>  
  379.         /// 修正网格位置  
  380.         /// </summary>  
  381.         private CellPoint CorrectCellPoint(CellPoint point)  
  382.         {  
  383.             if (point.X > _columns - 1)  
  384.                 point.X = _columns - point.X;  
  385.             else if (point.X < 0)  
  386.                 point.X = point.X + _columns;  
  387.  
  388.             if (point.Y > _rows - 1)  
  389.                 point.Y = _rows - point.Y;  
  390.             else if (point.Y < 0)  
  391.                 point.Y = point.Y + _rows;  
  392.  
  393.             return point;  
  394.         }  
  395.         /// <summary>  
  396.         /// 获取空网格集合  
  397.         /// </summary>  
  398.         private List<CellPoint> GetEmptyCells()  
  399.         {  
  400.             List<CellPoint> emptyCells = new List<CellPoint>();  
  401.             List<CellPoint> aroundHeadCells = new List<CellPoint>();  
  402.             CellPoint headPoint = _bodies.First().Value;  
  403.             for (int i = -5; i < 5; i++)  
  404.             {  
  405.                 for (int j = -5; j < 5; j++)  
  406.                 {  
  407.                     CellPoint point = new CellPoint(headPoint.X + i, headPoint.Y + j);  
  408.                     point = CorrectCellPoint(point);  
  409.  
  410.                     aroundHeadCells.Add(point);  
  411.                 }  
  412.             }  
  413.             // skin 的占位情况因为确定了就不变了,所以在 AddSkin() 处计算  
  414.             // 为了以下 LINQ 的可用,需要重写 CellPoint 的 public override bool Equals(object obj)  
  415.             emptyCells = _emptyCells.Where(p => !_bodies.Select(x => x.Value).Contains(p)).ToList();  
  416.             emptyCells = emptyCells.Where(p => !_beans.Select(x => x.Value).Contains(p)).ToList();  
  417.             emptyCells = emptyCells.Where(p => !aroundHeadCells.Contains(p)).ToList();  
  418.             return emptyCells;  
  419.         }  
  420.         #endregion  
  421.         #region 属性  
  422.         public Direction MoveDirection  
  423.         {  
  424.             set  
  425.             {  
  426.                 Body head = _bodies.First().Key;  
  427.  
  428.                 if (head.MoveDirection == Direction.Up && value == Direction.Down)  
  429.                     return;  
  430.                 if (head.MoveDirection == Direction.Down && value == Direction.Up)  
  431.                     return;  
  432.                 if (head.MoveDirection == Direction.Left && value == Direction.Right)  
  433.                     return;  
  434.                 if (head.MoveDirection == Direction.Right && value == Direction.Left)  
  435.                     return;  
  436.                 _moveDirection = value;  
  437.             }  
  438.         }  
  439.         public bool Enabled  
  440.         {  
  441.             get { return _enabled; }  
  442.             set { _enabled = value; }  
  443.         }  
  444.         public double Speed  
  445.         {  
  446.             get { return _speed; }  
  447.             set { _speed = value; }  
  448.         }  
  449.         public int AteCapacity  
  450.         {  
  451.             get { return _ateCapacity; }  
  452.             set { _ateCapacity = value; }  
  453.         }  
  454.         #endregion  
  455.         #region 事件 GameOver 和 Ate  
  456.         public event EventHandler GameOver;  
  457.         public event EventHandler Ate;  
  458.         #endregion  
  459.     }  

原文链接:http://www.cnblogs.com/webabcd/archive/2009/09/14/1566082.html

责任编辑:张伟 来源: webabcd的博客
相关推荐

2021-06-15 09:18:51

鸿蒙HarmonyOS应用

2015-07-31 11:26:24

Swift贪吃蛇

2020-08-20 20:30:49

C语言小游戏贪吃蛇

2021-09-02 15:25:53

鸿蒙HarmonyOS应用

2024-01-18 11:22:41

C++Windows开发

2022-10-28 09:33:10

Linux贪吃蛇

2022-07-25 14:17:04

JS应用开发

2022-11-07 11:27:00

JS游戏开发

2021-04-20 11:40:12

Linux图形库curses

2012-05-31 14:20:14

2023-10-17 10:20:53

VueReact

2016-09-14 21:17:47

PythonAsyncio游戏

2016-09-19 21:24:08

PythonAsyncio游戏

2016-09-22 21:12:14

2010-02-05 15:00:44

Android 调用u

2015-07-07 15:47:57

Razer雷蛇

2011-02-21 17:15:14

SilverlightNEY

2021-05-27 16:53:09

开发技能代码

2018-08-31 15:48:33

2013-08-13 16:14:10

点赞
收藏

51CTO技术栈公众号