flappylearning: toggle update period with `space`, exit with `esc`

pull/7759/head
Delyan Angelov 2020-12-31 17:46:37 +02:00
parent 1fb6710c89
commit 06766fd0eb
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 29 additions and 4 deletions

View File

@ -1,6 +1,7 @@
module main module main
import gg import gg
import sokol.sapp
import gx import gx
import os import os
import time import time
@ -9,9 +10,8 @@ import rand
import neuroevolution import neuroevolution
const ( const (
win_width = 500 win_width = 500
win_height = 512 win_height = 512
timer_period = 24 // ms
) )
struct Bird { struct Bird {
@ -86,6 +86,7 @@ mut:
generation int generation int
background_speed f64 = 0.5 background_speed f64 = 0.5
background_x f64 background_x f64
timer_period_ms int = 24
} }
fn (mut app App) start() { fn (mut app App) start() {
@ -185,6 +186,7 @@ fn main() {
create_window: true create_window: true
window_title: 'flappylearning-v' window_title: 'flappylearning-v'
frame_fn: frame frame_fn: frame
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: os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')
@ -201,7 +203,7 @@ fn main() {
fn (mut app App) run() { fn (mut app App) run() {
for { for {
app.update() 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() { fn (app &App) draw() {
app.display() 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 {}
}
}