-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.cpp
77 lines (60 loc) · 1.12 KB
/
ball.cpp
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
#include "ball.h"
#include "settings.h"
#include <QMediaPlayer>
#include <cmath>
Ball::Ball(QGraphicsItem *parent)
:QGraphicsRectItem(parent)
{
setRect(0 , 0 , Global::Height/41*Global::ballSizeConst , Global::Height/41*Global::ballSizeConst);
reflect_sound = new QMediaPlayer;
reflect_sound->setMedia(QUrl("qrc:/sounds/reflect.mp3"));
}
void Ball::resize()
{
setRect(0 , 0 , Global::Height/41*Global::ballSizeConst , Global::Height/41*Global::ballSizeConst);
speed = Global::Height/343.0 * Global::ballSpeedConst;
dx = speed*1.80;
dy = random(-speed,speed);
}
void Ball::soundPlay()
{
reflect_sound->play();
}
void Ball::setVolume(int sound)
{
reflect_sound->setVolume(sound);
}
void Ball::move()
{
setPos(x()+dx,y()+dy);
}
void Ball::changeTurn()
{
if(Turn == Left)
{
Turn = Right;
dx = -abs(dx);
}
else
{
Turn = Left;
dx = abs(dx);
}
dy = 0;
}
double Ball::getDX() const
{
return dx;
}
void Ball::reflectY()
{
dy = -dy;
}
void Ball::changeDY()
{
dy = random(-speed,speed);
}
void Ball::reflectX()
{
dx = -dx;
}