forked from zacoppotamus/Breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.java
97 lines (81 loc) · 1.6 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
/**
The paddle class contains the coordinates
of the paddle as well as its horizontal speed.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
class Paddle
{
private int xLeft;
private int pWidth;
private int pHeight;
private int dx;
public Paddle(int x, int y)
{
xLeft = x;
pHeight = y;
pWidth = 60;
}
public int getXLeft()
{
return xLeft;
}
public int getPHeight()
{
return pHeight;
}
public int getPWidth()
{
return pWidth;
}
/**
* Move left or right until
* a wall is reached.
*/
public void move()
{
Commons commons = new Commons();
xLeft += dx;
if (xLeft <= 2)
xLeft = 2;
if (xLeft >= commons.getWidth()-pWidth)
xLeft = commons.getWidth()-pWidth-2;
}
public Rectangle paddleAsRect()
{
Rectangle paddle
= new Rectangle(xLeft, pHeight, pWidth, 3);
return paddle;
}
/**
* Event listeners for controlling
* the paddle.
* @param e
*/
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
dx = -2;
}
if (key == KeyEvent.VK_RIGHT)
{
dx = 2;
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
dx = 0;
}
if (key == KeyEvent.VK_RIGHT)
{
dx = 0;
}
}
}