Results 1 to 9 of 9

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

  1. #1
    Join Date
    Feb 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Snake game, in what kind of containter its best to store pieces of snake body?

    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.

  2. #2
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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.
    Qt Code:
    1. struct Node
    2. {
    3. enum NodeType
    4. {
    5. Node = 0,
    6. Tail,
    7. Head
    8. };
    9.  
    10. Node()
    11. {
    12. x = 0;
    13. y = 0;
    14. type = Node;
    15. }
    16.  
    17. NodeType type;
    18. int x;
    19. int y;
    20. };
    21.  
    22. class Snake: public QObject
    23. {
    24. Q_OBJECT
    25. public:
    26. Snake();
    27. ~Snake();
    28.  
    29. private:
    30. QList<Node> snakeNodes;
    31. QPixmap snakeNode;
    32. QPixmap snakeTail;
    33. QPixmap snakeHead;
    34. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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)
    Quote Originally Posted by MSDN
    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.

  4. #4
    Join Date
    Feb 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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.

  6. #6
    Join Date
    Feb 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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?
    Last edited by Rogagol; 7th February 2016 at 20:29.

  7. #7
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    @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)
    Last edited by code_err; 7th February 2016 at 21:00.

  8. #8
    Join Date
    Feb 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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

  9. #9
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Snake game, in what kind of containter its best to store pieces of snake body?

    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.

Similar Threads

  1. Replies: 1
    Last Post: 4th January 2012, 14:59
  2. Image/Pixmap after scaling parcelled into 4 pieces.
    By Douglish in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2011, 09:28
  3. Replies: 1
    Last Post: 22nd May 2010, 07:38
  4. Chess Program Drag and drop pieces
    By cwnelatury in forum Newbie
    Replies: 2
    Last Post: 29th April 2009, 22:43
  5. In what order do you create program pieces?
    By Backslash in forum Newbie
    Replies: 2
    Last Post: 15th July 2007, 18:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.