This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaddle.java
More file actions
executable file
·89 lines (73 loc) · 1.67 KB
/
Paddle.java
File metadata and controls
executable file
·89 lines (73 loc) · 1.67 KB
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
import java.awt.Color;
import java.awt.event.KeyEvent;
import acm.graphics.*;
public class Paddle extends GRect implements Commons{
int pid;
double dy = 0;
public Paddle(double width, double height, int player) {
super(width, height);
pid = player;
resetState();
if(pid==1){
setColor(Color.BLUE);
}else{
setColor(Color.RED);
}
}
public void move(int y){
setLocation(getX(), y);
if(getY()+getHeight()>Screen_Height){
setLocation(getX(), Screen_Height-getHeight());
}else if(getY()<0){
this.setLocation(getX(), getHeight()/2);
}
}
public void keyMove(){
if(getY()+getHeight()>Commons.Screen_Height){
if(dy<0){
move(0, dy);
}
}else if(getY()<0){
if(dy>0){
move(0, dy);
}
}else{
move(0, dy);
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(pid == 1){
if(key == KeyEvent.VK_W){
dy = -Commons.Paddle_Velocity;
}else if(key == KeyEvent.VK_S){
dy = Commons.Paddle_Velocity;
}
}else{
if(key == KeyEvent.VK_UP){
dy = -Commons.Paddle_Velocity;
}else if(key == KeyEvent.VK_DOWN){
dy = Commons.Paddle_Velocity;
}
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(pid == 1){
if(key == KeyEvent.VK_W || key == KeyEvent.VK_S){
dy = 0;
}
}else{
if(key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN){
dy = 0;
}
}
}
public void resetState(){
if(pid==1){
this.setLocation(10, Screen_Height/2-(Paddle_Height/2));
}else{
this.setLocation(Screen_Width-20, Screen_Height/2-(Paddle_Height/2));
}
}
}