example: support running flappylearning on Android (#8612)
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
Before Width: | Height: | Size: 382 B After Width: | Height: | Size: 382 B |
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
@ -40,8 +40,8 @@ fn (b Bird) is_dead(height f64, pipes []Pipe) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for pipe in pipes {
|
for pipe in pipes {
|
||||||
if !(b.x > pipe.x + pipe.width ||
|
if !(b.x > pipe.x + pipe.width || b.x + b.width < pipe.x || b.y > pipe.y + pipe.height
|
||||||
b.x + b.width < pipe.x || b.y > pipe.y + pipe.height || b.y + b.height < pipe.y) {
|
|| b.y + b.height < pipe.y) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,8 +153,8 @@ fn (mut app App) update() {
|
||||||
if app.interval == 0 {
|
if app.interval == 0 {
|
||||||
delta_bord := f64(50)
|
delta_bord := f64(50)
|
||||||
pipe_holl := f64(120)
|
pipe_holl := f64(120)
|
||||||
holl_position := math.round(rand.f64() *
|
holl_position := math.round(rand.f64() * (app.height - delta_bord * 2.0 - pipe_holl)) +
|
||||||
(app.height - delta_bord * 2.0 - pipe_holl)) + delta_bord
|
delta_bord
|
||||||
app.pipes << Pipe{
|
app.pipes << Pipe{
|
||||||
x: app.width
|
x: app.width
|
||||||
y: 0
|
y: 0
|
||||||
|
@ -178,6 +178,10 @@ fn main() {
|
||||||
mut app := &App{
|
mut app := &App{
|
||||||
gg: 0
|
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(
|
app.gg = gg.new_context(
|
||||||
bg_color: gx.white
|
bg_color: gx.white
|
||||||
width: win_width
|
width: win_width
|
||||||
|
@ -189,7 +193,7 @@ fn main() {
|
||||||
event_fn: on_event
|
event_fn: on_event
|
||||||
user_data: app
|
user_data: app
|
||||||
init_fn: init_images
|
init_fn: init_images
|
||||||
font_path: os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')
|
font_path: font_path
|
||||||
)
|
)
|
||||||
app.nv = neuroevolution.Generations{
|
app.nv = neuroevolution.Generations{
|
||||||
population: 50
|
population: 50
|
||||||
|
@ -208,10 +212,21 @@ fn (mut app App) run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_images(mut app App) {
|
fn init_images(mut app App) {
|
||||||
app.background = app.gg.create_image(os.resource_abs_path('./img/background.png'))
|
$if android {
|
||||||
app.bird = app.gg.create_image(os.resource_abs_path('./img/bird.png'))
|
background := os.read_apk_asset('img/background.png') or { panic(err) }
|
||||||
app.pipetop = app.gg.create_image(os.resource_abs_path('./img/pipetop.png'))
|
app.background = app.gg.create_image_from_byte_array(background)
|
||||||
app.pipebottom = app.gg.create_image(os.resource_abs_path('./img/pipebottom.png'))
|
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) {
|
fn frame(app &App) {
|
||||||
|
|