Grid-aligned 2D game with QGraphicsView
I want to write a game using QGraphics View in which the items move in a grid.
The items must always be aligned to the 16x16 grid, and may never have an X or Y position that isn't a multiple of 16. Also, there is a fixed amount of cells. How do I:
- Keep items aligned with the grid (maybe it would be nice to have some kind of abstraction)
- Keep the grid, which has a fixed number of cells, in the middle of the scene (in case the user resizes the window) and have the items move relative to it, not the scene.
- For each item to be able to check for the presence of another item in an adjacent cell.
add a abstraction layer.
Make a class wo draw a tile and a manager for compose all tiles in the grid.
the tile manager no need to know the size os the nature of a tile.
I'm starting to think, maybe it's best to ditch the QGraphicsView framework and write my own simple grid-based framework?
It might also make "collision detection" easier, because my game only needs to know what object is in a certain cell, not true collision detection.
You can use QGraphicsFramework for render the tiles. And write a grid based framework for manage the tiles.
You can create a class hwo manage the map and recive cordinates like 2,3; work with this coordinates for colision and "see" what is around the cell and pass coordinates like 16,32 for the QGraphicsSene
It seems easy enough to do your own rendering, just for each item get its image and paint it in its x and y coordinates.
One thing I am confused about, though: would it be better for the items to store their coordinates, of for the grid widget to keep track of the coordinates (or maybe even store the items in a 2D array corresponding to the grid)?
Items will often have to move themselves and check for adjacent items.
well.. i don't know
You can use QML also.
The Grid container is a good solution.
brandont last edited by
Just out of curiosity, how were you going to do your own rendering? QPainter?
I'm still new to this framework, but I think I would do it this way:
Each cell is 16x16 big, in a grid of 16x16 correct?
Use QGraphicsItem based classes and keep track of their x and y coordinates. When you set their position, just multiply x and y by 16. Then you can just keep track of all of them in an array, and add a checkNeighbours() function.
Not sure about how to center it, but I'm sure it's easy.
[quote author="brandont" date="1290227431"]Just out of curiosity, how were you going to do your own rendering? QPainter?[/quote]
For every item, draw its image at its X and Y coordinates multiplied by 16.
yoogi last edited by
How your game is coming along? can you share the structure of the game you've designed.
Does it use state machines? How do you shift between Intro screen, Game Screen , help screen / Score screens.. etc.
How do you capture mouse interactions? Game Screen as one Widget or use MouseGrab items on interactive parts of games?
I am also intrested in making simple 2d games.