上接游戏人生Silverlight(4) - 连连看[Silverlight 2.0(c#)]

简介:
/// <summary> 
                 /// 获取指定的 CardPoint 的 Y 轴方向上的所有 ImageName 为 Global.EmptyImageName 的 CardPoint 集合 
                 /// </summary> 
                 private List<CardPoint> GetYPositions(CardPoint p) 
                { 
                        var result =  new List<CardPoint>() { p }; 
                         for ( int i = 0; i < Global.ContainerRows; i++) 
                        { 
                                var point =  new CardPoint(i * Global.ContainerColumns + p.X); 
                                 if (_cards[point.Position].ImageName == Global.EmptyImageName) 
                                        result.Add(point); 
                        } 
 
                         return result; 
                } 
 
                 /// <summary> 
                 /// 执行消去两个卡片的任务 
                 /// 参数为:两个卡片对象和消去路径的四个点(此四个点需按路径方向依次传入) 
                 /// </summary> 
                 private  void RemoveCard(CardModel c1, CardModel c2, CardPoint p1, CardPoint p2, CardPoint p3, CardPoint p4) 
                { 
                        _cards[c1.Position] =  new CardModel(Global.EmptyImageName, c1.Position); 
                        _cards[c2.Position] =  new CardModel(Global.EmptyImageName, c2.Position); 
 
                        Points.Clear(); 
                        Points.Add(CardPoint2Point(p1)); 
                        Points.Add(CardPoint2Point(p2)); 
                        Points.Add(CardPoint2Point(p3)); 
                        Points.Add(CardPoint2Point(p4)); 
 
                        Thread thread =  new Thread 
                        ( 
                                x => 
                                { 
                                        Thread.Sleep(100); 
 
                                        _syncContext.Post 
                                        ( 
                                                y => 
                                                { 
                                                        Points.Clear(); 
                                                }, 
                                                 null 
                                        ); 
                                } 
                        ); 
                        thread.Start(); 
                } 
 
                 /// <summary> 
                 /// CardPoint 转换成坐标位置 Point 
                 /// </summary> 
                 private Point CardPoint2Point(CardPoint cardPoint) 
                { 
                         // 38 - 每个正方形卡片的边长 
                         // 19 - 边长 / 2 
                         // cardPoint.X * 2 - 卡片的 Padding 为 1 ,所以卡片间的间距为 2 
                        var x = cardPoint.X * 38 + 19 + cardPoint.X * 2; 
                        var y = cardPoint.Y * 38 + 19 + cardPoint.Y * 2; 
 
                         return  new Point(x, y); 
                } 
 
                 /// <summary> 
                 /// 检查当前是否仍然有可消除的卡片 
                 /// </summary> 
                 public  bool IsActive() 
                { 
                        var currentCards = _cards.Where(p => p.ImageName != Global.EmptyImageName).ToList(); 
 
                         for ( int i = 0; i < currentCards.Count() - 1; i++) 
                        { 
                                 for ( int j = i + 1; j < currentCards.Count(); j++) 
                                { 
                                         if (Match(currentCards[i], currentCards[j],  false)) 
                                                 return  true
                                } 
                        } 
 
                         return  false
                } 
 
                 /// <summary> 
                 /// 重新排列当前卡片集合 
                 /// 并保证有可消卡片 
                 /// </summary> 
                 public  void Replace() 
                { 
                        Random r =  new Random(); 
                        var currentCards = _cards.Where(p => p.ImageName != Global.EmptyImageName).ToList(); 
                        var count = currentCards.Count; 
                         if (count == 0) 
                                 return
 
                        var targetImageNames = currentCards.Select(p => p.ImageName).ToList(); 
                        var targetPositions = currentCards.Select(p => p.Position).ToList(); 
 
                         for ( int i = 0; i < count; i++) 
                        { 
                                var index = r.Next(0, count - i); 
 
                                var targetImageName = targetImageNames.Skip(index).First(); 
                                var targetPosition = targetPositions.Skip(i).First(); 
 
                                _cards[targetPosition] =  new CardModel(targetImageName, targetPosition); 
                                targetImageNames.RemoveAt(index); 
                        } 
 
                         while (!IsActive()) 
                        { 
                                Replace(); 
                        } 
                } 
 
                 /// <summary> 
                 /// 清除卡片集合 
                 /// </summary> 
                 public  void Clear() 
                { 
                        _cards.Clear(); 
                        Points.Clear(); 
                } 
 
                 /// <summary> 
                 /// 当前卡片数量 
                 /// </summary> 
                 public  int CardCount 
                { 
                        get {  return _cards.Where(p => p.ImageName != Global.EmptyImageName).Count(); } 
                } 
 
                 /// <summary> 
                 /// 连连看的卡片集合 
                 /// </summary> 
                 public ObservableCollection<CardModel> Cards 
                { 
                        get {  return _cards; } 
                } 
 
                 private PointCollection _points; 
                 /// <summary> 
                 /// 可消两卡片的连接路径上的 4 个点的集合 
                 /// </summary> 
                 public PointCollection Points 
                { 
                        get 
                        { 
                                 if (_points ==  null
                                        _points =  new PointCollection(); 
 
                                 return _points; 
                        } 
                        set 
                        { 
                                _points = value; 
                                 if (PropertyChanged !=  null
                                        PropertyChanged( thisnew PropertyChangedEventArgs( "Points")); 
                        } 
                } 
 
                 public  event PropertyChangedEventHandler PropertyChanged; 
        } 
}
 



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345623 ,如需转载请自行联系原作者

相关文章
|
C# 图形学
C#之四十九 游戏编程周每日总结
C#之四十九 游戏编程周每日总结
53 0
|
2月前
|
SQL API 定位技术
基于C#使用winform技术的游戏平台的实现【C#课程设计】
本文介绍了基于C#使用WinForms技术开发的游戏平台项目,包括项目结构、运行截图、实现功能、部分代码说明、数据库设计和完整代码资源。项目涵盖了登录注册、个人信息修改、游戏商城列表查看、游戏管理、用户信息管理、数据分析等功能。代码示例包括ListView和ImageList的使用、图片上传、图表插件使用和SQL工具类封装,以及高德地图天气API的调用。
基于C#使用winform技术的游戏平台的实现【C#课程设计】
|
4月前
|
存储 缓存 C#
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
|
6月前
|
程序员 C# Python
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
|
6月前
|
存储 程序员 C#
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
|
6月前
|
存储 C# 开发工具
22.C# 中使用变量记录玩家创建的角色名:实现与游戏角色的互动
22.C# 中使用变量记录玩家创建的角色名:实现与游戏角色的互动
58 0
|
6月前
|
定位技术 C# 图形学
Unity和C#游戏编程入门:创建迷宫小球游戏示例
Unity和C#游戏编程入门:创建迷宫小球游戏示例
150 2
C#实现的打飞机游戏(课程设计)
C#实现的打飞机游戏(课程设计)
144 1
|
存储 C# UED
C# 拼图游戏(超详细)
C# 拼图游戏(超详细)
460 0
C# 拼图游戏(超详细)