龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

Silverlight游戏编程:使用Silverlight 3.0实现贪吃蛇游戏(c#)(3)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
/// summary /// 更新蛇的每一段的运动方向 /// /summary private void UpdateDirection() { for ( int i = _bodies.Count - 1 ;i - 1 ;i -- ) { if (i == 0 ) _bodies.ElementAt(i).Key.MoveDirection

        /// <summary>
        
/// 更新蛇的每一段的运动方向
        
/// </summary>
        private void UpdateDirection()
        {
            
for (int i = _bodies.Count - 1; i > -1; i--)
            {
                
if (i == 0)
                    _bodies.ElementAt(i).Key.MoveDirection 
= _moveDirection;
                
else
                    _bodies.ElementAt(i).Key.MoveDirection 
= _bodies.ElementAt(i - 1).Key.MoveDirection;
            }
        }

        
/// <summary>
        
/// 更新蛇的每一段的位置
        
/// </summary>
        private void UpdatePosition()
        {
            
double offset = Math.Round(_speed * _dt, _decimals);

            
foreach (var body in _bodies.Keys)
            {
                
if (body.MoveDirection == Direction.Up)
                    body.SetValue(Canvas.TopProperty, Math.Round((
double)body.GetValue(Canvas.TopProperty) - offset, _decimals));
                
else if (body.MoveDirection == Direction.Down)
                    body.SetValue(Canvas.TopProperty, Math.Round((
double)body.GetValue(Canvas.TopProperty) + offset, _decimals));
                
else if (body.MoveDirection == Direction.Left)
                    body.SetValue(Canvas.LeftProperty, Math.Round((
double)body.GetValue(Canvas.LeftProperty) - offset, _decimals));
                
else if (body.MoveDirection == Direction.Right)
                    body.SetValue(Canvas.LeftProperty, Math.Round((
double)body.GetValue(Canvas.LeftProperty) + offset, _decimals));
            }
        }

        
/// <summary>
        
/// 蜕指定数量的皮
        
/// </summary>
        private void AddSkin(int count)
        {
            player.PlaySkin();

            
while (count > 0)
            {
                KeyValuePair
<Body, CellPoint> body = _bodies.ElementAt(_bodies.Count - 1);

                CellPoint skinPoint 
= body.Value;
                Skin skin 
= new Skin();
                skin.SetValue(Canvas.LeftProperty, skinPoint.X 
* App.CellSize);
                skin.SetValue(Canvas.TopProperty, skinPoint.Y 
* App.CellSize);
                _skins.Add(skin, skinPoint);
                canvasSkin.Children.Add(skin);

                _emptyCells.Remove(skinPoint);

                canvasSnake.Children.Remove(body.Key);
                _bodies.Remove(body.Key);
                count
--;
            }
        }

        
#region 辅助方法
        
/// <summary>
        
/// 修正指定的位置信息为整数
        
/// </summary>
        private int CorrectPosition(double position)
        {
            
double result;
            
double offset = Math.Round(_speed * _dt, _decimals);

            
double temp = Math.Round(position % App.CellSize, _decimals);
            
if (Math.Abs(temp) < offset)
                result 
= Math.Round(position - temp);
            
else
                result 
= Math.Round(position - temp) + Math.Sign(temp) * App.CellSize;

            
return (int)result;
        }

        
/// <summary>
        
/// 修正蛇的每一段的位置为整数
        
/// </summary>
        private void CorrectPosition()
        {
            
foreach (Body body in _bodies.Keys)
            {
                
double x = CorrectPosition((double)body.GetValue(Canvas.LeftProperty));
                
double y = CorrectPosition((double)body.GetValue(Canvas.TopProperty));

                
if (x == App.Width)
                    x 
= 0d;
                
else if (x == -App.CellSize)
                    x 
= App.Width - App.CellSize;
                
else if (y == App.Height)
                    y 
= 0d;
                
else if (y == -App.CellSize)
                    y 
= App.Height - App.CellSize;

                body.SetValue(Canvas.LeftProperty, x);
                body.SetValue(Canvas.TopProperty, y);
            }
        }

        
/// <summary>
        
/// 更新蛇的每一段的网格位置信息
        
/// </summary>
        private void UpdateBodyCell()
        {
            
for (int i = 0; i < _bodies.Count; i++)
            {
                UpdateBodyCell(_bodies.ElementAt(i).Key);
            }
        }

        
/// <summary>
        
/// 更新指定的 Body 的网格位置信息
        
/// </summary>
        private void UpdateBodyCell(Body body)
        {
            CellPoint point 
= new CellPoint((int)((double)body.GetValue(Canvas.LeftProperty) / App.CellSize), (int)((double)body.GetValue(Canvas.TopProperty) / App.CellSize));
            
if (body.MoveDirection == Direction.Up)
                point.Y
--;
            
else if (body.MoveDirection == Direction.Down)
                point.Y
++;
            
else if (body.MoveDirection == Direction.Left)
                point.X
--;
            
else if (body.MoveDirection == Direction.Right)
                point.X
++;

            point 
= CorrectCellPoint(point);

            _bodies[body] 
= point;
        }

        
/// <summary>
        
/// 修正网格位置
        
/// </summary>
        private CellPoint CorrectCellPoint(CellPoint point)
        {
            
if (point.X > _columns - 1)
                point.X 
= _columns - point.X;
            
else if (point.X < 0)
                point.X 
= point.X + _columns;

            
if (point.Y > _rows - 1)
                point.Y 
= _rows - point.Y;
            
else if (point.Y < 0)
                point.Y 
= point.Y + _rows;

            
return point;
        }

        
/// <summary>
        
/// 获取空网格集合
        
/// </summary>
        private List<CellPoint> GetEmptyCells()
        {
            List
<CellPoint> emptyCells = new List<CellPoint>();

            List
<CellPoint> aroundHeadCells = new List<CellPoint>();
            CellPoint headPoint 
= _bodies.First().Value;
            
for (int i = -5; i < 5; i++)
            {
                
for (int j = -5; j < 5; j++)
                {
                    CellPoint point 
= new CellPoint(headPoint.X + i, headPoint.Y + j);
                    point 
= CorrectCellPoint(point);

                    aroundHeadCells.Add(point);
                }
            }

            
// skin 的占位情况因为确定了就不变了,所以在 AddSkin() 处计算
            
// 为了以下 LINQ 的可用,需要重写 CellPoint 的 public override bool Equals(object obj)
            emptyCells = _emptyCells.Where(p => !_bodies.Select(x => x.Value).Contains(p)).ToList();
            emptyCells 
= emptyCells.Where(p => !_beans.Select(x => x.Value).Contains(p)).ToList();
            emptyCells 
= emptyCells.Where(p => !aroundHeadCells.Contains(p)).ToList();

            
return emptyCells;
        }
        
#endregion

精彩图集

赞助商链接