From 62027ba25490f15acfc01fb3efdafea5cb9a1ba6 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 19 Sep 2020 22:19:01 +0200 Subject: Use key-pressed? instead of key press/release handlers --- heyllama.scm | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/heyllama.scm b/heyllama.scm index c3a325f..885918a 100644 --- a/heyllama.scm +++ b/heyllama.scm @@ -49,14 +49,6 @@ (define (update-animal animal tstep) - (let ((wd (get-walk-direction animal))) - (unless (eq? wd 0) - (set-face-direction! animal wd) - (vec2-add! (get-pos animal) #v((* tstep wd 0.2) 0)) - (set-animation-step! animal - (floor-remainder - (+ (get-animation-step animal) wd) - 16)))) (vec2-add! (get-pos animal) (get-vel animal)) (if (> (vec2-y (get-pos animal)) 200.0) (set-vel! animal @@ -119,6 +111,22 @@ (update-animal llama tstep) + (when (key-pressed? 'right) + (set-face-direction! llama 1) + (vec2-add! (get-pos llama) #v((* tstep 0.2) 0)) + (set-animation-step! llama + (floor-remainder + (+ (get-animation-step llama) 1) + 16))) + + (when (key-pressed? 'left) + (set-face-direction! llama -1) + (vec2-add! (get-pos llama) #v((* tstep -0.2) 0)) + (set-animation-step! llama + (floor-remainder + (- (get-animation-step llama) 1) + 16))) + ;; Ensure llama is near centre of screen (let ((fovea (make-rect 150.0 150.0 350.0 350.0)) (llama-rect (get-screen-rect llama))) @@ -129,22 +137,13 @@ (define (key-press key scancode modifier repeat?) (case key ((q) (abort-game)) - ((right) (set-walk-direction! llama 1)) - ((left) (set-walk-direction! llama -1)) ((space) (unless repeat? (set-vel! llama #v(0.0 15.0)))))) -(define (key-release key scancode modifier) - (case key - ((right) (set-walk-direction! llama 0)) - ((left) (set-walk-direction! llama 0)))) - - (run-game #:window-title "Hey Llama!" #:load load #:draw draw #:key-press key-press #:update-hz 60 - #:key-release key-release #:update update) -- cgit v1.2.3