-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
117 lines (94 loc) · 2.09 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
const PIPE_RATE = 120;
const TOTAL_PLAYER = 500;
var counter = 0;
var birds = [];
var savedBirds = [];
var pipes = [];
var button;
var scoretext;
var gentext;
let dead;
let genCount = 0;
let bestScore =0;
let slider;
function setup() {
dead = false;
createCanvas(400, 600);
slider = createSlider(1, 100, 1);
for(let i =0; i<TOTAL_PLAYER;i++)
{
birds[i] = new Bird();
}
}
function draw() {
for(let c =0;c<slider.value();c++){
if (counter % PIPE_RATE == 0) {
pipes.push(new Pipe());
}
counter++;
for (let i = 0; i < pipes.length; i++) {
pipes[i].update();
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
for(let bird of birds){
bird.gameScore++;
}
}
for(let j = birds.length -1; j>=0; j--){
if (pipes[i].coll(birds[j])) {
savedBirds.push(birds.splice(j,1));
// deathScreen();
// dead =true;
}
}
}
for(let bird of birds)
{
bird.think(pipes);
bird.update();
}
if(birds.length === 0){
counter = 0;
nextGeneration();
pipes= [];
}
for(let i = birds.length-1;i>=0;i--){
if(birds[i].gameScore > bestScore)bestScore = birds[i].gameScore;
}
}
textAlign(CENTER);
if (!dead) {
//scoretext = text('Score:' + score, 30, 50);
scoretext = text('Score:' + bestScore, 80, height -50);
gentext = text('Generation:' + genCount,70, 50);
}
background(150, 150, 150);
for(let bird of birds){
bird.show();
}
for(let pipe of pipes){
pipe.show();
}
}
function deathScreen() {
background(255, 150, 0);
noLoop();
button = createButton('Restart!');
button.position(width / 2 - 30, height / 2 + 40);
button.mousePressed(resetSketch);
textAlign(CENTER);
text('YOU DEAD!', width / 2, height / 2);
textAlign(CENTER);
text('Your Score:' + bestScore, width / 2, height / 2 + 20);
}
function resetSketch() {
button.remove();
frameCount = 0;
loop();
setup();
}
// function keyPressed() {
// if (key == 'f') {
// bird.fly();
// }
// }