-
Notifications
You must be signed in to change notification settings - Fork 0
/
DinoGame.pde
350 lines (305 loc) · 10.5 KB
/
DinoGame.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//Globals
int nextConnectionNo = 1000;
Population pop;
int frameSpeed = 60;
boolean showBestEachGen = false;
int upToGen = 0;
Player genPlayerTemp;
boolean showNothing = false;
//images
PImage dinoRun1;
PImage dinoRun2;
PImage dinoJump;
PImage dinoDuck;
PImage dinoDuck1;
PImage smallCactus;
PImage manySmallCactus;
PImage bigCactus;
PImage bird;
PImage bird1;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Bird> birds = new ArrayList<Bird>();
ArrayList<Ground> grounds = new ArrayList<Ground>();
int obstacleTimer = 0;
int minimumTimeBetweenObstacles = 60;
int randomAddition = 0;
int groundCounter = 0;
float speed = 10;
int groundHeight = 250;
int playerXpos = 150;
ArrayList<Integer> obstacleHistory = new ArrayList<Integer>();
ArrayList<Integer> randomAdditionHistory = new ArrayList<Integer>();
int highScore = 0;
int screenshotIndex = 1;
//--------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
frameRate(60);
fullScreen();
dinoRun1 = loadImage("dinorun0000.png");
dinoRun2 = loadImage("dinorun0001.png");
dinoJump = loadImage("dinoJump0000.png");
dinoDuck = loadImage("dinoduck0000.png");
dinoDuck1 = loadImage("dinoduck0001.png");
smallCactus = loadImage("cactusSmall0000.png");
bigCactus = loadImage("cactusBig0000.png");
manySmallCactus = loadImage("cactusSmallMany0000.png");
bird = loadImage("berd.png");
bird1 = loadImage("berd2.png");
pop = new Population(500); //<<number of dinosaurs in each generation
}
String sketchPath = sketchPath("E:/ML/Processing/Google-Chrome-Dino-Game-AI-master/DinoGame/Progress"); // Get the path to your sketch folder
String screenshotFolder = sketchPath + "" + File.separator; // Construct the path to the "progress" folder
void saveScreenshot(String path, String filename){
String filepath = path + File.separator + filename;
saveFrame(filepath);
}
int highss = 0;
void checkss(int score){
if (score>highScore){
highScore = score;
if (score>highss+100){
highss = score;
String screenshotFilename = "screenshot_" + screenshotIndex + ".png";
saveScreenshot(screenshotFolder, screenshotFilename);
screenshotIndex++;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
void draw() {
drawToScreen();
if (showBestEachGen) {//show the best of each gen
if (!genPlayerTemp.dead) {//if current gen player is not dead then update it
genPlayerTemp.updateLocalObstacles();
genPlayerTemp.look();
genPlayerTemp.think();
genPlayerTemp.update();
genPlayerTemp.show();
} else {//if dead move on to the next generation
upToGen ++;
if (upToGen >= pop.genPlayers.size()) {//if at the end then return to the start and stop doing it
upToGen= 0;
showBestEachGen = false;
} else {//if not at the end then get the next generation
genPlayerTemp = pop.genPlayers.get(upToGen).cloneForReplay();
}
}
} else {//if just evolving normally
if (!pop.done()) {//if any players are alive then update them
updateObstacles();
pop.updateAlive();
} else {//all dead
//genetic algorithm
pop.naturalSelection();
resetObstacles();
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//draws the display screen
void drawToScreen() {
if (!showNothing) {
background(250);
stroke(0);
strokeWeight(2);
line(0, height - groundHeight - 30, width, height - groundHeight - 30);
drawBrain();
writeInfo();
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void drawBrain() { //show the brain of whatever genome is currently showing
int startX = 600;
int startY = 10;
int w = 600;
int h = 400;
if (showBestEachGen) {
genPlayerTemp.brain.drawGenome(startX, startY, w, h);
} else {
for (int i = 0; i< pop.pop.size(); i++) {
if (!pop.pop.get(i).dead) {
pop.pop.get(i).brain.drawGenome(startX, startY, w, h);
break;
}
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//writes info about the current player
void writeInfo() {
fill(0);
textAlign(LEFT);
textSize(40);
if (showBestEachGen) { //if showing the best for each gen then write the applicable info
text("Score: " + genPlayerTemp.score, 30, height - 30);
//text(, width/2-180, height-30);
textAlign(RIGHT);
text("Gen: " + (genPlayerTemp.gen +1), width -40, height-30);
textSize(20);
int x = 580;
text("Distace to next obstacle", x, 18+44.44444);
text("Height of obstacle", x, 18+2*44.44444);
text("Width of obstacle", x, 18+3*44.44444);
text("Bird height", x, 18+4*44.44444);
text("Speed", x, 18+5*44.44444);
text("Players Y position", x, 18+6*44.44444);
text("Gap between obstacles", x, 18+7*44.44444);
text("Bias", x, 18+8*44.44444);
textAlign(LEFT);
text("Small Jump", 1220, 118);
text("Big Jump", 1220, 218);
text("Duck", 1220, 318);
} else { //evolving normally
text("Score: " + floor(pop.populationLife/3.0), 30, height - 30);
fill(0, 255, 0);
text("HighScore: " + highScore, 300, height - 30);
fill(0);
int score = floor(pop.populationLife/3.0);
checkss(score);
//text(, width/2-180, height-30);
textAlign(RIGHT);
text("Gen: " + (pop.gen +1), width -40, height-30);
textSize(20);
int x = 580;
text("Distace to next obstacle", x, 18+44.44444);
text("Height of obstacle", x, 18+2*44.44444);
text("Width of obstacle", x, 18+3*44.44444);
text("Bird height", x, 18+4*44.44444);
text("Speed", x, 18+5*44.44444);
text("Players Y position", x, 18+6*44.44444);
text("Gap between obstacles", x, 18+7*44.44444);
text("Bias", x, 18+8*44.44444);
textAlign(LEFT);
text("Small Jump", 1220, 118);
text("Big Jump", 1220, 218);
text("Duck", 1220, 318);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
void keyPressed() {
switch(key) {
case '+'://speed up frame rate
frameSpeed += 10;
frameRate(frameSpeed);
println(frameSpeed);
break;
case '-'://slow down frame rate
if (frameSpeed > 10) {
frameSpeed -= 10;
frameRate(frameSpeed);
println(frameSpeed);
}
break;
case 'g'://show generations
showBestEachGen = !showBestEachGen;
upToGen = 0;
genPlayerTemp = pop.genPlayers.get(upToGen).cloneForReplay();
break;
case 'n'://show absolutely nothing in order to speed up computation
showNothing = !showNothing;
break;
case CODED://any of the arrow keys
switch(keyCode) {
case RIGHT://right is used to move through the generations
if (showBestEachGen) {//if showing the best player each generation then move on to the next generation
upToGen++;
if (upToGen >= pop.genPlayers.size()) {//if reached the current generation then exit out of the showing generations mode
showBestEachGen = false;
} else {
genPlayerTemp = pop.genPlayers.get(upToGen).cloneForReplay();
}
break;
}
break;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//called every frame
void updateObstacles() {
obstacleTimer ++;
speed += 0.002;
if (obstacleTimer > minimumTimeBetweenObstacles + randomAddition) { //if the obstacle timer is high enough then add a new obstacle
addObstacle();
}
groundCounter ++;
if (groundCounter> 10) { //every 10 frames add a ground bit
groundCounter =0;
grounds.add(new Ground());
}
moveObstacles();//move everything
if (!showNothing) {//show everything
showObstacles();
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//moves obstacles to the left based on the speed of the game
void moveObstacles() {
println(speed);
for (int i = 0; i< obstacles.size(); i++) {
obstacles.get(i).move(speed);
if (obstacles.get(i).posX < -playerXpos) {
obstacles.remove(i);
i--;
}
}
for (int i = 0; i< birds.size(); i++) {
birds.get(i).move(speed);
if (birds.get(i).posX < -playerXpos) {
birds.remove(i);
i--;
}
}
for (int i = 0; i < grounds.size(); i++) {
grounds.get(i).move(speed);
if (grounds.get(i).posX < -playerXpos) {
grounds.remove(i);
i--;
}
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
//every so often add an obstacle
void addObstacle() {
int lifespan = pop.populationLife;
int tempInt;
if (lifespan > 1000 && random(1) < 0.15) { // 15% of the time add a bird
tempInt = floor(random(3));
Bird temp = new Bird(tempInt);//floor(random(3)));
birds.add(temp);
} else {//otherwise add a cactus
tempInt = floor(random(3));
Obstacle temp = new Obstacle(tempInt);//floor(random(3)));
obstacles.add(temp);
tempInt+=3;
}
obstacleHistory.add(tempInt);
randomAddition = floor(random(50));
randomAdditionHistory.add(randomAddition);
obstacleTimer = 0;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//what do you think this does?
void showObstacles() {
for (int i = 0; i< grounds.size(); i++) {
grounds.get(i).show();
}
for (int i = 0; i< obstacles.size(); i++) {
obstacles.get(i).show();
}
for (int i = 0; i< birds.size(); i++) {
birds.get(i).show();
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
//resets all the obstacles after every dino has died
void resetObstacles() {
randomAdditionHistory = new ArrayList<Integer>();
obstacleHistory = new ArrayList<Integer>();
obstacles = new ArrayList<Obstacle>();
birds = new ArrayList<Bird>();
obstacleTimer = 0;
randomAddition = 0;
groundCounter = 0;
speed = 10;
}