Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 1b402be603
Fetching contributors…

Cannot retrieve contributors at this time

82 lines (69 sloc) 1.855 kB
class Ball {
final Board board;
CanvasRenderingContext2D context;
Paddle paddle;
Point point;
static final int RAD = 10;
static final String COLOR = 'grey';
static final int LINE_WIDTH = 3;
Point delta = new Point(-1, -2);
bool _cliped = true;
//Конструктор
Ball(this.board, this.paddle) {
context = board.context;
//начальная точка
point = new Point(
paddle.centerX,
paddle.startY - Ball.RAD - Paddle.LINE_WIDTH
);
}
void draw() {
context.beginPath();
context.fillStyle = COLOR;
context.strokeStyle = COLOR;
context.lineWidth = LINE_WIDTH;
context.arc(point.x, point.y, RAD, 0, PI*2, true);
context.closePath();
context.stroke();
}
void followPaddle() {
point.x = paddle.centerX;
point.y = paddle.startY - RAD - Paddle.LINE_WIDTH;
}
void bounce() {
//удар о боковые стенки
if (point.x + delta.x > board.width - RAD || point.x + delta.x < RAD) {
delta.x = -delta.x;
}
//удар о верхнюю стенку
if (point.y + delta.y < RAD) {
delta.y = -delta.y;
}
//столкновение с полом или битой
else if (point.y + delta.y > paddle.startY - RAD) {
if (point.x > paddle.startX && point.x < paddle.endX) {
delta.x = 8 * ((point.x-(paddle.centerX))/Paddle.WIDTH);
delta.y = max(4-(delta.x).abs(), 0.8);
delta.y = min(3, delta.y);
delta.y = -delta.y;
} else {
clip();
}
}
point.x += delta.x;
point.y += delta.y;
}
//методы прикрепления шара к бите
bool isCliped() {
return _cliped;
}
void clip() {
_cliped = true;
}
void unClip() {
_cliped = false;
}
void toggleClip() {
_cliped = !_cliped;
}
}
Jump to Line
Something went wrong with that request. Please try again.