-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.js
executable file
·213 lines (166 loc) · 5.37 KB
/
Game.js
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
let canvas = document.querySelector("canvas");
canvas.height = 660;
canvas.width = 550;
let c = canvas.getContext("2d");
// global game variables
//--------------------------------------------------------------------------------------------------------------------------------
let frameCount = 0;
let score = 0;
let moveGround = 0;
let closestEnemy = [];
let stopId = undefined;
let timeLapse = 1;
let Score = 0;
let showBest= false;
let bird = new Image();
let pipeTop = new Image();
let pipeBottom = new Image();
let background = new Image();
let ground = new Image();
bird.src = "images/Bird.png";
pipeTop.src = "images/pipeTop.png";
pipeBottom.src = "images/pipeBottom.png";
background.src = "images/background.png";
ground.src = "images/ground.png";
bird.addEventListener("load", () => c.drawImage(bird, 0, 0));
pipeTop.addEventListener("load", () => c.drawImage(pipeTop, 0, 0));
pipeBottom.addEventListener("load", () => c.drawImage(pipeBottom, 0, 0,));
background.addEventListener("load", () => c.drawImage(background, 0, 0));
ground.addEventListener("load", () => c.drawImage(ground, 0, 0));
//--------------------------------------------------------------------------------------------------------------------------------
//global neat variables
//--------------------------------------------------------------------------------------------------------------------------------
let noOfInputNodes = 2;
let noOfOutputNodes = 1;
let noOfPlayers = 1000;
let generation = 1;
let networkWindow = {
x : canvas.width - 200,
y : canvas.height - 200,
width : 200,
height : 200
};
//--------------------------------------------------------------------------------------------------------------------------------
for(let i = 0; i < noOfPlayers; i++){
Neat.players.push(new Player());
}
let channel = [0,0,0,0,0];
function drawBackground(){
c.drawImage(background, 0, 0, canvas.width, canvas.height);
}
function drawGround(){
if(moveGround + canvas.width <= 0){
moveGround = 0;
}
c.drawImage(ground, moveGround, canvas.height - 30, 2 * canvas.width, 30);
c.drawImage(ground, moveGround + canvas.width, canvas.height - 30, 2 * canvas.width, 30);
}
function restart(){
score = 0;
Score = 0;
frameCount = 0;
moveGround = 0;
stopId = undefined;
// timeLapse = 1;
PipePair.pipeList = [new PipePair()];
}
addEventListener("keydown", (event) => {
if(event.keyCode == 221){
timeLapse++;
} else if(event.keyCode == 219){
timeLapse--;
} else if(event.keyCode == 32){
humanPlayer.velocity = -7;
} else if(event.keyCode == 83){
showBest = true;
}
});
function findClosestPipe(){
if(PipePair.pipeList.length == 1){
return PipePair.pipeList[0];
} else if(PipePair.pipeList.length == 2){
let distance = PipePair.pipeList[0].x + PipePair.width - 0.2 * canvas.width;
if(distance >= 0){
return PipePair.pipeList[0];
} else {
return PipePair.pipeList[1];
}
}
console.log("Unexpected Error");
return new PipePair();
}
PipePair.pipeList.push(new PipePair())
// display everything
function animate(){
drawBackground();
for(let pipe of PipePair.pipeList){
pipe.show();
// pipe.update();
}
// for debugging purpose
// c.beginPath();
// c.moveTo(Neat.players[0].x, Neat.players[0].y);
// c.lineTo(closestPipe.x, closestPipe.center);
// c.lineWidth = 4;
// c.stroke();
// c.closePath();
drawGround();
//display score
c.font = "bold " + 20 + "pt Ubuntu";
c.fillStyle = "#FFFFFF";
c.fillText("" + Score, canvas.width/2 - c.measureText("" + Score).width/2, 100);
c.fillText("Generation : " + generation, 10, 50);
if(showBest){
if(!Neat.players[0].dead){
Neat.players[0].show();
} else {
for(let i = 0 ; i < noOfPlayers; i++){
if(!Neat.players[i].dead){
Neat.players[i].show();
break;
}
}
}
} else{
for(let i = 0 ; i < noOfPlayers; i++){
if(!Neat.players[i].dead){
Neat.players[i].show();
}
}
}
Neat.players[0].brain.drawGenome();
stopId = requestAnimationFrame(animate);
//----------------------------------------------------------------------------------------------------------------
for(let t = 0 ; t < timeLapse; t++){
score = Math.floor(frameCount / 10);
for(let pipe of PipePair.pipeList){
pipe.update();
}
let closestPipe = findClosestPipe();
for(let i = 0; i < noOfPlayers; i++){
if(!Neat.players[i].dead){
Neat.players[i].observe(closestPipe);
Neat.players[i].think();
Neat.players[i].update();
}
}
if(allDead()){
cancelAnimationFrame(stopId);
restart();
Neat.evolve();
animate();
break;
}
frameCount++;
moveGround--;
}
}
function allDead(){
for(let i = 0 ; i < Neat.players.length; i++){
if(!Neat.players[i].dead){
return false;
}
}
return true;
}
animate();