From 06766fd0eb680e58be5ebe22f2a6fbce748679b2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 31 Dec 2020 17:46:37 +0200 Subject: [PATCH] flappylearning: toggle update period with `space`, exit with `esc` --- examples/flappylearning/game.v | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/examples/flappylearning/game.v b/examples/flappylearning/game.v index b9a816020d..b87adb38f0 100644 --- a/examples/flappylearning/game.v +++ b/examples/flappylearning/game.v @@ -1,6 +1,7 @@ module main import gg +import sokol.sapp import gx import os import time @@ -9,9 +10,8 @@ import rand import neuroevolution const ( - win_width = 500 - win_height = 512 - timer_period = 24 // ms + win_width = 500 + win_height = 512 ) struct Bird { @@ -86,6 +86,7 @@ mut: generation int background_speed f64 = 0.5 background_x f64 + timer_period_ms int = 24 } fn (mut app App) start() { @@ -185,6 +186,7 @@ fn main() { create_window: true window_title: 'flappylearning-v' frame_fn: frame + event_fn: on_event user_data: app init_fn: init_images font_path: os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') @@ -201,7 +203,7 @@ fn main() { fn (mut app App) run() { for { app.update() - time.sleep_ms(timer_period) + time.sleep_ms(app.timer_period_ms) } } @@ -248,3 +250,26 @@ fn (app &App) display() { fn (app &App) draw() { app.display() } + +fn on_event(e &sapp.Event, mut app App) { + if e.typ == .key_down { + app.key_down(e.key_code) + } +} + +fn (mut app App) key_down(key sapp.KeyCode) { + // global keys + match key { + .escape { + exit(0) + } + .space { + if app.timer_period_ms == 24 { + app.timer_period_ms = 3 + } else { + app.timer_period_ms = 24 + } + } + else {} + } +}