-
Notifications
You must be signed in to change notification settings - Fork 1
/
Obstacle.java
61 lines (55 loc) · 1.39 KB
/
Obstacle.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
/*
* Name: Obstacles
* Beschreibung: Hindernisse
*
*
*/
import java.awt.Color;
public class Obstacle extends GameObject {
/**
* obstacle constructor (basically the same as the players constructor).
* this uses the GameObjects super constructor as well, using the same parameters
* @param x
* x coordinate
* @param y
* y coordinate
* @param w
* width
* @param h
* height
* @param color
* color
* */
public Obstacle(double x, double y, double w, double h, Color color) {
super(x, y, w, h, color);
}
public Obstacle(double x, double y, double w, double h) {
this(x, y, w, h, new Color(100, 200, 100));
}
/**
* update method of the obstacle;
* whenever this is called by the GameModel, the obstacles x-position is slightly decreased
* and it therefore scrolls to the left.
* when it disappears out of the view (or passes the left boundary of the view) it
* replaces itself at either its old starting position or a new position, altering the y-position.
* this is determined by Math.random() < 0.5.
* so we can get two kinds of obstacles, these from above and from below
* */
@Override
public void update(){
this.setX(getX()-0.15);
if (getX() < -getWidth()) {
if (Math.random() < 0.5) {
setX(60.0);
setY(5.0);
setWidth(2.0);
setHeight(9.0);
} else {
setX(60.0);
setY(0);
setWidth(2.0);
setHeight(4.0);
}
}
}
}