-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.js
72 lines (63 loc) · 1.63 KB
/
Paddle.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
class Paddle {
constructor(isLeft) {
this.isLeft = isLeft;
this.w = 10;
this.h = 50;
this.pos = createVector(this.isLeft ? 20 : width - 20, height / 2);
// this.x = this.isLeft ? 20 : width - 20;
// this.y = height / 2;
this.brain = new Brain(3, 8, 3);
this.speed = 4;
this.isGameOver = false;
this.score = 0;
this.fitness = 0;
}
hit = (ball) => {
// if (this.isLeft) {
if (this.pos.y - this.h / 2 < ball.pos.y + ball.d / 2 && this.pos.y + this.h / 2 > ball.pos.y - ball.d / 2) {
return true;
}
return false;
// } else {
// if (this.y - this.h / 2 < ball.y + ball.d / 2 || this.y + this.h / 2 > ball.y - ball.d / 2) {
// return true;
// }
// return false;
// }
};
moveUp = () => {
if (this.pos.y - this.h / 2 > 0) {
this.pos.set(this.pos.x, this.pos.y - this.speed);
}
};
moveDown = () => {
if (this.pos.y + this.h / 2 < height) {
this.pos.set(this.pos.x, this.pos.y + this.speed);
}
};
update = (ball) => {
// if (keyIsDown(UP_ARROW)) {
// this.y -= this.speed;
// } else if (keyIsDown(DOWN_ARROW)) {
// this.y += this.speed;
// }
const input = [this.pos.y, ball.pos.y, this.pos.dist(ball.pos)];
// console.log(input);
const prediction = this.brain.think(input);
switch (1) {
case prediction[0]:
this.moveUp();
break;
case prediction[1]:
break;
case prediction[2]:
this.moveDown();
break;
}
};
show = () => {
fill(255);
rectMode(CENTER);
rect(this.pos.x, this.pos.y, this.w, this.h);
};
}