PDA

View Full Version : Snake game, in what kind of containter its best to store pieces of snake body?



Rogagol
5th February 2016, 17:53
Hi Guys,
i'm writing snake game, and i need help with concept.
What way will be the best to store pieces of snake body? I thought to create snake class (QObject) and item class (inherit from QGraphicsItem), and use QList to store all item's in snake class.
But i think this idea can create problem with add items to scene, and i get error on start writing code, when i tried to add items to QList (C2248 error).
So i want to ask expirienced users, how you design classes in that kind of program.
I hope that you understand me, and sorry for my english.

code_err
5th February 2016, 19:28
I am not sure what You want to do. Maybe this way. But I think that snake's data should be separated from bitmap data.


struct Node
{
enum NodeType
{
Node = 0,
Tail,
Head
};

Node()
{
x = 0;
y = 0;
type = Node;
}

NodeType type;
int x;
int y;
};

class Snake: public QObject
{
Q_OBJECT
public:
Snake();
~Snake();

private:
QList<Node> snakeNodes;
QPixmap snakeNode;
QPixmap snakeTail;
QPixmap snakeHead;
};

ChrisW67
6th February 2016, 20:54
There is no perfect or one way to store data to solve a particular problem. You need to use whatever works for you.

You are receiving compilation errors in your existing code because it violates basic C++ rules (not a Qt problem)


Compiler Error C2248

'member' : cannot access 'access' member declared in class 'class'
Members of a derived class cannot access private members of a base class. You cannot access private or protected members of class instances.
https://msdn.microsoft.com/en-us/library/tsbce2bh.aspx
At a guess you have a private QList in a class and are trying to access it directly from a derived class or unrelated class. You can consider changing the visibility from private to protected, or providing a public API to access the data from outside.

Rogagol
7th February 2016, 16:31
Thanks for your answers.
I am looking for way to use Qt classes to crate snake, and i thought that using QGraphicsItem its good idea, because i like the way of animation and other stuff in this class. Then i thought to connect this items with QObject to have signal and slots etc. But i don't now how connect QGraphiscItem with QObject.
Maybe you tell me how you would do this?
@code_err this way is not very Qt ;) If you now what i mean.

d_stranz
7th February 2016, 19:10
QGraphicsObject.

code_err is on the right track - it will be a better design if you keep your game logic and implementation separate from the code you use to display the game on screen. Of course, you can take advantage of the Qt Graphics / View architecture to create good graphics and animation. But the rules of the game should not be mixed up with the graphics. It makes for a design that is not very flexible or easy to change.

Rogagol
7th February 2016, 19:23
Ok is step forward for me ;) so i know to separate logic and display part of program, but how connect this parts? How you do this using Qt classes?


edit:
What you think about this way?:

Class "Snake", this class have QVector of class "Noode" objects, every node have QGraphicsItem object
In class Snake we have pointer to QGrphicsScene and by this pointer we communicate with scene, and add QGraphicsItem object frome "Noode" class.
It is good idea, or not? ;)

code_err
7th February 2016, 20:44
@code_err this way is not very Qt If you now what i mean. No, I don't ;) but... may the MOC be with you (If You know what I mean brother... ;-))

I think that You just should try a couple of different designs and then You will learn something on Your own experience. Every (pro)grammer wrote at least one snake game before he become pro, this is Your path, this is Your life, these are Your choices! (just kidding)

Rogagol
7th February 2016, 20:54
Thx for good words (i don't now is that phrasal verb exist in english ;))
I wrote couple of snake games, but first time i try to do this using Qt. I can try many ways, but i want to know "prettiest" :) ways from expirienced users, and i ask because i dont want do basic mistakes :)

code_err
7th February 2016, 21:03
Yeah but I don't know anything about QGraphicsItem so can only give You some lame answers. I would do drawing by subclassing QWidget and draw with QPainter on this widget but maybe it is wrong approach, I never drew anything with Qt.