-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathball.cpp
More file actions
217 lines (195 loc) · 7.82 KB
/
Copy pathball.cpp
File metadata and controls
217 lines (195 loc) · 7.82 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "ball.h"
#include "player.h"
#include "border.h"
#include <QDebug>
#include <QMediaPlayer>
#include "socketthread.h"
#include <QLabel>
#include <stdlib.h>
#include <unistd.h>
Ball::Ball(RealPlayer *p1, RealPlayer *p2, QLabel *l1, QLabel *l2, double cX, double cY, QObject *parent) : QObject(parent), Circle(cX, cY), animation(new QPropertyAnimation(this))// change the circle r, x, y too appropriate things
{
setPos(0, 0);
this->l1 = l1; this->l2 = l2;
this->p1 = p1; this->p2 = p2;
this->vX = 0; this->vY = 0;
setPixmap(QPixmap(":/Images/ball.png"));
r = 15;
animation->setPropertyName("movingBall");
animation->setTargetObject(this);
anForMovingX = new QPropertyAnimation(); anForMovingY = new QPropertyAnimation(this);
anForMovingX->setPropertyName("changeX"); anForMovingY->setPropertyName("changeY");
rePositioning();
}
//this func is for changing coordinates for QPropertyAnimation
void Ball::setChangeX(int)
{
if(this->pos().x() - 624.5 > 0) {
if(abs(this->pos().x() - 5) > 624.5)
this->setX(this->pos().x() - 5);
else {
this->setX(this->pos().x() - abs(this->pos().x() - 624.5));
this->anForMovingX->stop();
}
}
else {
if(abs(this->pos().x() + 5) < 624.5)
this->setX(this->pos().x() + 5);
else {
this->setX(this->pos().x() + abs(this->pos().x() - 624.5));
this->anForMovingX->stop();
}
}
}
//this func changes the coordinate for QPropertyAnimation
void Ball::setChangeY(int)
{
if(this->pos().y() - 325 > 0) {
if(abs(this->pos().y() - 5) > 325)
this->setY(this->pos().y() - 5);
else {
this->setY(this->pos().y() - abs(this->pos().y() - 325));
this->anForMovingY->stop();
}
}
else {
if(abs(this->pos().y() + 5) < 325)
this->setY(this->pos().y() + 5);
else {
this->setY(this->pos().y() + abs(this->pos().y() - 325));
this->anForMovingY->stop();
}
}
}
//this is for moving circle and checking collisions of circle
void Ball::setMoving(int)
{
//add some collision check and ...
this->setPos(this->pos().x() + vX, this->pos().y() + vY);
if(this->pos().x() <= 109 && (this->pos().y() <= 226 || this->pos().y() - this->r * 2 >= 458)) {
this->vX *= -.8;
this->setX(111);
}
if(this->pos().x() + this->r * 2 >= 1175 && (this->pos().y() <= 226 || this->pos().y() - this->r * 2 >= 458)) {
this->vX *= -.8;
this->setX(1173 - this->r * 2);
}
if(this->pos().y() <= 10) {
this->setY(11);
this->vY *= -.8;
}
if(this->pos().y() + this->r * 2 >= 674) {
this->setY(674 - this->r * 2);
this->vY *= -.8;
}
QString str = "5 " + QString::number(this->pos().x()) + " " + QString::number(this->pos().y());
str.append(QChar(23));
thread->sendMess(str);
//checking for goal,
if(this->xC(this->pos().x()) > 1177 && this->pos().y() > 228 && this->pos().y() < 453) {
this->vX = 0;
this->vY = 0;
this->animation->stop();
return;
}
if(this->xC(this->pos().x()) <103 && this->pos().y() > 228 && this->pos().y() < 456) {
this->vX = 0;
this->vY = 0;
this->animation->stop();
return;
}
vX > 0 ? vX -= fX : vX += fX;
vY > 0 ? vY -= fY : vY += fY;
if(abs(vX) < .3 && abs(vY) < .3) {
this->vX = 0;
this->vY = 0;
animation->stop();
return;
}
QList<QGraphicsItem *> l = this->collidingItems();
for(int i = 0; i < l.size(); i++) {
//checking collision between player and ball
if(Player *c = dynamic_cast<Player *> (l[i])) {
//adding sounds of collision between ball and player...
QMediaPlayer *shot = new QMediaPlayer();
shot->setMedia(QUrl("qrc:/sounds/shot.wav"));
shot->setVolume(110);
shot->play();
double thisCx = xC(this->pos().x()), thisCy = yC(this->pos().y()), otherCx = c->xC(c->pos().x()), otherCy = c->yC(c->pos().y());
thisCy *= -1; otherCy *= -1;
if(thisCx - otherCx == 0) {
double tmp = this->vY;
this->vY = c->vY;
c->vY = tmp;
}
else {
//calculating speed of ball and palyer after collision
double mCenters = (thisCy - otherCy) / (thisCx - otherCx);
double mCNorm = -1. / mCenters;
double picOfVxOnMC = coss(mCenters) * vX, picOfVxOnMCN = coss(mCNorm) * vX, picOfVyOnMC = sinn(mCenters) * vY * -1, picOfVyOnMCN = sinn(mCNorm) * vY * -1;
double sPicOfVxONMC = coss(mCenters) * c->vX, sPicofVxOnMCN = coss(mCNorm) * c->vX, sPicOfVyOnMC = sinn(mCenters) * c->vY * -1, sPicOfVyOnMCN = sinn(mCNorm) * c->vY * -1;
if(mCenters < 0) {
picOfVxOnMC *= -1;
sPicOfVxONMC *= -1;
}
else {
picOfVxOnMCN *= -1;
sPicofVxOnMCN *= -1;
}
double fOnMC = picOfVxOnMC + picOfVyOnMC, fOnMCN = picOfVxOnMCN + picOfVyOnMCN, sOnMC = sPicOfVxONMC + sPicOfVyOnMC, sOnMCN = sPicofVxOnMCN + sPicOfVyOnMCN;
double tmp = fOnMC;
fOnMC = sOnMC;
sOnMC = tmp;
if(mCenters < 0) {
double fVx1 = fOnMCN * coss(mCNorm), fVx2 = fOnMC * coss(mCenters) * -1, fVy1 = fOnMCN * sinn(mCNorm) * -1, fVy2 = fOnMC * sinn(mCenters) * -1;
this->vX = fVx1 + fVx2; this->vY = fVy1 + fVy2;
double sVx1 = sOnMCN * coss(mCNorm), sVx2 = sOnMC * coss(mCenters) * -1, sVy1 = sOnMCN * sinn(mCNorm) * -1, sVy2 = sOnMC * sinn(mCenters) * -1;
c->vX = sVx1 + sVx2; c->vY = sVy1 + sVy2;
}
else {
double fVx1 = fOnMCN * coss(mCNorm) * -1, fVx2 = fOnMC * coss(mCenters), fVy1 = fOnMCN * sinn(mCNorm) * -1, fVy2 = fOnMC * sinn(mCenters) * -1;
this->vX = fVx1 + fVx2; this->vY = fVy1 + fVy2;
double sVx1 = sOnMCN * coss(mCNorm) * -1, sVx2 = sOnMC * coss(mCenters), sVy1 = sOnMCN * sinn(mCNorm) * -1, sVy2 = sOnMC * sinn(mCenters) * -1;
c->vX = sVx1 + sVx2; c->vY = sVy1 + sVy2;
}
//this->setPos(this->pos().x() + this->vX, this->pos().y() + this->vY);
//c->setPos(c->pos().x() + c->vX, c->pos().y() + c->vY);
this->startAnimaion();
c->startAnimaion();
}
}
}
}
void Ball::startAnimaion()
{
//function for starting animation of Player
double tmp = sqrt(vX * vX + vY * vY);
fX = abs(vX) / (tmp * 10), fY = abs(vY) / (tmp * 10);
animation->pause();
animation->setStartValue(vX);
animation->setEndValue(0);
animation->setDuration(10000);
animation->start();
}
//this item checks if things collide with ball...
bool Ball::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
if (const Player *b = dynamic_cast<const Player *> (other)) {
double tX = this->pos().x() + 15, tY = this->pos().y() + 15, oX = b->pos().x() + 35, oY = b->pos().y() + 35;
double tmp = sqrt((tX - oX) * (tX - oX) + (tY - oY) * (tY - oY));
if(tmp <= 50) {
return true;
}
return false;
}
return false;
}
//animation of starting...
void Ball::rePositioning()
{
anForMovingX->setDuration(3000); anForMovingY->setDuration(3000);
anForMovingX->setStartValue(this->pos().x()); anForMovingX->setEndValue(624.5);
anForMovingY->setStartValue(this->pos().y()); anForMovingY->setEndValue(325);
anForMovingX->setTargetObject(this); anForMovingY->setTargetObject(this);
anForMovingX->start(); anForMovingY->start();
}