examples: add a consistent background to flappylearning, shown when the height of the view is very high (on Android)

This commit is contained in:
Delyan Angelov 2024-09-23 13:30:22 +03:00
parent b79aa0208f
commit e5d46d2f81
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED

View file

@ -1,7 +1,6 @@
module main module main
import gg import gg
import gx
import time import time
import math import math
import rand import rand
@ -11,6 +10,8 @@ import os.asset
const win_width = 500 const win_width = 500
const win_height = 512 const win_height = 512
const bg_color = gg.Color{0x96, 0xE2, 0x82, 0xFF}
struct Bird { struct Bird {
mut: mut:
x f64 = 80 x f64 = 80
@ -174,7 +175,7 @@ fn (mut app App) update() {
fn main() { fn main() {
mut app := &App{} mut app := &App{}
app.gg = gg.new_context( app.gg = gg.new_context(
bg_color: gx.white bg_color: bg_color
width: win_width width: win_width
height: win_height height: win_height
create_window: true create_window: true
@ -206,15 +207,15 @@ fn (mut app App) init_images_wrapper() {
} }
fn (mut app App) init_images() ! { fn (mut app App) init_images() ! {
app.background = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/background.png')!)!
app.bird = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/bird.png')!)! app.bird = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/bird.png')!)!
app.pipetop = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/pipetop.png')!)! app.pipetop = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/pipetop.png')!)!
app.pipebottom = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/pipebottom.png')!)! app.pipebottom = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/pipebottom.png')!)!
app.background = app.gg.create_image_from_byte_array(asset.read_bytes('assets', 'img/background.png')!)!
} }
fn frame(app &App) { fn frame(app &App) {
app.gg.begin() app.gg.begin()
app.draw() app.display()
app.gg.end() app.gg.end()
} }
@ -239,16 +240,16 @@ fn (app &App) display() {
app.bird) app.bird)
} }
} }
app.gg.draw_rect_filled(0, 510, app.background.width * 3, 5, gg.Color{0x21, 0x19, 0x28, 255})
app.gg.draw_rect_filled(0, 513, app.background.width * 3, app.background.height, bg_color)
app.gg.draw_rect_filled(550, 0, app.background.width + 50, app.background.height + 20,
bg_color)
app.gg.draw_text_def(10, 25, 'Score: ${app.score}') 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, 50, 'Max Score: ${app.max_score}')
app.gg.draw_text_def(10, 75, 'Generation: ${app.generation}') app.gg.draw_text_def(10, 75, 'Generation: ${app.generation}')
app.gg.draw_text_def(10, 100, 'Alive: ${app.alives} / ${app.nv.population}') 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) { fn on_event(e &gg.Event, mut app App) {
if e.typ == .key_down { if e.typ == .key_down {
app.key_down(e.key_code) app.key_down(e.key_code)