Important: Please read the Qt Code of Conduct -

Getting started with a Tile-based game in Qt

  • Hello, I've programmed for quite a while in many languages but never really got into game programming. I understand some of the basics of game programming, atleast in theory. My goal is to create a simple 2D tilebased roleplaying game with lots of hack'n'slash in it. But that's beyond the point for now. I don't have that much experience with Qt either but it's always a great idea to learn two things at once, right?

    Now to the actual questions.. I was thinking that I'd create a class for tiles (called MapTile or whatever) that contains basic information such as the image to draw, any special properties (passable/not) etc and make a two-dimensional array of them. Does that sound okay or are there any better solutions? What if the map grows very large? For now I'm going to generate the map using Perlin noise so I might make it large. All characters and sprites should probably inherit from QGraphicsItem (or QGraphicsPixmapItem) and be drawn on top with respective x and y coordinates.

    I guess I should use QGraphicsView and QGraphicsScene for actually displaying everything on the screen. I'm not sure if I understood this correctly but could QGraphicsScene be used to store the whole map at the same time, even at large sizes? Could I just add every tile in the map to the scene on some layer as I initialize the game? Or should I still limit the area myself and only "draw" nearby tiles. The map layer is going to be pretty much static and only stuff on it (monsters, the player) are going to move around.

    I'm just looking for some tips since getting going seems a bit complicated with lots of things to consider. Any links to relevant articles or websites are also appreciated.

    Thanks in advance!

  • Hey, I'm working on a similar project and I'm using more or less the same approach, a grid of QGraphicsPixmapItems drawn on a QGraphicsScene. What seems to be working for me is defining an array of QGraphicsPixmapItems, and having a struct located elsewhere which stores references to the pixmaps for each tile type, making a 30x30 grid of persistent QGraphicsPixmapItems. When an event happens that causes the map to be redrawn, the map data itself, stored in a 400x400 array, is accessed by the QGraphicsScene, which keeps track of what tile the top left corner currently is, and then it requests the integer that represents the tile type for each QGraphicsPixmapItem that is currently displayed. It then calls setPixmap on each QGraphicsPixmapItem, getting the pixmap from the struct that stores it, calling setPixmap on the corresponding QGraphicsPixmapItem which discards the previous pixmap and causes it to be redrawn. By iterating over the entire series of tiles in the view, you can just swap out the pixmaps each time you need to change the view and it's far, far more efficient than managing the entire map at once, I haven't had any performance issues with it so far.

    As for putting other content on top of it...that's what I'm trying to figure out right now. I'm thinking of wrapping the tile QGraphicsPixmapItems with another class, maybe a QGraphicsItemGroup, so the items can easily have their position set relative to the tile they currently inhabit.

Log in to reply