41 #include <QGraphicsScene> 43 #include <QStyleOption> 47 static const double Pi = 3.14159265358979323846264338327950288419717;
60 : angle(0), speed(0), mouseEyeDirection(0),
61 color(qrand() % 256, qrand() % 256, qrand() % 256)
63 rotate(qrand() % (360 * 16));
64 startTimer(1000 / 33);
70 return QRectF(-18 - adjust, -22 - adjust,
71 36 + adjust, 60 + adjust);
77 path.addRect(-10, -20, 20, 40);
81 void Mouse::paint(QPainter *painter,
const QStyleOptionGraphicsItem *, QWidget *)
84 painter->setBrush(
color);
85 painter->drawEllipse(-10, -20, 20, 40);
88 painter->setBrush(Qt::white);
89 painter->drawEllipse(-10, -17, 8, 8);
90 painter->drawEllipse(2, -17, 8, 8);
93 painter->setBrush(Qt::black);
94 painter->drawEllipse(QRectF(-2, -22, 4, 4));
101 painter->setBrush(scene()->collidingItems(
this).isEmpty() ? Qt::darkYellow : Qt::red);
102 painter->drawEllipse(-17, -12, 16, 16);
103 painter->drawEllipse(1, -12, 16, 16);
106 QPainterPath path(QPointF(0, 20));
107 path.cubicTo(-5, 22, -5, 22, 0, 25);
108 path.cubicTo(5, 27, 5, 32, 0, 30);
109 path.cubicTo(-5, 32, -5, 42, 0, 35);
110 painter->setBrush(Qt::NoBrush);
111 painter->drawPath(path);
117 QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0));
118 if (lineToCenter.length() > 150) {
119 qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length());
120 if (lineToCenter.dy() < 0)
121 angleToCenter =
TwoPi - angleToCenter;
124 if (angleToCenter < Pi && angleToCenter >
Pi / 4) {
127 }
else if (angleToCenter >=
Pi && angleToCenter < (
Pi +
Pi / 2 +
Pi / 4)) {
131 }
else if (::sin(
angle) < 0) {
133 }
else if (::sin(
angle) > 0) {
138 QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
140 << mapToScene(-30, -50)
141 << mapToScene(30, -50));
142 foreach (QGraphicsItem *item, dangerMice) {
146 QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
147 qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length());
148 if (lineToMouse.dy() < 0)
149 angleToMouse =
TwoPi - angleToMouse;
152 if (angleToMouse >= 0 && angleToMouse <
Pi / 2) {
155 }
else if (angleToMouse <= TwoPi && angleToMouse > (
TwoPi -
Pi / 2)) {
162 if (dangerMice.size() > 1 && (qrand() % 10) == 0) {
164 angle += (qrand() % 100) / 500.0;
166 angle -= (qrand() % 100) / 500.0;
169 speed += (-50 + qrand() % 100) / 100.0;
171 qreal dx = ::sin(
angle) * 10;
175 setPos(mapToParent(0, -(3 + sin(
speed) * 3)));
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
QRectF boundingRect() const
static qreal normalizeAngle(qreal angle)
void timerEvent(QTimerEvent *event)
QPainterPath shape() const