-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.cpp
More file actions
85 lines (79 loc) · 1.48 KB
/
ball.cpp
File metadata and controls
85 lines (79 loc) · 1.48 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
#include "ball.h"
Ball::Ball(int id)
{
this->id = id;
changeDirection();
this->currentX = 8;
this->currentY = 8;
this->lastX = 0;
this->lastY = 0;
this->bounceCounter = 0;
}
Ball::~Ball()
{
}
void Ball::changeDirection()
{
this->direction = rand() % 7 + 0;
}
bool Ball::toUpdate()
{
return currentX != lastX || currentY != lastY;
}
bool Ball::cordsValid(int y, int x)
{
return !(x <= 1 || x >= 15 || y <= 0 || y >= 16);
}
void Ball::updateBall()
{
int prevDirection = direction;
int tmpX = currentX;
int tmpY = currentY;
while (true)
{
switch (direction)
{
case 0:
tmpY--;
break;
case 1:
tmpX++;
tmpY--;
break;
case 2:
tmpX++;
break;
case 3:
tmpX++;
tmpY++;
break;
case 4:
tmpY++;
break;
case 5:
tmpX--;
tmpY++;
break;
case 6:
tmpX--;
break;
case 7:
tmpY--;
tmpX--;
break;
}
if (cordsValid(tmpY, tmpX))
break;
else{
changeDirection();
tmpX = currentX;
tmpY = currentY;
}
}
lastX = currentX;
lastY = currentY;
currentX = tmpX;
currentY = tmpY;
if(prevDirection != direction)
bounceCounter++;
}