-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.java
116 lines (111 loc) · 2.31 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Paddle extends Rectangle {
int id;
int yVelocity;
int xVelocity;
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_Z ) {
setYDirection(-speed);
move();
}
if(e.getKeyCode() == KeyEvent.VK_S ) {
setYDirection(speed);
move();
}
if(e.getKeyCode() == KeyEvent.VK_Q ) {
setXDirection(-speed);
move();
}
if(e.getKeyCode() == KeyEvent.VK_D ) {
setXDirection(speed);
move();
}
break;
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_LEFT ) {
setXDirection(-speed);
move();
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT ) {
setXDirection(speed);
move();
}
break;
}
}
public void keyReleased(KeyEvent e) {
switch (id) {
case 1 :
if(e.getKeyCode() == KeyEvent.VK_Z ) {
setYDirection(0);
move();
}
if(e.getKeyCode() == KeyEvent.VK_S ) {
setYDirection(0);
move();
}
if(e.getKeyCode() == KeyEvent.VK_Q ) {
setXDirection(0);
move();
}
if(e.getKeyCode() == KeyEvent.VK_D ) {
setXDirection(0);
move();
}
break;
case 2 :
if(e.getKeyCode() == KeyEvent.VK_UP ) {
setYDirection(0);
move();
}
if(e.getKeyCode() == KeyEvent.VK_DOWN ) {
setYDirection(0);
move();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT ) {
setXDirection(0);
move();
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT ) {
setXDirection(0);
move();
}
break;
}
}
public void setYDirection(int yDirection) {
yVelocity = yDirection;
}
public void setXDirection(int xDirection) {
xVelocity = xDirection;
}
public void move() {
y = y + yVelocity;
x = x + xVelocity;
}
public void draw(Graphics g) {
if(id ==1)
g.setColor(Color.BLUE);
else
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}