forked from Calbabreaker/neat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (65 loc) · 1.66 KB
/
Copy pathscript.js
File metadata and controls
77 lines (65 loc) · 1.66 KB
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
let bird;
let gravity;
let pipes = [];
let birdSize;
let birdRadius;
let tickCount = 0;
let ticksPerFrame = 1;
let score = 0;
const speedLabel = document.getElementById("speedLabel");
const scoreLabel = document.getElementById("scoreLabel");
const fitnessLabel = document.getElementById("fitnessLabel");
function setup() {
createCanvas(innerWidth, innerHeight);
birdSize = height / 16;
birdRadius = birdSize / 2;
gravity = birdSize / 64;
bird = new Bird();
}
function draw() {
for (let i = 0; i < ticksPerFrame; i++) {
for (let i = pipes.length - 1; i >= 0; i--) {
const pipe = pipes[i];
pipe.update();
if (pipe.x < -pipe.width) {
pipes.splice(i, 1);
score++;
}
}
if (tickCount % 50 == 0) {
pipes.push(new Pipe());
}
if (pipes.length !== 0) {
const incomingPipe =
bird.x - birdRadius < pipes[0].x + pipes[0].width ? pipes[0] : pipes[1];
if (bird.outsideBounds() || bird.collidesWith(incomingPipe)) {
reset();
break;
}
}
bird.update();
tickCount += 1;
}
background(10);
pipes.forEach((pipe) => {
pipe.draw();
});
bird.draw();
speedLabel.textContent = `${ticksPerFrame}x`;
scoreLabel.textContent = `Score: ${score}`;
fitnessLabel.textContent = `Fitness: ${tickCount}`;
}
function reset() {
pipes = [];
tickCount = 0;
bird = new Bird();
score = 0;
}
function keyPressed() {
if (key == " ") {
bird.jump();
}
}
function mousePressed() {
bird.jump();
}