The program is mainly composed of Onebox, Boxgroup and MyView three classes, and realizes small squares, block graphics and game scenes respectively.
Class Onebox:public Qgraphicsobject
{
Public
OneBox (const qcolor &color=qt::red);
QRECTF boundingrect () const;
void Paint (Qpainter *painter, const qstyleoptiongraphicsitem *option, Qwidget *widget);
Qpainterpath shape () const;
Private
Qcolor BrushColor;
};
Onebox::onebox (const Qcolor &color)
{
}
QRECTF Onebox::boundingrect () const
{
Qreal penwdith=1;
Return QRECTF ( -10-PENWDITH/2,-10,-PENWDITH/2,
20+penwdith,20+penwdith);
}
void OneBox::p aint (qpainter *painter, const qstyleoptiongraphicsitem *option, Qwidget *widget)
{
Use stickers for small squares
Painter->drawpixmap ( -10,-10,,20,20,qpixmap (":/myimage/5.jpg"));
Painter->setbrush (BrushColor);
Qcolor Pencolor=brushcolor;
Pencolor.setalpha (20);
Painter->setpen (PenColor);
Painter->drawrect ( -10,-10,20,20);
}
Qpainterpath Onebox::shape () const
{
Qpainterpath path;
Path.addrect ( -9.5,-9.5,19,19);
return path;
}
In order to use the collision detection function, two adjacent small squares in the block group are not detected as collisions, and the shape of the small squares is set to 0.5 pixels larger than the actual size. Because shape () is used to return shapes when detecting collisions, they are not detected to collide.
Function Description:
QRECTF:
The class defines a rectangle in a plane, using the precision of a floating-point type.
#include <QRect>
Public functions:
QRECTF (float x, float y, float width, float height)
Qreal is equivalent to double
Qt Tetris One