-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroid.java
79 lines (69 loc) · 1.32 KB
/
Asteroid.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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class Asteroid {
boolean alive;
double x;
double y;
double xSpeed;
double ySpeed;
int radius;
BreakLevel breakLevel;
public enum BreakLevel {
LEVELONE, LEVELTWO, LEVELTHREE
}
public Asteroid(double x, double y, double xSpeed, double ySpeed, BreakLevel breakLevel) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.breakLevel = breakLevel;
switch (this.breakLevel) {
case LEVELONE:
radius = 20;
break;
case LEVELTWO:
radius = 40;
break;
case LEVELTHREE:
radius = 80;
break;
}
}
public void moveAsteroid() {
x = (int) (x + xSpeed);
y = (int) (y + ySpeed);
if (x > 1000) {
x = 0;
}
if (y > 700) {
y = 0;
}
if (x < 0) {
x = 1000;
}
if (y < 0) {
y = 700;
}
}
public void drawAsteroid(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
// wrap
int x = (int) this.x;
int y = (int) this.y;
if (x + radius > 1000) {
g.drawOval(x - 1000, y, radius, radius);
}
if (y + radius > 700) {
g.drawOval(x, y - 700, radius, radius);
}
if (x + radius < 0) {
g.drawOval(x + 1000, y, radius, radius);
}
if (y + radius < 0) {
g.drawOval(x, y + 700, radius, radius);
}
g2.drawOval(x, y, radius, radius);
}
}