-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
72 lines (55 loc) · 1.13 KB
/
Triangle.java
File metadata and controls
72 lines (55 loc) · 1.13 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
import java.awt.Rectangle;
import java.awt.*;
import java.awt.event.*;
public class Triangle {
int x[];
int y[];
int base, height;
int dx;
double secondsToWait, secondsWaited;
public Triangle(int x, int y, int b, int h) {
this.x = new int[3];
this.y = new int[3];
this.x[0] = x;
this.y[0] = y;
this.base = b;
this.height = h;
this.x[1] = (x + (base / 2) );
this.x[2] = (x + base);
this.y[1] = (y - height);
this.y[2] = this.y[0];
this.dx = 3;
timeToWait();
secondsWaited = 0.0;
}
public void draw(Graphics g) {
if (secondsWaited < secondsToWait) {
this.sitBackAndWait();
} else {
moveEnemy();
}
g.setColor(Color.RED);
g.fillPolygon(new Polygon(x, y, 3));
}
private void sitBackAndWait() {
secondsWaited+=0.010;
}
private void timeToWait() {
while (secondsToWait < 0.5) {
System.out.println("to wait: " + secondsToWait);
secondsToWait = Math.random() * 3.0 + 1;
}
}
public void moveEnemy() {
if (this.x[0] <= 0) {
x[0] = 640;
x[1] = x[0] + (base / 2);
x[2] = x[0] + base;
timeToWait();
secondsWaited = 0.0;
}
x[0]-=dx;
x[1]-=dx;
x[2]-=dx;
}
}