华容道问题求解_详细设计(六)之简单互动和动画

2024-03-11 01:36

本文主要是介绍华容道问题求解_详细设计(六)之简单互动和动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简单互动

为了增加趣味性,增加了简单的互动功能,即实现了一个简单的华容道游戏。在HrdGame中有两个鼠标操作的函数,在传入的控件中调用这个两个函数就可以了。
代码如下:
Click事件

        private void pnl_GameBoard_MouseClick(object sender, MouseEventArgs e) => _hrdGame.Click(e);

MouseMove事件

      private void pnl_GameBoard_MouseMove(object sender, MouseEventArgs e){if(cbx_ByHuman.Checked)//增加了一个界面控制CheckBox_hrdGame.MouseMove(e);}

mouseMove中的代码相对简单,并没有有抓取棋子的操作,只是在滑过的棋子做了高亮处理,这样显得稍微生动一些。

Click 事件核心代码

       internal void Click(MouseEventArgs e){var openPcsLst = GetOpenPcs();// select an open piecevar basicPcsArr = GetPieces();// pay attention to here , after the serilization !!!!!!!!!!!!!!// 检查鼠标是否在点击了 Open Piece,如果是则认为是选择了该 棋子,如果不是,则认为是把之前的棋子移动到这个位置。foreach (var openPcs in openPcsLst){var idx = openPcs.piece.idx;var pcs = basicPcsArr[idx];Rectangle pcsRect = new Rectangle((int)pcs.PcsLoc.X, (int)pcs.PcsLoc.Y, pcs.PcsSize.Width, pcs.PcsSize.Height);if (pcsRect.Contains(e.Location)){gameState.selPcs = pcs;RefreshLayout();break;}}// 移动到鼠标点击位置的合理性判断,如果是空白区域,则进行移动处理。if (gameState.selPcs == null){return;}var blnkPcs = GetBlanks();foreach (var blkPcs in blnkPcs){//var pcs = openPcs.piece;Rectangle pcsRect = new Rectangle((int)blkPcs.PcsLoc.X, (int)blkPcs.PcsLoc.Y, blkPcs.PcsSize.Width,blkPcs.PcsSize.Height);if (pcsRect.Contains(e.Location)){var selPcs = gameState.selPcs;var selPos = selPcs.GetHrdPos();var blkPos = blkPcs.GetHrdPos();if (Math.Abs(selPos.X - blkPos.X) < 2 + selPcs.PcsSize.Width && Math.Abs(selPos.Y - blkPos.Y) < 2 + selPcs.PcsSize.Height){ // might move the selected block and into the initial stateif (selPcs.MoveDir != ""){//移动时做一个简单的动画处理MoveWithAnimation(selPcs, blkPcs);RefreshLayout();break; ;}}}}var tmpHasCode = GetMyHashCodeV1(gameState);_curHashCode = tmpHasCode;}

简单动画

说明:代码是在GPT给的代码基础上进行改写的。
利用Timer事件,每隔一段时间显示一个画面,并进行痕迹的擦除。使用了大量的成员变量进行传值,这样简化俩Timer函数参数问题。另外对鼠标事件的多次快速点击没有做处理,因此会有重入发生,导致效果有些混乱。
核心代码如下:

       internal void MovePcsWithAnimation(Piece selPcs, HrdPoint srcPos, HrdPoint dstPos, HrdPoint anotherPos, Piece emptySp, Piece anotherES){curSelPcs=selPcs;srcPcsHrdLoc = HrdPosToLoc(srcPos);targeHrdLocLst = new List<PointF>();if(selPcs.GetHrdType()==HRDGame.TYPE_SMALL_PIECE && Math.Abs(srcPos.X - dstPos.X)+Math.Abs(srcPos.Y-dstPos.Y) == 2){// need to move to the another piece first //anotherPos = new HrdPointHrdPosToLoc(dstPos);var anotherLoc =  HrdPosToLoc(anotherPos);targeHrdLocLst.Add(anotherLoc);}targeHrdLocLst.Add(HrdPosToLoc(dstPos));srcBkgdRect = new Rectangle((int)selPcs.PcsLoc.X, (int)selPcs.PcsLoc.Y, selPcs.PcsSize.Width, selPcs.PcsSize.Height);animotionLocLstIdx = 0;timerTuple.selPcs = selPcs;timerTuple.srcPos = srcPos;timerTuple.dstPos= dstPos;timerTuple.anotherPos= anotherPos;timerTuple.emptySp = emptySp;timerTuple.anotherES = anotherES;timer.Start(); // Start the animation}

timer事件调用 movePcs函数,函数没有优化,因此代码比较冗长。核心代码如下

       private void MovePcs(){int speed = 5; // Adjust the speed of the animation//Refresh the background ;//float oldX,oldY;Rectangle tailRec= new Rectangle(0,0,0,0);// used to clear the back ground if(animotionLocLstIdx>= targeHrdLocLst.Count){return;}var targeHrdLoc= targeHrdLocLst[animotionLocLstIdx];//计算移动后的棋子的坐标和大小,并计算移动后需要擦除的区域//#############################################if (srcPcsHrdLoc.X < targeHrdLoc.X){tailRec.X = (int)srcPcsHrdLoc.X;tailRec.Y = (int)srcPcsHrdLoc.Y;tailRec.Width = speed;tailRec.Height = curSelPcs.PcsSize.Height;srcPcsHrdLoc.X += speed;}else if ...}////#############################################// 根据上述计算结果,进行绘制curSelPcs.PcsLoc = srcPcsHrdLoc;var g=_ctrl.CreateGraphics();DrawRegularBox(g, curSelPcs);g.FillRectangle(new SolidBrush(Color.Gray), tailRec);//srcBkgdRect = tRec;if (Math.Abs(srcPcsHrdLoc.X - targeHrdLoc.X) <= 5 && Math.Abs(srcPcsHrdLoc.Y - targeHrdLoc.Y) <= 5){animotionLocLstIdx++;if (animotionLocLstIdx < targeHrdLocLst.Count && animotionLocLstIdx>0){var tLoc = targeHrdLocLst[animotionLocLstIdx-1];//curSelPcs pcs.PcsSize.Width,pcs.PcsSize.HeightsrcBkgdRect = new Rectangle((int)(tLoc.X), (int)(tLoc.Y), curSelPcs.PcsSize.Width, curSelPcs.PcsSize.Height);}//判断是否移动可以移动两步(一个正方形)if (animotionLocLstIdx >= targeHrdLocLst.Count){.....}}}

Timer 的初始化代码

       private void InitializeTimer(){timer = new System.Windows.Forms.Timer();timer.Interval = 10; // Adjust the interval based on the desired smoothnesstimer.Tick += Timer_Tick;}

移动效果的目的是为了让过程更清晰一些,并没有对移动逻辑进行严格的分析和约束,因此移动过程不是很优雅。如果笔者有时间或者感兴趣会优化一下。

笔者在完成上述功能后,又增加了一个简单设计的功能,这样就可以利用这个程序进行一点儿华容道各种不同布局的探索以及之间关系的检测。(待续)

Marasun BJFWDQ
2024-03-10

这篇关于华容道问题求解_详细设计(六)之简单互动和动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/796174

相关文章

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应