Prevent QPixmap from being moved out of QGraphicsView
I'd like to write very simple game.
Here's a part of code:
Code:
ui->graphicsView->setScene(scene);
pixmap is a creature that can be moved by player using WSAD keys.
I did it by this way:
Code:
void MainWindow
::keyPressEvent(QKeyEvent *event
){ switch(event->key()){
case Qt::Key_D:
pixmap->setPos(pixmap->pos().x()+1, pixmap->pos().y());
break;
case Qt::Key_A:
pixmap->setPos(pixmap->pos().x()-1, pixmap->pos().y());
break;
(...)
}
}
Now i want to prevent pixmap from being moved out of QGraphicsView.
How can I do it?
Thanks in advance.
Re: Prevent QPixmap from being moved out of QGraphicsView
try to use
Quote:
bool QGraphicsItem::collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const [virtual]
Re: Prevent QPixmap from being moved out of QGraphicsView
Thanks, but what should I pass as arguments of this function?
Re: Prevent QPixmap from being moved out of QGraphicsView
Hello, here are some thoughts:
if you're making a sprite-based game, you should either:
- have a QGraphicsScene that has the size of the game and use QGraphicsView scrollbars/scrolling to pan/zoom the scene
- use a fixed Screen-size QGraphicsScene and do do your own code to 'scroll' the items (or the 'insert at right edga, scroll all items left, and remove at left edge type of thing)
I assume you're using 1, so the QGraphicsScene has the size of the game area and your 'player' sprite is in there.
In this case:
A. when moving the player, just don't move it out of the scene
QRect sceneRect = scene()->sceneRect().toRect();
player->setPos(qBound(sceneRect.left(), player->pos(), sceneRect.right() - player_width));
this keeps the X coordinate between scene rect boundaries (assuming the item has its origin in the top-left corner)
B. the QGraphicsView can scroll to see the player
just call player->ensureVisible(); and the view will scroll
Good luck ;-)
Re: Prevent QPixmap from being moved out of QGraphicsView
Take a look at QGraphicsItem::itemChange(). Notice the example.
Re: Prevent QPixmap from being moved out of QGraphicsView
Thanks for the replies.
@jpn
Based on that example I wrote something like this:
Code:
void MainWindow
::keyPressEvent(QKeyEvent *event
){ switch(event->key()){
case Qt::Key_D:
pixmap->setPos(pixmap->pos().x()+5, pixmap->pos().y());
break;
case Qt::Key_A:
pixmap->setPos(pixmap->pos().x()-5, pixmap->pos().y());
break;
case Qt::Key_W:
pixmap->setPos(pixmap->pos().x(), pixmap->pos().y()-5);
break;
case Qt::Key_S:
pixmap->setPos(pixmap->pos().x(), pixmap->pos().y()+5);
break;
}
if(!scene->sceneRect().contains(pixmap->pos())){
box.setText("Player is out of the screen!");
box.exec();
}
}
But it doesn't work.
I think the problem is that everytime I move pixmap, QGraphicsScene expands, so scene->sceneRect().contains(pixmap->pos()) will always return true.
Re: Prevent QPixmap from being moved out of QGraphicsView
Quote:
bool QGraphicsItem::collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const [virtual]
as a QGraphicsItem you can pass QGraphicsRectItem(scene->sceneRect())
It means that you create graphics item with sboundary same as of graphicsscene has and then you check if your pixmap icollides with this RectItem
Re: Prevent QPixmap from being moved out of QGraphicsView
Yes, the scene will expand automatically by default, but shouldn't you define the game board area if that's the desired behavior? Something like:
Code:
{
QGraphicsview::resizeEvent(event);
if (scene())
}
Re: Prevent QPixmap from being moved out of QGraphicsView
When I reimplemented QGraphicsView::resizeEvent program crashes imidiately after being started.
In QtCreator's console I have:
Quote:
The program has unexpectedly finished.
Re: Prevent QPixmap from being moved out of QGraphicsView
See the backtrace from the debugger.