-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaddle.java
More file actions
101 lines (96 loc) · 2.87 KB
/
Paddle.java
File metadata and controls
101 lines (96 loc) · 2.87 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
90
91
92
93
94
95
96
97
98
99
100
101
import greenfoot.*;
/**
* A paddle is an object that goes back and forth. Though it would be nice if balls would bounce of it.
*
* @author Patrik Valentiny (PM), Jakov Klaric, Abdulkader Alomar, Ignas Klimas
* @version 1
*/
public class Paddle extends Actor
{
private int width;
private int height;
private int dx;
private int lastDx;
/**
* Constructs a new paddle with the given dimensions.
*/
public Paddle(int width, int height)
{
this.width = width;
this.height = height;
dx = 2;
createImage();
}
/**
* Act - do whatever the Paddle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
tryChangeDirection();
changeDirection();
setLocation(getX() + dx, getY());
//((PingWorld)getWorld()).showText(""+dx,200,300);
}
/**
* Will rotate the paddle 180 degrees if the paddle is at worlds edge.
*/
private void tryChangeDirection()
{
//Check to see if we are touching the outer boundaries of the world:
// IF we are touching the right boundary OR we are touching the left boundary:
if(getX() + width/2 >= (getWorld().getWidth()) - 10 || getX() - width/2 <= 0 + 10)
{
//Change our 'x' direction to the inverted direction:
dx = dx * -1;
}
}
/**
* Creates and sets an image for the paddle, the image will have the same dimensions as the paddles width and height.
*/
private void createImage()
{
GreenfootImage image = new GreenfootImage(width, height);
image.setColor(Color.WHITE);
image.fill();
setImage(image);
}
private void changeDirection(){
String key = Greenfoot.getKey();
if (key != null){
if (key.equals("left")){
if (dx>0){
dx *= -1;
} else if (dx==0){
dx = -1;
}
} else if (key.equals("right")){
if (dx < 0){
dx *= -1;
} else if (dx ==0){
dx = 1;
}
} else if (key.equals("up")){
if (dx>0 && dx <5){
dx += 1;
} else if (dx<0 && dx > -5){
dx -= 1;
} else if (dx == 0){
dx = lastDx;
}
} else if (key.equals("down")){
if (dx>0){
if (dx != 0){
lastDx = dx;
dx -= 1;
}
} else if (dx < 0){
if (dx != 0){
lastDx = dx;
dx += 1;
}
}
}
}
}
}