-
Notifications
You must be signed in to change notification settings - Fork 2
/
Paddle.java
48 lines (42 loc) · 1001 Bytes
/
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* What you move to hit the ball.
*
* @author Cody A. Ray
* @version December 8, 2012
*/
public class Paddle extends Actor
{
private static final int LENGTH = 100;
private static final int HEIGHT = 5;
private static final int MOVE_DISTANCE = 5;
/**
* Create a new paddle.
*/
public Paddle()
{
GreenfootImage image = getImage();
image.scale(LENGTH, HEIGHT);
image.setColor(Color.RED);
image.fill();
}
/**
* Move left and right when the keys are pressed
*/
public void act()
{
checkKeys();
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX() - MOVE_DISTANCE, getY());
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX() + MOVE_DISTANCE, getY());
}
}
}