bikonja
13th February 2015, 12:04
Hello guys, this is yet another topic about coordinates.
im making a tetris game and i'm having some trouble setting up the scene.
i'm aware of the documentation, but learning another library on my own is quite new to me, and im kind of lost in all those words.
consider the following code
myRect::myRect(QGraphicsItem *parent) : QGraphicsItem(parent)
{
setFlag(QGraphicsItem::ItemIsFocusable);
}
QRectF myRect::boundingRect() const
{
return QRectF(0,0,50,50);
}
void myRect::keyPressEvent(QKeyEvent *event)
{
switch( event->key())
{
case Qt::Key_Left:{
setPos(x()-10,y());
checkBounds();
break;
}
case Qt::Key_Right:{
setPos(x()+10,y());
checkBounds();
break;
}
case Qt::Key_Up:{
setPos(x(),y()-10);
checkBounds();
break;
}
case Qt::Key_Down:{
setPos(x(),y()+10);
checkBounds();
break;
}
}
update();
}
void myRect::checkBounds()
{
if (x() < 0)
{
setPos(0, y());
}
else if (x() + boundingRect().right() > scene()->width())
{
setPos(scene()->width() - boundingRect().width(), y());
}
if (y() < 0)
{
setPos(x(), 0);
}
else if ( y()+ boundingRect().bottom() > scene()->height())
{
setPos(x(), scene()->height() - boundingRect().height());
}
}
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::yellow);
painter->drawRoundRect(0,0,50,50,5,5);
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
scene = new QGraphicsScene();
ui->view->setScene(scene);
scene->setSceneRect(0,0,ui->view->frameSize().width(),
ui->view->frameSize().height());
rect = new myRect;
scene->addItem(rect);
rect->setPos(scene->width()/2,0);
rect->setFocus();
}
so my question(s) is this.
i'm under the impresion my scene should have the same dimensions and the view.
but when i add my item on the scene it aint possitioned in the middle.
can someone explain to me whats going on here?
also.
lets say i want to have 8 columns on my board and 16 rows for block movement.
so my I block could be placed 2 times horizontally rotated on the board, and 8 times vertically.
10955
excuse me for my paint skills.
can you help me out with setting that up?
im making a tetris game and i'm having some trouble setting up the scene.
i'm aware of the documentation, but learning another library on my own is quite new to me, and im kind of lost in all those words.
consider the following code
myRect::myRect(QGraphicsItem *parent) : QGraphicsItem(parent)
{
setFlag(QGraphicsItem::ItemIsFocusable);
}
QRectF myRect::boundingRect() const
{
return QRectF(0,0,50,50);
}
void myRect::keyPressEvent(QKeyEvent *event)
{
switch( event->key())
{
case Qt::Key_Left:{
setPos(x()-10,y());
checkBounds();
break;
}
case Qt::Key_Right:{
setPos(x()+10,y());
checkBounds();
break;
}
case Qt::Key_Up:{
setPos(x(),y()-10);
checkBounds();
break;
}
case Qt::Key_Down:{
setPos(x(),y()+10);
checkBounds();
break;
}
}
update();
}
void myRect::checkBounds()
{
if (x() < 0)
{
setPos(0, y());
}
else if (x() + boundingRect().right() > scene()->width())
{
setPos(scene()->width() - boundingRect().width(), y());
}
if (y() < 0)
{
setPos(x(), 0);
}
else if ( y()+ boundingRect().bottom() > scene()->height())
{
setPos(x(), scene()->height() - boundingRect().height());
}
}
void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::yellow);
painter->drawRoundRect(0,0,50,50,5,5);
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
scene = new QGraphicsScene();
ui->view->setScene(scene);
scene->setSceneRect(0,0,ui->view->frameSize().width(),
ui->view->frameSize().height());
rect = new myRect;
scene->addItem(rect);
rect->setPos(scene->width()/2,0);
rect->setFocus();
}
so my question(s) is this.
i'm under the impresion my scene should have the same dimensions and the view.
but when i add my item on the scene it aint possitioned in the middle.
can someone explain to me whats going on here?
also.
lets say i want to have 8 columns on my board and 16 rows for block movement.
so my I block could be placed 2 times horizontally rotated on the board, and 8 times vertically.
10955
excuse me for my paint skills.
can you help me out with setting that up?