Qt Tetris One

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.