-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.java
82 lines (72 loc) · 2.05 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.awt.*;
import java.awt.event.KeyEvent;
public class Paddle extends Rectangle {
int id;
int yVelocity;
int speed = 10;
Paddle(int x, int y, int PADDLE_WIDTH, int PADDLE_HEIGHT, int id) {
super(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
this.id = id;
}
public void keyPressed(KeyEvent e) {
switch (id) {
case 1 -> {
if (e.getKeyCode() == KeyEvent.VK_W) {
setYDirection(-speed);
move();
}
if (e.getKeyCode() == KeyEvent.VK_S) {
setYDirection(speed);
move();
}
}
case 2 -> {
if (e.getKeyCode() == KeyEvent.VK_UP) {
setYDirection(-speed);
move();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setYDirection(speed);
move();
}
}
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {
switch (id) {
case 1 -> {
if (e.getKeyCode() == KeyEvent.VK_W) {
setYDirection(0);
move();
}
if (e.getKeyCode() == KeyEvent.VK_S) {
setYDirection(0);
move();
}
}
case 2 -> {
if (e.getKeyCode() == KeyEvent.VK_UP) {
setYDirection(0);
move();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setYDirection(0);
move();
}
}
}
}
public void setYDirection(int yDirection) {
yVelocity = yDirection;
}
public void move() {
y = y + yVelocity;
}
public void draw(Graphics g) {
g.setColor(Color.white);
g.fillRect(x, y, width, height);
}
}