1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

flappylearning: toggle update period with space, exit with esc

This commit is contained in:
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

View File

@ -1,6 +1,7 @@
module main
import gg
import sokol.sapp
import gx
import os
import time
@ -11,7 +12,6 @@ import neuroevolution
const (
win_width = 500
win_height = 512
timer_period = 24 // ms
)
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 {}
}
}