在 C++ 中为游戏创建可点击网格的最简单方法是什么?

发布于 2024-08-30 05:59:18 字数 189 浏览 6 评论 0原文

我已经有一段时间没有使用C++了,所以我决定做一个小项目来再次熟悉它。我正在尝试制作一个中国跳棋游戏,但我没有使用 C++ 进行 GUI 设计的经验。有没有一种真正简单的方法来制作网格(即位图六边形或其他东西),当单击该网格时,会给我所单击的网格的索引号? 如果有人有任何关于如何执行此操作的示例,即使只有二维正方形网格,那也会很有帮助。 任何帮助表示赞赏,谢谢!

I haven't used C++ in a while, so I decided to take on a small project to become familiar with it again. I am trying to make a chinese checkers game, but I have no experience with GUI design in C++. Is there a real simple way to just make grid (i.e. bitmap hexagons or something) that when clicked on, will give me the index number of the one I've clicked on?
If someone has any examples of how to do this, even with just a 2D grid of squares, that would be helpful.
Any help is appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝海 2024-09-06 05:59:18

取决于您采取什么简单的方式

Qt 可能是一种简单的方法,但需要时间来习惯它(如果您不习惯的话)。

如果我是您,我会创建一个简单的 winapi 应用程序,为 WM_CLICK 消息编写处理程序,并添加一个简单的网格渲染器。

Depends on what you take for simple way.

Qt could be a simple way, but it would take time to get used to it (if you aren't).

If I were you, I would use create a simple winapi application, write handlers for WM_CLICK messages and add a simple grid renderer.

转瞬即逝 2024-09-06 05:59:18

第一件事是决定您的应用程序要使用哪个 API。 Qt?雪迪龙? Win32? OpenGL?我的建议是使用 Qt 及其 QGraphicsView 类(您可以从使用 QGraphicsView 的 Qt 示例应用程序之一开始,并修改它以适合您的口味)。

The first thing is to decide which API you are going to use for your app. Qt? SDL? Win32? OpenGL? My recommendation is to use Qt and its QGraphicsView class (you could start with one of the Qt example apps that uses QGraphicsView and modify it to suit your taste).

阪姬 2024-09-06 05:59:18

它会根据您的 GUI 工具包而有所不同。但是,我相信两种最可能的方法可能是

  1. 每个方块、洞或您尝试在游戏中单击的任何内容的

    存储坐标的一些变化,并具有单击事件处理程序使用它获得的坐标来确定单击了哪个方块。

  2. 让每个方块都有自己的小部件。这样,当点击它时,正方形本身就会获取点击事件,并且可以以良好的面向对象的方式进行处理。然而,对于中国跳棋游戏来说,这意味着需要大量的小部件。

It would vary depending on your GUI toolkit. However, I believe that the two most likely ways would likely either be some variation on

  1. Store coordinates for each of the squares, holes, or whatever it is you're trying to click on for the game and have the click event handler use the coordinates that it gets to determine which square was clicked on.

  2. Make each square its own widget. That way, when it's clicked on, it's the square itself that gets the click event and it can be handled in a nice, object-oriented manner. However, that would mean quite a lot of widgets for a Chinese Checkers game.

水溶 2024-09-06 05:59:18

通常,GUI takeit 会让您覆盖(例如通过虚拟函数)代表棋盘的小部件的事件处理程序,因此您可以通过调用自己的一些函数(例如 MouseClicked(int x, int y) 来处理鼠标单击事件) ),其中处理程序还将为您提供鼠标坐标 x 和 y。

如果它是矩形网格,只需将坐标整数除以单元格宽度(以像素为单位)即可。

如果它是一个六边形单元格,那么弄清楚它会更加困难。我想您可以首先定义一个矩形网格来标记六边形的矩形中心,然后在边缘周围的四个三角形区域内添加更多检测。不过,仅使用矩形单元定义,游戏可能就相当可用,而忽略了三角形边缘。

Typically the GUI tookit will let you override (for example through virtual functions) the event handler of the widget representing the board, so you'd then handle a mouse click event by calling some function of your own like MouseClicked(int x, int y), where the handler will also give you the mouse coordinates x and y.

If it's a rectangular grid, just integer divide the coordinate by the cell width in pixels.

If it's a grid of hexagonal cells, then figuring that out will be more difficult. I imagine you could first define a rectangular grid marking the rectangular centers of the hexagons, then add more detection for within the four triangular areas around the edges. The game might be fairly usable with just the rectangular cell definitions, though, just ignoring the triangular edges.

梦回旧景 2024-09-06 05:59:18

使用一行数学运算将鼠标坐标转换为游戏网格坐标非常容易:

POINT grid_loc = POINT(click.x / grid.cell.width, click.y / grid.cell.height);

当然,这是一个从零开始的游戏网格。

It's easy enough to convert mouse coordinates to game-grid coordinates using a single line of math:

POINT grid_loc = POINT(click.x / grid.cell.width, click.y / grid.cell.height);

Of course that's a zero-based game-grid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文