-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartRockets.pde
140 lines (110 loc) · 2.86 KB
/
SmartRockets.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************+
* Author: Alexander Kraus (@akraus53)
* in large parts inspired by Daniel Shiffman (@shiffman)
* (c) 2017-18
*/
Population population;
int lifespan = 500;
int count = 0; //current frame of Lap
float maxforce = 2; // set screw for acceleration
int popSize = 250; // number of rockets launched every Lap
int randomOnes = 10; // number of new, random rockets launched
boolean allCrashed;
Rocket bestRocket;
boolean drawingLine = false;
// Game statistics
int totalHits;
int hitsThisRound;
int allTimeBestHits;
boolean timeLogged = false;
int bestTime = lifespan;
int allTimeBestTime = lifespan;
int lap = 0;
// target and barrier vars
PVector target;
ArrayList<PVector> barriers = new ArrayList<PVector>();
void setup() {
fullScreen();
//size(1600, 900);
population = new Population();
target = new PVector(width/2, 50);
}
void draw() {
background(0);
population.run();
count++;
// If every rocket has crashed, there's no need to wait for all of the loops to finish
if (population.allCrashed()) {
count = lifespan;
}
if (count == lifespan) {
population.evaluate();
population.selection();
updateHighscores();
resetStatistics();
}
// draw the barriers and the target
noStroke();
fill(255);
ellipse(target.x, target.y, 16, 16);
fill(0, 255, 255);
for (PVector p : barriers) {
ellipse(p.x, p.y, 20, 20);
}
drawBestPath();
showStats();
}
void updateHighscores() {
if (hitsThisRound > allTimeBestHits) {
allTimeBestHits = hitsThisRound;
}
if (bestTime < allTimeBestTime) {
allTimeBestTime = bestTime;
}
}
void resetStatistics() {
lap++;
hitsThisRound = 0;
count = 0;
timeLogged = false;
bestTime = lifespan;
}
void showStats() {
fill(255);
textSize(30);
textAlign(TOP);
if(hitsThisRound < 0) hitsThisRound = 0;
text(("Hits this round:" + hitsThisRound), 10, 30);
text(("Highscore:" + allTimeBestHits), 10, 60);
text(("Best time in this round:" + bestTime), 10, 90);
text(("Best time ever:" + allTimeBestTime), 10, 120);
text(("Lap #" + (lap+1)), 10, 150);
text(count, 10, 180);
}
void drawBestPath() {
// Draw a dotted line where the rocket with the fastest path was
strokeWeight(5);
stroke(255);
beginShape(LINES);
if (timeLogged || drawingLine) {
drawingLine = true;
// rocket has to be reset after every drawing cycle
bestRocket.pos = new PVector(width/2, height -30);
bestRocket.vel.mult(0);
bestRocket.acc.mult(0);
bestRocket.completed = false;
bestRocket.crashed = false;
for (int i = 0; i<lifespan; i++) {
if (!bestRocket.crashed && !bestRocket.completed) {
bestRocket.update(i);
}
vertex(bestRocket.pos.x, bestRocket.pos.y);
}
hitsThisRound--;
}
}
void mouseDragged() {
barriers.add(new PVector(mouseX, mouseY));
allTimeBestTime = lifespan;
drawingLine = false;
}