Question

I'm trying to implement a flowing background item for my scene (just like flappy bird)

Form::Form(QWidget *parent) : QMainWindow(parent), ui(new Ui::Form)
{
    ui->setupUi(this);
    showMaximized();

    background = new QGraphicsPixmapItem();
    background->setPixmap(QPixmap::fromImage(QImage("..//Game//images//background.jpg")));

    scene = new QGraphicsScene();
    ui->view->setScene(scene);
    scene->addItem(background);

    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(timerTick()));
    timer->start(1);
}

// ...

void Form::timerTick()
{
    background->moveBy(-0.2, 0);
}

Obvious problem is, item disappears after a while. I want to re-render and keep up the flow of the scene when the period ends. How could I do this?

I tried to change the pixmap item pixel by pixel, but it was way too slow.

Thanks in advance

Was it helpful?

Solution

I propose a different approach to your problem:

You can have a big background containing all the levels for instance. The trick is to extend the background image with the size of your view, for flappy birds where the height is fixed:

background_width + screen_width

You can construct this extended background in memory form your image.

Then you can use to navigate in your image:

void QPixmap::scroll ( int dx, int dy, const QRect & rect, QRegion * exposed = 0 )

And set the QGraphicsPixmapItem pixmap with:

void QGraphicsPixmapItem::setPixmap ( const QPixmap & pixmap )

When you reach the end of your background, you simply jump in the corresponding position at the beginning of the pixmap.

In conclusion, your QGraphicsPixmapItem never moves, but the corresponding QPixmap is extracted from the extended background.

Edit: Added schema Scheme

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top