-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddle1.java
89 lines (67 loc) · 1.87 KB
/
Paddle1.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
import java.awt.event.KeyEvent; //the KeyEvent class generates events: KEY_PRESSED, KEY_RELEASED, KEY_TYPED
import java.awt.Graphics; //the Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components
import java.awt.*;
public class Paddle1
{
// Global Variables
private final int init_paddle_x = 10;
private final int init_paddle_y = 250;
private int dx, dy;
private int x, y = 250;
private boolean scoreGuard = true;
public Paddle1 ()
{
x = 10;
} // Constructor
// Draws the paddle at x and y location.
public void draw (Graphics g)
{
g.setColor (Color.white);
g.fillRect (x, y, 25, 120);
}
// Enables the paddle to move up or down
public void move ()
{
y = y + dy;
if (y < 15)
y = 15;
if (y > 535)
y = 535;
}
// Returns the x value for the paddle
public int getX ()
{
return x;
}
// Returns the y value for the paddle
public int getY ()
{
return y;
}
// When W or S is pressed, the paddle moves up or down.
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_W)
{
dy = -1; // When pressed, moves up by 1
}
if (key == KeyEvent.VK_S)
{
dy = 1; // When pressed, moves down by 1
}
}
// When W or S is released, the paddle will stop moving.
public void keyReleased (KeyEvent e)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_W)
{
dy = 0; // When released, sets the dy variable to 0
}
if (key == KeyEvent.VK_S)
{
dy = 0; // When released, sets the dy variable to 0
}
}
}