-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.pde
179 lines (161 loc) · 4.04 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
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
int DINOS = 1000;
float MIN_SPAWN_MILLIS = 500;
float MAX_SPAWN_MILLIS = 1500;
class Game {
ArrayList<Dino> dinos;
ArrayList<Enemy> enemies;
float speed;
Ground ground;
int alive;
int gen;
int score;
int last_gen_avg_score;
int last_gen_max_score;
float last_spawn_time;
float time_to_spawn;
Game() {
dinos = new ArrayList<Dino>();
for (int i = 0; i < DINOS; i++) {
dinos.add(new Dino());
}
enemies = new ArrayList<Enemy>();
speed = 15;
gen = 1;
score = 0;
last_gen_avg_score = 0;
last_gen_max_score = 0;
alive = DINOS;
ground = new Ground();
last_spawn_time = millis();
time_to_spawn = random(MIN_SPAWN_MILLIS, MAX_SPAWN_MILLIS);
}
void update() {
for (Dino dino: dinos) {
if (dino.alive) {
dino.update(find_next_enemy(dino), (int) speed);
}
}
Iterator<Enemy> iterator = enemies.iterator();
while (iterator.hasNext()) {
Enemy enemy = iterator.next();
enemy.update((int)speed);
if (enemy.is_offscreen()) {
iterator.remove();
}
}
if (millis() - last_spawn_time > time_to_spawn) {
spawn_enemy();
last_spawn_time = millis();
time_to_spawn = random(MIN_SPAWN_MILLIS, MAX_SPAWN_MILLIS);
}
ground.update((int)speed);
collision_check();
speed += 0.001;
}
void collision_check() {
alive = 0;
for (Dino dino: dinos) {
for (Enemy enemy : enemies) {
if (dino.alive && dino.is_colliding(enemy))
dino.kill(score);
}
if (dino.alive)
alive++;
}
if (alive == 0)
next_gen();
}
void next_gen() {
gen++;
score = 0;
speed = 15;
enemies.clear();
int total_score = 0;
for (Dino dino: dinos)
total_score += dino.score;
last_gen_avg_score = total_score / DINOS;
Collections.sort(dinos);
Collections.reverse(dinos);
last_gen_max_score = dinos.get(0).score;
ArrayList<Dino> new_dinos = new ArrayList<Dino>();
// Best 10% as is
for (int i = 0; i < DINOS * 0.1; i++) {
new_dinos.add(dinos.get(i));
new_dinos.get(i).reset();
}
// New 10%
for (int i = 0; i < DINOS * 0.1; i++) {
new_dinos.add(new Dino());
}
// 20% Mutate from best
for (int i = 0; i < DINOS * 0.2; i++) {
Dino dino = new Dino();
dino.genome = dinos.get(0).genome.mutate();
dino.init_brain();
new_dinos.add(dino);
}
// 30% father from best 5%
for (int i = 0; i < DINOS * 0.3; i++) {
Dino father = dinos.get((int)random(DINOS * 0.05));
Dino son = new Dino();
son.genome = father.genome.mutate();
son.init_brain();
new_dinos.add(son);
}
// 30% father & mother from best 5%
for (int i = 0; i < DINOS * 0.2; i++) {
Dino father = dinos.get((int)random(DINOS * 0.05));
Dino mother = dinos.get((int)random(DINOS * 0.05));
Dino son = new Dino();
son.genome = father.genome.crossover(mother.genome);
son.init_brain();
new_dinos.add(son);
}
dinos = new_dinos;
}
void spawn_enemy() {
if (random(1) < 0.5) {
enemies.add(new Cactus());
} else {
enemies.add(new Bird());
}
}
void display() {
ground.display();
for (Enemy enemy : enemies) {
enemy.display();
}
for (Dino dino: dinos)
dino.display();
display_info();
}
void display_info() {
fill(0);
textSize(30);
text(score, 1200, 80);
text("Gen: " + gen, 80, 80);
text("Last Gen Average Score: " + last_gen_avg_score, 80, 120);
text("Last Gen Max Score: " + last_gen_max_score, 80, 160);
text("Alive: " + alive, 80, 200);
}
void tenth_second() {
for (Dino dino: dinos)
if (dino.alive) {
dino.toggle_sprite();
}
score++;
}
void quater_second() {
for (Enemy enemy : enemies) {
enemy.toggle_sprite();
}
}
Enemy find_next_enemy(Dino dino) {
for (Enemy enemy : enemies) {
if (enemy.x > dino.x) {
return enemy;
}
}
return null;
}
}