瓷砖尺寸算法
我正在用 C++ 制作一个瓷砖游戏。现在,当游戏加载时,所有图块都根据以下条件自行放置:
tilesize ->它们是正方形,所以这是宽度和高度
我
tile_count_xtile_count_y
有以下变量:
desktop_widthdesktop_heightgame_window_widthgame_window_heighttile_count_xtile_count_y
。
的
窗口
大小
适当
基于这些值,我正在寻找一种算法,该算法将在给定桌面和tile_count约束的情况下设置 然后,在此范围内,我希望我的图块有一个偏移量,该偏移量将在窗口周围与 x% 接壤,这也基本上决定了图块大小:
示例: 如果我有 10 * 3 块瓷砖,那么:
______________________________
Window Title _[]X
------------------------------
| |
| [][][][][][][][][][] |
| [][][][][][][][][][] |
| [][][][][][][][][][] |
| |
------------------------------
我只是不确定执行此操作所需的公式。
编辑(来自评论):
- Tilesize更改,tilecountx和y是静态的
- 我希望游戏窗口尽可能大,因为它可以给定桌面分辨率,但我也希望它的纵横比尊重tilecoutx和tilecounty
我找到了一个例子我的意思是,在 Windows 中打开扫雷
I'm making a tile game in c++. Right now when the game loads all the tiles place themselves based on:
tilesize -> they are squares so this is width and height
tile_count_x
tile_count_y
I have the following variables:
desktop_width
desktop_height
game_window_width
game_window_height
tile_count_x
tile_count_y
Based on these values, I'm looking for an algorithm that will set an appropriate window size given the desktop and tile_count constraints. Then within this, I want my tiles to have an offset that will border x% around the window which will basically decide the tilesize too:
Example:
If I have 10 * 3 of tiles then:
______________________________
Window Title _[]X
------------------------------
| |
| [][][][][][][][][][] |
| [][][][][][][][][][] |
| [][][][][][][][][][] |
| |
------------------------------
I'm just not sure the formula required to do this.
EDIT (from comments):
- Tilesize changes, tilecountx and y are static
- I want the gamewindow to be as big as it can be given the desktop resolution, but I also want its aspect ratio to respect the tilecoutx and tilecounty
I found an example of what I mean, open up Minesweeper in Windows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不是 C++ 程序员,但您应该从中得到一些东西:
现在您拥有最大窗口宽度和高度,以便:
请注意,我根本没有测试过这段代码,但它应该可以工作。如果您发现任何错误,请尝试了解我尝试执行的操作并进行相应的修复。
I'm not a C++ programmer but you should get something out of this:
Now you have the maximum window width and height so that:
Note that I haven't tested this code at all, but it should work. If you find any errors, try to understand what I tried to do and fix accordingly.
简单线性代数:
我们有 2 个变量(
TILE_WIDTH
和X
)和只有 1 个方程,所以嗯,你运气不好。您需要指定
TILE_WIDTH
或X
(边距)。假设您指定X
,那么您可以求解方程:如果我们考虑高度,并且需要相同的边框,那么我们有 2 个方程和 3 个未知数。仍然是索尔。
Simple linear algebra:
We have 2 variables (
TILE_WIDTH
andX
) and only 1 equation, so um, you're out of luck.You will need to either specify
TILE_WIDTH
orX
(the margin). Let's say you specifyX
, then you can solve the equation:If we take into account the height, and require the same border, then we have 2 equations, and 3 unknowns. Still SOL.
如果您使用的是 Win32,那么您可以使用类似以下内容来初始调整窗口客户区的大小。
当窗口接收到调整大小事件时,您只需将每个图块拉伸(即放大或缩小每个图块)窗口已扩展或缩小的像素数(假设轴被锁定,即您无法调整每个图块的大小)独立)。
换句话说,tile_size 会有所不同:
此代码来自内存,但它可能会给出一个想法。
If you are working with Win32, then you could use something like the following to initially size the client-area of your window.
When the Window recieves resize events you would simply stretchblt each tile (i.e. enlarge or shrink each tile) by the number of pixels that the window has been expanded or shrunk by (Assuming that the axis are locked, that is you can't resize each independetly).
In other words the tile_size would vary:
This code is from memory, but it may give an idea.
这是我解决问题的方法...
Here is how I solved it...