-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappy.js
More file actions
86 lines (85 loc) · 1.55 KB
/
flappy.js
File metadata and controls
86 lines (85 loc) · 1.55 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
flappy = {
screen: {
x: 10,
y: 10
},
Alive: true,
Fitness: 0,
bird: {
posX: 4,
posY: 2,
velocity: 1
},
obs: {
Y: 12,
topX: 3,
bottomX: 6
},
Reset: function(){
this.obs.Y = 12
this.obs.topX = 3
this.obs.bottomX = 6
this.bird = {
posX: 4,
posY: 2,
velocity: 1
};
this.Alive = true;
this.Fitness = 0;
},
Export: function() {
var out = [];
for(var x = 0; x < this.screen.x; x++) {
for(var y = 0; y < this.screen.y; y++) {
var pos = x * this.screen.x + y
if(this.bird.posX == x && this.bird.posY == y) {
out[pos] = 1.0;
} else if(this.obs.Y == y && (x <= this.obs.topX || x >= this.obs.bottomX)) {
out[pos] = -1.0;
} else {
out[pos] = 0;
}
}
}
return out;
},
Next: function(input) {
this.BirdNext()
if(input >= 0.5) {
this.bird.velocity = 3
}
this.ObstacleNext()
this.CheckAlive()
},
BirdNext: function() {
if(this.bird.velocity > -1) {
if(this.bird.posX > 0) {
this.bird.posX--
}
this.bird.velocity--
} else {
this.bird.posX++
}
},
ObstacleNext: function() {
if(this.obs.Y < 0) {
this.obs.Y = this.screen.y
this.obs.topX = Math.floor((Math.random() * 5))
this.obs.bottomX = this.obs.topX + 3
} else {
this.obs.Y--
}
},
CheckAlive: function() {
if(this.bird.posX >= this.screen.x) {
this.Alive = false
}
if(this.bird.posY == this.obs.Y) {
if(this.bird.posX >= this.obs.bottomX || this.bird.posX <= this.obs.topX) {
this.Alive = false
}
} else if(this.bird.posY == this.obs.Y - 1) {
this.Fitness++;
}
}
};