-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgameplay.cpp
More file actions
125 lines (103 loc) · 2.97 KB
/
Copy pathgameplay.cpp
File metadata and controls
125 lines (103 loc) · 2.97 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "gameplay.h"
#include <QGraphicsItem>
#include <QDebug>
#include <QTimer>
#include <QEvent>
#include <QKeyEvent>
Gameplay::Gameplay(QGraphicsScene & scene, QGraphicsItem *p1, QGraphicsItem *p2, QGraphicsItem *ball, QObject *parent) :
QObject(parent),
iScene ( scene ),
iP1 ( p1 ),
iP2 ( p2 ),
iBall ( ball ),
iBallDirection ( -3, -3 ),
iP1Direction( 0 ),
iP2Direction( 0 )
{
iScene.setSceneRect(0, 0, 350, 320);
iScene.addItem(iP1);
iScene.addItem(iP2);
iScene.addItem(iBall);
iP1->setPos(135, 5);
iP2->setPos(135, 300);
iBall->setPos(150, 150);
iTimer = new QTimer(this);
iTimer->setInterval(12);
iTimer->start();
QObject::connect(iTimer, SIGNAL(timeout()), this, SLOT(tick()));
}
void Gameplay::tick()
{
qreal newX = iBall->pos().x() + iBallDirection.x();
qreal newY = iBall->pos().y() + iBallDirection.y();
qreal pnewx = iP1->pos().x() + iP1Direction;
qreal p2newx = iP2->pos().x() + iP2Direction;
if ( ( newX < 0 ) || ( newX + iBall->boundingRect().right() > iScene.sceneRect().right() ) )
{
iBallDirection.rx() *= -1;
}
if ( ( newY < 0 ) || ( newY + iBall->boundingRect().bottom() > iScene.sceneRect().bottom() ) )
{
// 1 for hitting the bottom wall, -1 for hitting the top wall
emit goal(newY / abs(newY));
iBallDirection.ry() *= -1;
}
if ( ( pnewx < 0 ) || ( pnewx + iP1->boundingRect().right() > iScene.sceneRect().right() ) )
{
iP1Direction = 0;
}
if ( ( p2newx < 0 ) || ( p2newx + iP1->boundingRect().right() > iScene.sceneRect().right() ) )
{
iP2Direction = 0;
}
if ( ( iP1->collidesWithItem(iBall) ) && ( iBallDirection.y() < 0 ) )
{
iBallDirection.ry() *= -1;
}
if ( ( iP2->collidesWithItem(iBall) ) && ( iBallDirection.y() > 0 ) )
{
iBallDirection.ry() *= -1;
}
if ( qrand() % 10 == 0 )
{
iP2Direction = calculateP2Direction();
}
iBall->moveBy(iBallDirection.x(), iBallDirection.y());
iP1->moveBy(iP1Direction, 0);
iP2->moveBy(iP2Direction, 0);
}
bool Gameplay::eventFilter(QObject *target, QEvent *e)
{
Q_UNUSED(target);
bool handled = false;
if ( e->type() == QEvent::KeyPress )
{
QKeyEvent *keyEvent = (QKeyEvent *)e;
if ( keyEvent->key() == Qt::Key_Left )
{
iP1Direction = (iP1Direction == 0 ? -5 : 0);
handled = true;
}
else if ( keyEvent->key() == Qt::Key_Right )
{
iP1Direction = (iP1Direction == 0 ? 5 : 0);
handled = true;
}
}
return handled;
}
qreal Gameplay::calculateP2Direction()
{
qreal dir = 0;
if ( iBall->pos().x() + iBallDirection.x() > iP2->sceneBoundingRect().right() )
{
// move right
dir = 5;
}
else if ( iBall->pos().x() + iBallDirection.x() < iP2->sceneBoundingRect().left() )
{
// move left
dir = -5;
}
return dir;
}