-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
195 lines (171 loc) · 5.34 KB
/
sketch.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
const playerWidth = 32;
const playerHeight = 32;
var population = 250;
var players = [];
var obstaclePositions = [544, 576, 608, 640, 672, 704, 736];
var obstacles = [];
const maxNumberOfObstacles = 8;
var lives = 1;
var displayLives = [];
var trees = [];
const maxNumberOfTrees = 25;
var alivePlayers = 0;
var bestScore = 0;
var inteligence = true;
var debugLives = 0;
var uiFont;
var uiFontSize = 32;
var simpleGraphics = false;
var counter = 0;
var runSpeed = 1;
var leftPressed = false;
var rightPressed = false;
const filename = "bestplayer.json";
let brainJSON = {};
var activePlayer = null;
function preload() {
uiFont = "monospace";
brainJSON = loadJSON(filename);
}
function generatePlayerX() {
let playerX = obstaclePositions[Math.floor(random(0, obstaclePositions.length))];
return playerX;
}
function saveBest() { // saves the best player to file
if (bestPlayerSoFar) {
if (bestPlayerSoFar.score > getBestPlayer().score)
saveJSON(bestPlayerSoFar.brain, filename);
else
saveJSON(getBestPlayer().brain, filename);
}
}
function loadBest(x, y) { // loads the best player from file
let playerBrain = NeuralNetwork.deserialize(brainJSON);
activePlayer = new Player(x, y, playerBrain);
}
function keyPressed() {
if (key == 'S') {
saveBest();
} else if (key == 'L') {
loadBest(generatePlayerX(), height - 64);
}
}
document.addEventListener('keydown', logKey);
document.addEventListener('keyup', logKey_up);
function logKey(e) {
if(e.code == "ArrowLeft" && inteligence == false){
leftPressed = true;
}
if(e.code == "ArrowRight" && inteligence == false){
rightPressed = true
}
}
function logKey_up(e) {
if(e.code == "ArrowLeft"){
leftPressed = false;
}
if(e.code == "ArrowRight"){
rightPressed = false
}
}
function setup() {
createCanvas(1280, 720);
for (let i = 0; i < population; i++) {
players[i] = new Player(generatePlayerX(), height - 64);
}
// set text characteristics
textFont(uiFont);
textSize(uiFontSize);
}
function showUI() {
fill(255);
textAlign(LEFT);
if (!activePlayer && inteligence == true) {
text("generation: " + generation, 10, 38);
text("score: " + bestScore, 10, 74);
text("remaining: " + alivePlayers + "/" + population, 10, 110);
} else if(!activePlayer && inteligence == false){
text("lives: " + displayLives.join(""), 10, 38);
text("score: " + bestScore, 10, 74);
} else {
text("score: " + activePlayer.score, 10, 38);
}
}
function drawHearts(){
displayLives = []
for(var i = 0; i < debugLives;i++){
displayLives[displayLives.length] = "❤️";
}
}
function draw() {
for (let n = 0; n < runSpeed; n++) {
if (counter % 25 == 0) { // create new obstacles
let randIndex = Math.floor(random(0, obstaclePositions.length));
obstacles.push(new Obstacle(obstaclePositions[randIndex], -75));
trees.push(new Tree(400, -150));
trees.push(new Tree(800, -150));
}
counter++;
if(rightPressed == true){
for (let i = 0; i < players.length; i++) {
players[i].goRight();
}
}
if(inteligence == false){
population = 1;
} else{
population = con.population
}
if(leftPressed == true){
for (let i = 0; i < players.length; i++) {
players[i].goLeft();
}
}
if (obstacles.length > maxNumberOfObstacles)
obstacles.splice(0, 1);
if (trees.length > maxNumberOfTrees)
trees.splice(0, 1);
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].update();
}
if (!activePlayer) {
for (let i = 0; i < players.length; i++) {
players[i].update();
}
alivePlayers = 0;
bestScore = 0;
for (let i = 0; i < players.length; i++) {
if (players[i].alive)
alivePlayers++;
if (players[i].score > bestScore)
bestScore = players[i].score;
}
if (alivePlayers == 0 && inteligence == true){
nextGeneration();
}if(alivePlayers == 0 && inteligence == false){
nextmanualGeneration();
}
} else {
activePlayer.update();
}
}
// all the drawing
background(0,111,60);
fill(200);
rect(obstaclePositions[0], 0, obstaclePositions[obstaclePositions.length - 1] - obstaclePositions[0] + playerWidth, height);
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].show();
}
for (let i = 0; i < trees.length; i++) {
trees[i].update();
trees[i].show();
}
if (!activePlayer) {
for (let i = 0; i < players.length; i++) {
players[i].show();
}
} else {
activePlayer.show();
}
showUI();
}