使用 QGraphicsScene 和 QGraphicsView 在 Qt 中开始基于 Tile 的游戏 [英] Getting started with a Tile-based game in Qt using QGraphicsScene and QGraphicsView

查看:32
本文介绍了使用 QGraphicsScene 和 QGraphicsView 在 Qt 中开始基于 Tile 的游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开始在 Qt 中编写基于 2D tile 的游戏,并了解用于显示和处理大量 2D 对象的 QGraphicsScene 和 QGraphicsView 类.

I'm going to start programming a 2D tile-based game in Qt and read about the QGraphicsScene and QGraphicsView classes which are intended for displaying and handling lots of 2D objects.

我的问题是,使用 QGraphicsScene 创建具有大量图块的世界是否可行?我可以逐个图块一次添加整个世界,还是应该尝试实施一些东西来限制该区域?我读过 QGraphicsScene 可以处理数千个项目",但 2D 瓷砖地图很容易变得非常非常大(200x200 瓷砖?不是很多,但已经有 40,000 个对象,很多).

My question is that will it be feasible to create a world with lots of tiles using QGraphicsScene? Can I add the whole world at once tile-by-tile or should I try to implement something to restrict the area a bit? I've read that QGraphicsScene can handle "thousands of items" but a 2D tile map can easily get really, really big (200x200 tiles? not that many, but that's already 40,000 objects which is a lot).

地图也几乎是静态的,因此可以将其绘制为一个大像素图,但这确实阻止您使用 QGraphicsScene 中的所有花哨的东西,例如处理独立项目上的鼠标单击等.我将绘制玩家、NPC 等等,它们不会与瓷砖网格对齐.是否有一些优化的东西来使用大量静态对象和一些动态对象?

The map is also going to be pretty much static so it might be possible to draw it as one big pixmap but this really prevents you from using all the fancy stuff in QGraphicsScene like handling mouse clicks on independent items etc. On top of that I'm going to draw the player, the NPCs and so forth which will not be aligned to the tile grid. Are there some optimization stuff for using lots of static objects and some dynamic ones on top of them?

使用 QGraphicsScene 和 QGraphicsView 甚至是一个好主意,还是我应该尝试在 Qt 中寻找替代方案,或者可能是一个不同的、更面向游戏的库?

Is using QGraphicsScene and QGraphicsView even a good idea at all or should I try to look for an alternative inside Qt or maybe a different, more game-oriented library?

提前致谢

推荐答案

你应该使用 QGraphicsView.

You should use QGraphicsView.

Qt 文档中的 40,000 芯片是您仔细检查的最佳示例.它处理大量元素的复杂性,以多种比例绘制它们等等.

The 40,000 Chips int the Qt Documentation is your best example to closely examine. It deals with the complexity of large numbers of elements, drawing them at multiple scales and a lot more.

玩这个例子,你会看到所有的筹码实际上组成了一张大照片,如果你缩小,你可以选择,在任何视图拖放多个筹码,或者你可以放大到足以看到一些单个芯片上的文本.理解每个部分需要时间,但这是一个非常全面的例子.

Play with the example and you will see that all the chips actually make up a large photo if you zoom out and that you can select, and drag and drop multiple chips at any view, or you can zoom in enough to see some text on an individual chip. It will take time to understand each part, but this is a very thorough example to look into.

chips.cpp源代码,它展示了如何通过使用LevelOfDetail"或基于存储在样式选项中的转换的 lod 变量 switch 语句仍然可以快速运行.

In the chips.cpp source, it shows how it can still run quickly by using a "LevelOfDetail" or lod variable switch statement based off of the transformation stored in a style option.

Qt 图形视图已经过优化,可以完成您在问题中谈到的很多事情,但需要一段时间才能理解如何处理它.

Qt Graphics View has been optimized to do a lot of the things you have talked about in your question, but it takes a while to understand how to approach it.

如果您仍然对要使用的地图大小有疑问,我会将图块布局存储在硬盘驱动器上,并在需要时加载您需要的图块布局,然后删除不需要的图块布局必要的场景.

If you are still having issues with the size of the map you want to use, I would store tile layouts on the harddrive and load the ones you need when you need them, and remove the ones you don't need off of the scene as necessary.

这篇关于使用 QGraphicsScene 和 QGraphicsView 在 Qt 中开始基于 Tile 的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆