-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaddle.java
More file actions
69 lines (44 loc) · 1.35 KB
/
Paddle.java
File metadata and controls
69 lines (44 loc) · 1.35 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
package pong;
import java.awt.*;
public class Paddle {
private int height, x, y, speed;
private Color color;
static final int PADDLE_WIDTH = 15 ;
public Paddle(int x, int y, int height, int speed, Color color) {
this.x = x;
this.y = y;
this.height = height;
this.speed = speed;
this.color = color;
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(x, y, PADDLE_WIDTH, height);
}
public void moveTowards(int moveToY){
int centerY = y + height / 2 ;
if (Math.abs(centerY - moveToY) > speed) {
if (centerY > moveToY){
y -= speed;
}
if (centerY < moveToY) {
y += speed;
}
}
}
//checks if paddle is colliding with the ball
// if colliding return true vice versa
public boolean checkCollision(Ball b) {
int rightX = x + PADDLE_WIDTH;
int bottomY = y + height;
//check collison
if (b.getX() > (x - b.getSize()) && b.getX() < rightX) {
// so now we know its within the horizontal range
if (b.getY() > y && b.getY() < bottomY){
//we know its between top and bottom of paddle
return true;
}
}
return false;
}
}