-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbox.cpp
More file actions
88 lines (78 loc) · 1.68 KB
/
box.cpp
File metadata and controls
88 lines (78 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "box.h"
Box::Box()
{
angle=(qrand()%360);
setRotation(angle);
speed=20;
int startx=0;
int starty=0;
if((qrand() % 1))
{
startx=(qrand() % 200);
starty=(qrand() % 200);
}
else
{
startx=(qrand() % -100);
starty=(qrand() % -100);
}
setPos(mapToParent(startx,starty));
}
QRectF Box::boundingRect() const
{
return QRect(0,0,40,40);
}
void Box::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect=boundingRect();
QBrush brush(Qt::white);
if(scene()->collidingItems(this).isEmpty())
{
brush.setColor(Qt::white);
}
else
{
brush.setColor(Qt::red);
DoCol();
}
int txtspeed=speed;
QFont sansFont("Helvetica [Cronyx]", 14);
QString txt=QString::number(txtspeed);
painter->fillRect(rect,brush);
painter->drawRect(rect);
painter->setFont(sansFont);
painter->drawText(rect,Qt::AlignCenter,txt);
}
void Box::advance(int phase)
{
if(!phase)
return ;
//QPointF loc=this->pos();
setPos(mapToParent(0,-(speed)));
}
void Box::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
speed=speed+1;
QGraphicsItem::mousePressEvent(event);
}
void Box::DoCol()
{
if((qrand() %1))
{
setRotation(rotation()+ 180+ (qrand() % 10));
}
else
{
setRotation(rotation()+ 180+ (qrand() % -10));
}
QPointF newpoint=mapToParent(-(boundingRect().width()),-(boundingRect().width() +10));
if(!scene()->sceneRect().contains(newpoint))
{
newpoint=mapToParent(0,0);
setPos(newpoint);
}
else
{
setPos(newpoint);
}
}