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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
module main
import gg
import gx
import os
import time
import math
import rand
import neuroevolution
const (
win_width = 500
win_height = 512
)
struct Bird {
mut:
x f64 = 80
y f64 = 250
width f64 = 40
height f64 = 30
alive bool = true
gravity f64
velocity f64 = 0.3
jump f64 = -6
}
fn (mut b Bird) flap() {
b.gravity = b.jump
}
fn (mut b Bird) update() {
b.gravity += b.velocity
b.y += b.gravity
}
fn (b Bird) is_dead(height f64, pipes []Pipe) bool {
if b.y >= height || b.y + b.height <= 0 {
return true
}
for pipe in pipes {
if !(b.x > pipe.x + pipe.width || b.x + b.width < pipe.x || b.y > pipe.y + pipe.height
|| b.y + b.height < pipe.y) {
return true
}
}
return false
}
struct Pipe {
mut:
x f64 = 80
y f64 = 250
width f64 = 40
height f64 = 30
speed f64 = 3
}
fn (mut p Pipe) update() {
p.x -= p.speed
}
fn (p Pipe) is_out() bool {
return p.x + p.width < 0
}
struct App {
mut:
gg &gg.Context
background gg.Image
bird gg.Image
pipetop gg.Image
pipebottom gg.Image
pipes []Pipe
birds []Bird
score int
max_score int
width f64 = win_width
height f64 = win_height
spawn_interval f64 = 90
interval f64
nv neuroevolution.Generations
gen []neuroevolution.Network
alives int
generation int
background_speed f64 = 0.5
background_x f64
timer_period_ms int = 24
}
fn (mut app App) start() {
app.interval = 0
app.score = 0
app.pipes = []
app.birds = []
app.gen = app.nv.generate()
for _ in 0 .. app.gen.len {
app.birds << Bird{}
}
app.generation++
app.alives = app.birds.len
}
fn (app &App) is_it_end() bool {
for i in 0 .. app.birds.len {
if app.birds[i].alive {
return false
}
}
return true
}
fn (mut app App) update() {
app.background_x += app.background_speed
mut next_holl := f64(0)
if app.birds.len > 0 {
for i := 0; i < app.pipes.len; i += 2 {
if app.pipes[i].x + app.pipes[i].width > app.birds[0].x {
next_holl = app.pipes[i].height / app.height
break
}
}
}
for j, mut bird in app.birds {
if bird.alive {
inputs := [
bird.y / app.height,
next_holl,
]
res := app.gen[j].compute(inputs)
if res[0] > 0.5 {
bird.flap()
}
bird.update()
if bird.is_dead(app.height, app.pipes) {
bird.alive = false
app.alives--
app.nv.network_score(app.gen[j], app.score)
if app.is_it_end() {
app.start()
}
}
}
}
for k := 0; k < app.pipes.len; k++ {
app.pipes[k].update()
if app.pipes[k].is_out() {
app.pipes.delete(k)
k--
}
}
if app.interval == 0 {
delta_bord := f64(50)
pipe_holl := f64(120)
holl_position := math.round(rand.f64() * (app.height - delta_bord * 2.0 - pipe_holl)) +
delta_bord
app.pipes << Pipe{
x: app.width
y: 0
height: holl_position
}
app.pipes << Pipe{
x: app.width
y: holl_position + pipe_holl
height: app.height
}
}
app.interval++
if app.interval == app.spawn_interval {
app.interval = 0
}
app.score++
app.max_score = if app.score > app.max_score { app.score } else { app.max_score }
}
fn main() {
mut app := &App{
gg: 0
}
mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf'))
$if android {
font_path = 'fonts/RobotoMono-Regular.ttf'
}
app.gg = gg.new_context(
bg_color: gx.white
width: win_width
height: win_height
create_window: true
window_title: 'flappylearning-v'
frame_fn: frame
event_fn: on_event
user_data: app
init_fn: init_images
font_path: font_path
)
app.nv = neuroevolution.Generations{
population: 50
network: [2, 2, 1]
}
app.start()
go app.run()
app.gg.run()
}
fn (mut app App) run() {
for {
app.update()
time.sleep(app.timer_period_ms * time.millisecond)
}
}
fn init_images(mut app App) {
$if android {
background := os.read_apk_asset('img/background.png') or { panic(err) }
app.background = app.gg.create_image_from_byte_array(background)
bird := os.read_apk_asset('img/bird.png') or { panic(err) }
app.bird = app.gg.create_image_from_byte_array(bird)
pipetop := os.read_apk_asset('img/pipetop.png') or { panic(err) }
app.pipetop = app.gg.create_image_from_byte_array(pipetop)
pipebottom := os.read_apk_asset('img/pipebottom.png') or { panic(err) }
app.pipebottom = app.gg.create_image_from_byte_array(pipebottom)
} $else {
app.background = app.gg.create_image(os.resource_abs_path('assets/img/background.png'))
app.bird = app.gg.create_image(os.resource_abs_path('assets/img/bird.png'))
app.pipetop = app.gg.create_image(os.resource_abs_path('assets/img/pipetop.png'))
app.pipebottom = app.gg.create_image(os.resource_abs_path('assets/img/pipebottom.png'))
}
}
fn frame(app &App) {
app.gg.begin()
app.draw()
app.gg.end()
}
fn (app &App) display() {
for i := 0; i < int(math.ceil(app.width / app.background.width) + 1.0); i++ {
background_x := i * app.background.width - math.floor(int(app.background_x) % int(app.background.width))
app.gg.draw_image(f32(background_x), 0, app.background.width, app.background.height,
app.background)
}
for i, pipe in app.pipes {
if i % 2 == 0 {
app.gg.draw_image(f32(pipe.x), f32(pipe.y + pipe.height - app.pipetop.height),
app.pipetop.width, app.pipetop.height, app.pipetop)
} else {
app.gg.draw_image(f32(pipe.x), f32(pipe.y), app.pipebottom.width, app.pipebottom.height,
app.pipebottom)
}
}
for bird in app.birds {
if bird.alive {
app.gg.draw_image(f32(bird.x), f32(bird.y), app.bird.width, app.bird.height,
app.bird)
}
}
app.gg.draw_text_def(10, 25, 'Score: $app.score')
app.gg.draw_text_def(10, 50, 'Max Score: $app.max_score')
app.gg.draw_text_def(10, 75, 'Generation: $app.generation')
app.gg.draw_text_def(10, 100, 'Alive: $app.alives / $app.nv.population')
}
fn (app &App) draw() {
app.display()
}
fn on_event(e &gg.Event, mut app App) {
if e.typ == .key_down {
app.key_down(e.key_code)
}
}
fn (mut app App) key_down(key gg.KeyCode) {
// global keys
match key {
.escape {
exit(0)
}
._0 {
app.timer_period_ms = 0
}
.space {
if app.timer_period_ms == 24 {
app.timer_period_ms = 4
} else {
app.timer_period_ms = 24
}
}
else {}
}
}
|