-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.pde
101 lines (91 loc) · 2.09 KB
/
Game.pde
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
class Game {
Ship ship;
Ufo[] ufos;
ArrayList<Shot> shots = new ArrayList();
int punkte = 0;
Game(int n) {
ufos = new Ufo[n];
for (int i=0; i<ufos.length; i++) {
ufos[i] = initUfo();
}
ship = new Ship(50, 125, 25);
ship.leben = 5;
}
void setShipPosition(float y) {
ship.y = y;
}
void step() {
for (int i=0; i<ufos.length; i++) {
ufos[i].move();
// Wenn Asteroid über den linken Rand geht...
if (ufos[i].x < -ufos[i].r) {
ufos[i] = initUfo();
}
}
// Liste rückwärts durchlaufen, da ggf. Elemente gelöscht werden
for (int i=shots.size()-1; i>=0; i--) {
Shot shot = shots.get(i);
shot.move();
// rausfliegende Shots entfernen!
if (shot.x > width+shot.r) {
shots.remove(i);
break;
}
for (int j=0; j<ufos.length; j++) {
if (shot.collides(ufos[j])) {
shots.remove(i);
ufos[j] = initUfo();
break;
}
}
}
for (int i=0; i<ufos.length; i++) {
if (ship.collides(ufos[i])) {
if (ufos[i] instanceof Asteroid) {
ship.leben--;
} else if (ufos[i] instanceof Bonus) {
punkte += ((Bonus)ufos[i]).points;
}
// Ufo wieder am Ende starten
ufos[i] = initUfo();
}
}
}
void draw() {
// Zeichne Spiel
background(0);
ship.draw();
for (Ufo ufo : ufos) {
ufo.draw();
}
for (Shot shot : shots) {
shot.draw();
}
fill(255);
textSize(20);
textAlign(LEFT, TOP);
text(punkte, 10, 10);
}
/* neuen Asteroid oder Bonus an rechten Rand setzen, y-Position zufällig
*/
Ufo initUfo() {
float r = ((int)random(1, 5))*10;
float x = width;
float y = random(height);
float vx = -random(0.5, 2);
if (random(1) < 0.1) {
return new Bonus(x, y, (int)r, vx);
} else {
return new Asteroid(x+r, y, r, vx);
}
}
void shoot() {
shots.add(new Shot(ship.x+ship.r, ship.y, 3));
}
boolean isGameOver() {
return ship.leben <= 0;
}
int getScore() {
return punkte;
}
}