Skip to content

Commit 03d033f

Browse files
committed
examples: make the 2048 game update rate, independent from the frame rate as well
1 parent aafbda4 commit 03d033f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

examples/2048/2048.v

+13-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mut:
3131
state GameState = .play
3232
tile_format TileFormat = .normal
3333
moves int
34+
updates u64
3435

3536
is_ai_mode bool
3637
ai_fpm u64 = 8
@@ -956,14 +957,23 @@ fn on_event(e &gg.Event, mut app App) {
956957
}
957958

958959
fn frame(mut app App) {
960+
mut do_update := false
961+
if app.gg.timer.elapsed().milliseconds() > 15 {
962+
app.gg.timer.restart()
963+
do_update = true
964+
app.updates++
965+
}
959966
app.gg.begin()
960-
app.update_tickers()
967+
if do_update {
968+
app.update_tickers()
969+
}
961970
app.draw()
962971
app.gg.end()
963-
if app.is_ai_mode && app.state in [.play, .freeplay] && app.gg.frame % app.ai_fpm == 0 {
972+
if do_update && app.is_ai_mode && app.state in [.play, .freeplay]
973+
&& app.updates % app.ai_fpm == 0 {
964974
app.ai_move()
965975
}
966-
if app.gg.frame % 120 == 0 {
976+
if app.updates % 120 == 0 {
967977
// do GC once per 2 seconds
968978
// eprintln('> gc_memory_use: ${gc_memory_use()}')
969979
if gc_is_enabled() {

0 commit comments

Comments
 (0)