-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.java
59 lines (50 loc) · 1.4 KB
/
Paddle.java
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Paddle {
// instance variables
private int width, height;
private int xPos, yPos;
private Color color;
private int xVelocity;
private Breakout breakout;
// constructor
public Paddle(Breakout out) {
breakout = out; // this allows for communication/access to the Breakout object
width = 100;
height = 20;
xPos = 200;
yPos = 450;
color = Color.RED;
xVelocity = 0;
}
// methods
public void keyPressed(KeyEvent e) { // explain
if (e.getKeyCode() == 37) {
xVelocity = -10;
}
if (e.getKeyCode() == 39) {
xVelocity = 10;
}
}
public void keyReleased(KeyEvent e) { // explain
xVelocity = 0;
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(xPos, yPos, width, height);
}
public void move() {
if (xPos + xVelocity > 0 && xPos + xVelocity < breakout.getWidth() - width) {
xPos += xVelocity;
}
}
public Rectangle getBounds() {
return new Rectangle(xPos, yPos, width, height);
}
public boolean collision() {
return this.getBounds().intersects(breakout.getBall().getBounds());
// is the paddle being intersected with the ball
}
}