Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. [solved] deal cards and add to the scene
Forum Updated to NodeBB v4.3 + New Features

[solved] deal cards and add to the scene

Scheduled Pinned Locked Moved Game Development
29 Posts 2 Posters 10.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marcius
    wrote on last edited by
    #1

    Hey guys,

    I can't seem to figure out the best way to do this. Here is what I've got (to put it simply):

    A Card class that has its suit and rank;
    A Deck class made of 32 Cards. In here a Card is assigned a suit and a rank using Card's setter function. Then the Deck is shuffled and Cards dealt to 3 players (using 3 vectors, 10 cards each).

    At this stage, Cards are only sets of numbers, i.e. 1/5, 3/4 etc. The first number is a suit (1-4) and the second is a rank (1-8). And that's where I get stuck. How do I incorporate Qt so that every card is assigned its corresponding GIF image (let's say 1/1 would be seven of hearts, 2/2 - eight of diamonds etc) and put in a scene?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You could use a QMap<QPair<int, int>, QPixmap> that will be your card deck.

      Then copy the keys (QList<QPair<int, int>> cards = myMap.keys() and run std::random_shuffle on them.

      From there you can create your scene getting the pixmap base on your randomized cards.

      Hope it helps.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Marcius
        wrote on last edited by
        #3

        Thank you for your input, SGaist. I'll give it a try and see how far I get with this. Will be back with an update. Meanwhile, if anyone else has any other ideas, that would be greatly appreciated!

        Marcius

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Marcius
          wrote on last edited by
          #4

          Hey,

          After some time playing around, I ended up with this.

          My Card class inherits QGraphicsPixmapItem. When deck is created and all the cards get their suits and values, they also get a corresponding picture using setPixmap function. Then I call shuffle and deal functions, the later looks something like this (a loop here to put the cards onto the scene) :

          @
          QGraphicsPixmapItem *item = new QGraphicsPixmapItem();

          item->setPixmap(QPixmap(card[i].pixmap()));
          scene.addItem(item);
          @

          I'm happy with the result so far, but it's not tested fully yet, so don't know if it's going to do exactly what I want.

          Please let me know your thoughts.

          Thanks!

          EDIT:

          Although now I think after the cards are put on the screen, they become simply pictures and are not associated with Card class anymore because of this
          @ item->setPixmap(QPixmap(card[i].pixmap())); @

          Any thoughts on this?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marcius
            wrote on last edited by
            #5

            Sorry to bombard this thread with questions, but how could I use
            @ scene.addItem(item); @ item being my Card object? Since all of them already are QGraphicsPixmapItems and have Qpixmaps assigned. I think that would help me a lot.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You could make a subclass of QGraphicsPixmapItem and make it your card e.g. add getters for suit/rank to that subclass

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Marcius
                wrote on last edited by
                #7

                Pardon my ignorance (I'm still very new to C++), but when you say 'make a subclass of QGraphicsPixmapItem' is it
                @ class Card : public QGraphicsPixmapItem @
                or the other way around? And what is a difference? I tried the first one and it didn't work the way I wanted to, or I did something wrong. By the way, my original Card class has its setter and getter methods.

                Thanks a lot for your help!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Yes, it's that.

                  What didn't work the way you wanted ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Marcius
                    wrote on last edited by
                    #9

                    Oh, sorry, I simply forgot to pass my Card object as a reference to addItem, it seems to be working now.

                    Ok, so I've got a couple of cards on the screen now and a mouse press event, that looks like this:
                    @
                    void Card::mousePressEvent(QGraphicsSceneMouseEvent *Card)
                    {
                    qDebug() << "I clicked a card ";
                    }
                    @

                    This is only for testing at the moment. Now, when I click on a Card, I'd like qDebug() to output its suit and value. As I mentioned before, I do have getter methods for that, but not too sure where to go from here.

                    Again, much appreciated, SGaist!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      It should rather be:

                      @
                      void Card::mousePressEvent(QGraphicsSceneMouseEvent *event) <- here
                      {
                      qDebug() << "I clicked a card ";
                      }@

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Marcius
                        wrote on last edited by
                        #11

                        There's still heaps of work left to be done, but you, sir, got me helluva closer :)

                        Thank you!

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          You're welcome !

                          Don't hesitate to take some times to look at Qt's documentation examples. They might contain a lot of stuff you can reuse/get inspiration from for your application

                          Happy coding !

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Marcius
                            wrote on last edited by
                            #13

                            Hi,

                            Not too happy about coming back to the same thread, but I'm facing an issue.

                            @
                            class Card : public QGraphicsPixmapItem
                            @

                            This line generates a C2248 error, saying I cannot access private member in a QGraphicsPixmapItem class.

                            I have only a slight idea as to why this happens, but unfortunately no clue what to do about it. Funny thing is, it compiled no problem before, although I haven't made any changes to the Card class or any other relevant part of the code..

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              What's the exact error you are getting ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Marcius
                                wrote on last edited by
                                #15

                                error C2248: 'QGraphicsPixmapItem::QGraphicsPixmapItem' : cannot access private member declared in class 'QGraphicsPixmapItem'

                                error C2248: 'QGraphicsPixmapItem::operator =' : cannot access private member declared in class 'QGraphicsPixmapItem'

                                And that's compiler's output:

                                1> c:\qt\5.2.1\msvc2012\include\qtwidgets\qgraphicsitem.h(866) : see declaration of 'QGraphicsPixmapItem::QGraphicsPixmapItem'
                                1> c:\qt\5.2.1\msvc2012\include\qtwidgets\qgraphicsitem.h(822) : see declaration of 'QGraphicsPixmapItem'
                                1> This diagnostic occurred in the compiler generated function 'Card::Card(const Card &)'

                                and

                                'Card &Card::operator =(const Card &)'

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  Marcius
                                  wrote on last edited by
                                  #16

                                  I can't believe I did it (given my almost non-existent experience), but I finally got rid of the errors by providing my own class copy and assignment operator constructors. Please keep tuned though, will see where it leads me to.. :)

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    What you are doing is risky, you've got the error because the base class (QGraphicsPixmapItem ) explicitly forbids the copy.

                                    If you look at QGraphicsScene you see that you addItem takes a pointer to an item

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Marcius
                                      wrote on last edited by
                                      #18

                                      I see what you're saying. I actually may be having problems because of that at the moment (not sure yet though). How would you go about this issue then? Quite frankly, I still don't know which which part of my program causes the error.

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        Just allocate your Cards on the heap and not on the stack

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          Marcius
                                          wrote on last edited by
                                          #20

                                          I tried that, although I still can't remove copy and assignment operator constructors without getting the same error..

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved