0

Hud Example

Hi, I have to create a HUD for a Supermario Project with C++ and Qt. I have to recreate Supermario Bros 3 World 2 - Level 1 . So I wrote all the "static elements" like the "M" of Mario and the "World 2" element. Now I want to ask you how can I write dynamic writings like timer level (300 s) or the points counter?

This is my Hud.cpp file, in which I create the Hud panel.

Hud::Hud(const QPoint & position)
{

    setPos(position);

    // create collage texture
    QPixmap collage(16 * 16 + 4, 3 * 16);
    QPainter painter(&collage);


    painter.fillRect(collage.rect(), Qt::black);
    painter.drawPixmap(10, 5, Sprites::instance()->get("hud"));
    painter.drawPixmap(49, 11, Sprites::instance()->get("font-2"));
    painter.drawPixmap(14, 18, Sprites::instance()->get("font-Mario"));

    //lifes 
    //AT THE BEGININNG MARIO HAS 3 LIFES 
    //THIS IS A STATIC WRITING 
    //HOW CAN I MAKE IT DYNAMIC? (if mario dies, show he has 2 lifes? )   
    painter.drawPixmap(45, 19, Sprites::instance()->get("font-3"));


    painter.end();

    setPixmap(collage);
}
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
theorly_
  • 9
  • 2
  • Generally you shouldn't draw once, you should draw and *redraw* as needed. There are Qt events that you can use to know when you need to redraw your components. – Some programmer dude May 12 '20 at 16:10
  • So I have to create the "main window" and draw it every time a component is updated? – theorly_ May 12 '20 at 16:14
  • Usually you have to reimplement a `paintEvent()` method and don't care about calling this, the Qt callback should do the work for you. Depends on what you really use – thibsc May 12 '20 at 17:58
  • Thanks for the answer. Now I will try to implement a paintEvent() method and I will let you know!! – theorly_ May 13 '20 at 12:48

0 Answers0