-
Notifications
You must be signed in to change notification settings - Fork 38
/
ball.cpp
215 lines (166 loc) · 4.25 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
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
/*
Arduino Breakout
Copyright (C) 2015 João André Esteves Vilaça
https://github.com/vilaca/Handheld-Color-Console
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef BALLCPP
#define BALLCPP
#include "tiles.cpp"
#include "config.h"
#include "beeping.cpp"
#include "paddle.cpp"
class Ball
{
const int SCORE[7] = {0, 1, 1, 3, 5, 5, 7};
int left, right, top, bottom;
float x, y, xi, yi;
public:
Ball(int left_, int right_, int top_, int bottom_)
{
left = left_;
right = right_;
top = top_;
bottom = bottom_;
}
void clear()
{
tft.fillRectangle(x, y, BALL_R * 2, BALL_R * 2, BLACK);
}
void draw()
{
tft.fillRectangle(x, y, BALL_R * 2, BALL_R * 2, BALL_COLOR);
}
void setXY(int x_, int y_)
{
x = x_;
y = y_;
}
void setX(int x_)
{
x = x_;
}
void setXiYi(int xi_, int yi_)
{
xi = xi_;
yi = yi_;
}
bool escaped(Paddle &paddle)
{
return y >= paddle.getY();
}
void move()
{
if((xi < 0 && x + xi < left)||(xi > 0 && x +xi >= right))
{
xi *= -1;
}
if(yi < 0 && y+yi-BALL_R*2 < top)
{
yi *= -1;
}
x+= xi;
y+= yi;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
int getXi()
{
return xi;
}
// check for collision with paddle
// calculate score and bounce
int collision(Tiles &tiles)
{
const int x = this->x+xi;
const int y = this->y+yi;
int hit;
if ( hit = hitTile( tiles, x, y ))
{
return SCORE[hit];
}
if ( hit = hitTile( tiles, x + BALL_R * 2, y + BALL_R * 2 ))
{
return SCORE[hit];
}
if ( hit = hitTile( tiles, x + BALL_R * 2, y ))
{
return SCORE[hit];
}
hit = hitTile( tiles, x, y + BALL_R * 2 );
return SCORE[hit];
}
// check for collision with paddle
// and bounce
bool collision(Paddle &paddle)
{
// only interested if ball is moving down
if ( yi < 0 ) return false;
const int ny = y+yi+BALL_R*2;
if ( ny < paddle.getY() ) return false;
// ball center
const int bc = x + BALL_R;
const int diff = bc - paddle.getX();
if ( diff < -PADDLE_W-PADDLE_TOLERANCE || diff > PADDLE_W + PADDLE_TOLERANCE)
{
return false;
}
else if (diff > 0)
{
const int hit = diff / ((PADDLE_W + PADDLE_TOLERANCE)/2);
xi = (float)hit * .5f + .5f;
yi = - 2 + xi;
}
else if (diff < 0)
{
const int hit = diff / ((PADDLE_W + PADDLE_TOLERANCE)/2);
xi = (float)hit * .5f - .5f;
yi = - 2 - xi;
}
else
{
yi *= -1;
}
return true;
}
private:
int hitTile(Tiles &tiles, int x, int y)
{
int ty =(y - TILES_TOP)/ TILE_H;
int tx =(x - TILES_LEFT)/ TILE_W;
const boolean hit = ty >= 0 && ty < ROWS && tx >= 0 && tx < COLS && tiles.exists(tx,ty);
if (!hit) return 0;
tiles.clearTile( tx, ty );
const int score = ROWS - ty;
ty*= TILE_H;
ty+= TILES_TOP;
tft.fillRectangle(TILES_LEFT + tx * TILE_W, ty, TILE_W - 2, TILE_H - 2, BACKGROUND_COLOR);
const int ty2= ty + TILE_H;
if ( (yi > 0 && y > ty) || (yi < 0 && y > ty2))
{
xi *=-1;
}
else
{
yi *=-1;
}
return score;
}
};
#endif