-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerPaddle.java
59 lines (51 loc) · 1.51 KB
/
PlayerPaddle.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
/*
* This PlayerPaddle class defines any player paddles, and how they move.
* With minor changes, more PlayerPaddle objects could be created to create
* four-player pong, make the paddles move freely, or something else.
*
* @author Anthony Chen
* @version 1.0
* @since 2022-05-27
*/
import java.awt.*;
import java.awt.event.*;
public class PlayerPaddle extends Rectangle{
public int direction; // direction of paddle
public int speed = 8; // speed of paddle
public char keyOne; // two keys that control paddle
public char keyTwo;
// constructor creates a paddle at a given location with given controls
public PlayerPaddle(int x, int y, char kO, char kT){
super(x, y, 8, 64);
keyOne = kO;
keyTwo = kT;
}
// moves the paddle
public void move(){
y = y + direction * speed;
}
// controls the paddle
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == keyOne){
direction = -1;
move();
}
if(e.getKeyChar() == keyTwo){
direction = 1;
move();
}
}
// stops paddle moving when key released
public void keyReleased(KeyEvent e){
if(e.getKeyChar() == keyOne || e.getKeyChar() == keyTwo){
direction = 0;
move();
}
}
// called frequently from the GamePanel class
// draws the current location of the paddle to the screen
public void draw(Graphics g){
g.setColor(Color.white);
g.fillRect(x, y, 8, 64);
}
}