-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
120 lines (102 loc) · 2.47 KB
/
sketch.js
File metadata and controls
120 lines (102 loc) · 2.47 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
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
var population;
let lifespan = 200;
let lifeP, target;
let cnt = 0;
let obstacles = [];
let tmpobstacle = [-1, -1, -1, -1];
let startingPoint;
let playerRocket;
let restartButton, lifespanE, speedS;
function setup() {
createCanvas(400, 400);
startingPoint = createVector(width/2, height);
population = new Population();
lifeP = createP();
speedS = createSlider(1, 180, 60);
target = new Target(10, 10);
restartButton = createButton("Restart");
restartButton.mousePressed(()=>{
population = new Population();
lifespan = int(lifespanE.value());
cnt = 0;
});
lifespanE = createInput("200");
playerRocket = new Rocket();
}
function draw() {
background(0);
speed = speedS.value();
for(let i=0;i<speed;i++) {
population.run();
cnt ++;
if(cnt >= lifespan) {
population.evaluate();
population.selection();
cnt = 0;
break;
}
}
lifeP.html(`Time: ${cnt}`);
showObstacles();
target.show();
population.show();
// playerRocket.show(color = [100, 200, 0]);
}
function mousePressed() {
if(target.contains(mouseX, mouseY)) {
target.picked = true;
} else {
tmpobstacle[0] = mouseX;
tmpobstacle[1] = mouseY;
}
}
function mouseDragged() {
if(target.picked) {
target.x = mouseX;
target.y = mouseY;
}
}
function mouseReleased() {
if(target.picked) {
target = new Target(mouseX, mouseY);
} else {
tmpobstacle[2] = mouseX - tmpobstacle[0];
tmpobstacle[3] = mouseY - tmpobstacle[1];
obstacles.push([...tmpobstacle])
}
}
function showObstacles() {
fill(200, 0, 0);
for(let obstacle of obstacles) {
rect(...obstacle);
}
}
function keyIsDow() {
if(keyCode == LEFT_ARROW) {
playerRocket.applyForce(createVector(0.1, 0));
playerRocket.update();
} else if (keyCode == RIGHT_ARROW) {
game.snake.go('right');
} else if (keyCode == UP_ARROW) {
game.snake.go('up');
} else if (keyCode == DOWN_ARROW) {
game.snake.go('down');
}
}
class Target {
constructor(x, y) {
this.x = x;
this.y = y;
this.picked = false;
}
contains(pointA, pointB) {
return dist(this.x, this.y, pointA, pointB) < 16;
}
show() {
fill(255);
ellipse(this.x, this.y, 16, 16);
}
copy() {
return createVector(this.x, this.y);
}
}