在网格上寻找形状的空间
我正在开发一款游戏,其中有一个 8x12 网格,其中每个单元格大小相同,并且所有单元格都直接相邻。
您可以拖动各种类似俄罗斯方块的形状,并将它们放置在网格上的有效位置,有效位置是该形状将占据的所有单元格不被其他形状占据的位置。
我的问题是我不确定如何在网格空间中搜索有效位置。我一直在寻找一种可以解决此类问题的算法,但到目前为止我却空手而归。检测有效位置似乎应该非常简单,但我未能找到成功的解决方案。
任何关于如何解决这个问题的流程、算法建议或想法都将非常有帮助。谢谢!
编辑:
这是预期的功能:当形状位于有效位置时,可以在有效位置之间自由拖动它并跟随鼠标指针。但是,当您尝试将形状拖动到无效区域时(即,沿指定方向移动会将形状的一个或多个块放置在无效位置),它会停留在最后一个有效位置。
此时,当鼠标位于无效区域时,我想做一些“预测”移动,这样如果玩家将鼠标光标移动到有效位置附近,形状就会“捕捉”到位,比如说如果有效位置距离两个网格空间。
感谢您迄今为止的建议;我没想到这个方法!
I'm working on a game where you have an 8x12 grid where each cell is the same size and all the cells are directly next to one another.
You drag around various Tetris-like shapes and place them on the grid in valid locations, a valid location being one where all the cells that the shape will occupy are not occupied by some other shape.
My problem is that I'm not sure how to search the grid space for valid locations. I've been searching for an algorithm that will solve this sort of problem, but I have come up empty handed thus far. It seems like it should be pretty straightforward to detect valid locations, but I have not been able to come to a successful solution.
Any processes, algorithm suggestions, or ideas for how to go about solving this would be extremely helpful. Thanks!
EDIT:
Here is the expected functionality: When the shape is in a valid location, it can be dragged between valid locations freely and follows the mouse pointer. However, when you try to drag the shape into an invalid area (ie movement in the direction specified would place one or more blocks of the shape in invalid locations), it stays in the last valid location.
At this point, when the mouse is in an invalid area, I want to do some "predictive" movement so that if the player moves the mouse cursor near a valid position, the shape then "snaps" into place, say if the valid position is two grid spaces away.
Thanks for your suggestion so far; I hadn't thought of that method!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如上所述,该算法非常简单。在形状上任意选择一个起始块,并尝试将其与网格的每个开放单元相匹配。如果使用与当前单元格对齐的块“绘制”形状不会导致碰撞,则您已经找到了有效的位置。
As described, the algorithm would be quite simple. Arbitrarily choose a starting block on your shape, and try to match it to each open cell of the grid. If "drawing" the shape with the block aligned with the current cell doesn't cause a collision, you've found a valid position.