-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.pde
98 lines (85 loc) · 1.92 KB
/
player.pde
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
int PLAYER_STATE_IDLE = 0;
int PLAYER_STATE_JUMPING = 1;
int PLAYER_STATE_CRAWLING = 2;
class Player extends Entity {
int state = PLAYER_STATE_IDLE;
boolean killed = false;
float fitness = 0;
String name;
int crawlFrames;
int[] topology = new int[] {99, 132, 2};
Network brain;
Player(String name) {
super(100, 300, 30, 90);
this.name = name;
ay = .62;
NetworkBuilder nb = new NetworkBuilder(topology);
brain = nb.build();
}
void think() {
brain.run(Vision.screen);
float jump = brain.layers.get(2).outputs[0];
float crawl = brain.layers.get(2).outputs[1];
//print(jump, " ", crawl, "\n");
if (crawl > 0.8) {
doCrawl();
}
else if (jump > 0.8) {
doJump();
}
else {
doIdle();
}
}
void doJump() {
if (state == PLAYER_STATE_IDLE) {
rect.h = 90;
vy = -10;
state = PLAYER_STATE_JUMPING;
}
}
void doCrawl() {
if (state == PLAYER_STATE_IDLE) {
state = PLAYER_STATE_CRAWLING;
rect.h = 55;
}
}
void doIdle() {
if (state == PLAYER_STATE_CRAWLING) {
state = PLAYER_STATE_IDLE;
rect.h = 90;
}
}
void display() {
noFill();
rect(rect.x, rect.y, rect.w, rect.h);
}
void update() {
super.update();
if (rect.y > height - rect.h) {
rect.y = height - rect.h;
vy = 0;
state = PLAYER_STATE_IDLE;
}
fitness++;
}
void mutate() {
for (int i=0; i<topology.length; i++) {
int idx = int(random(0, 2));
ArrayList<Perceptron> perceptrons = brain.layers.get(idx).perceptrons;
perceptrons.get(int(random(0, perceptrons.size()-1))).mutate();
}
}
};
class Bush extends Entity {
Bush(float x) {
super(x, height-30, 30, 30);
vx = -5 ;// -0.08; //-1 * random(0.01, 0.15);
}
};
class Bird extends Entity {
Bird(float x) {
super(x, height-90, 30, 30);
vx = -5; //-1 * random(0.01, 0.15);
}
};