This repository was archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.cpp
More file actions
72 lines (61 loc) · 1.17 KB
/
Copy pathball.cpp
File metadata and controls
72 lines (61 loc) · 1.17 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
#include "ball.h"
ball::ball(QWidget *parent) : QWidget(parent)
{
p = parent;
x = p->rect().width()/2;
y = p->rect().height()/2;
}
QRect ball::shape()
{
QRect out(x,y,ballshape,ballshape);
return out;
}
void ball::moveBall()
{
if(x <= 0){
emit scored(true);
}else if(x+ballshape >= p->rect().width()){
emit scored(false);
}
if(y <= 0 || y+ballshape >= p->rect().height())moveY *= -1;
x += moveX;
y += moveY;
QRect myRect(x,y,ballshape,ballshape);
emit ballMoves(myRect);
}
void ball::drawBall(QPainter &painter)
{
painter.drawEllipse(x,y,ballshape,ballshape);
}
void ball::resetBall()
{
x = p->rect().width()/2;
y = p->rect().height()/2;
if(rand()%2){
moveX = ballspeed;
moveY = ballspeed;
}
else{
moveX = -1*ballspeed;
moveY = -1*ballspeed;
}
}
void ball::bounce(int type)
{
switch (type) {
case 0:
moveX *= -1.5;
moveY = -1*(moveY+1);
x += moveX;
y += moveY;
break;
case 1:
moveX *= -1;
moveY = -1*(moveY+6);
x += moveX;
y += moveY;
break;
default:
break;
}
}