-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.js
90 lines (74 loc) · 1.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Paddle {
constructor(r,g,b, brain, side) {
this.y = displayHeight / 2;
if(side == "Left"){
this.x = 100;
}
else {
this.x = displayWidth - 100;
}
this.w = 1;
this.h = 100;
this.r = r;
this.g = g;
this.b = b;
//Copies so not a pointer
this.brain = brain.copy();
}
draw() {
//beginPath();
rect(this.x,this.y, this.w, this.h);
//fillStyle = 'Black';
fill(this.r,this.g,this.b,100);
}
up() {
if(this.y <= 0){
this.y = 1;
}
else {
this.y -= 8;
}
}
down() {
if(this.y + this.h > displayHeight){
this.y = displayHeight - this.h - 1;
}
else {
this.y += 8;
}
}
nothing() {
this.y = this.y;
}
mutate() {
//Changes weights within neural network
//Allows for evolution and adaption
this.brain.mutate(0.1);
}
name(){
//Returns Name of model
//Used to check that not pointer
this.brain.name();
}
update(ball) {
//Thinking
let inputs = [];
inputs[0] = this.y;
inputs[1] = ball.x;
inputs[2] = ball.y;
//Adding velocity may allow prediction of where ball is in the future
//maybe too complex
//inputs[3] = ball.vel.x;
//inputs[4] = ball.vel.y;
let output = this.brain.predict(inputs);
if (output[0] > output[1] && output[0] > output[2]) {
this.up();
}
if (output[1] > output[2] && output[1] > output[0]) {
this.nothing();
}
if (output[2] > output[1] && output[2] > output[0]) {
this.down();
};
}
}