-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddle.java
68 lines (64 loc) · 1.25 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
import java.awt.*;
import java.awt.event.*;
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);
}
if(e.getKeyCode()==KeyEvent.VK_S) {
setYDirection(speed);
}
break;
case 2:
if(e.getKeyCode()==KeyEvent.VK_UP) {
setYDirection(-speed);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN) {
setYDirection(speed);
}
break;
}
}
public void keyReleased(KeyEvent e) {
switch(id) {
case 1:
if(e.getKeyCode()==KeyEvent.VK_W) {
setYDirection(0);
}
if(e.getKeyCode()==KeyEvent.VK_S) {
setYDirection(0);
}
break;
case 2:
if(e.getKeyCode()==KeyEvent.VK_UP) {
setYDirection(0);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN) {
setYDirection(0);
}
break;
}
}
public void setYDirection(int yDirection) {
yVelocity = yDirection;
}
public void move() {
y= y + yVelocity;
}
public void draw(Graphics g) {
if(id==1)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.fillRect(x, y, width, height);
}
}