Racket现在刷新,线程,并屈服



我一直在编写一些简单的racket GUI程序,为秋季的课程做准备。我在动画方面遇到了一些问题。我使用的是一个基本画布,并使用动画模型,在该模型中,通过调用绘制过程,每帧都会刷新整个画布。下面是一个示例程序。

我的问题是,我必须将动画作为单独的thread运行,或者在refresh-now的每个实例之后调用yield。为什么会这样?我希望refresh-now能立即刷新图像,而无需我做额外的工作。

我读过racket页面上的动画示例,发现它们通常直接绘制到画布上。我知道由于画布是双缓冲的,所以效果很好。。。但对于我的应用程序来说,仅仅让油漆程序来承担负载更容易,因为我无论如何都需要一个工作油漆程序,以防最小化等。(当然,yield不是一个巨大的负担,但如果不需要的话,它会更容易教授。)

谢谢,
约翰·

#lang racket
; Demonstrate simple animation in Racket
(require racket/gui)
(define min-x 0)
(define min-y 0)
(define max-x 200)
(define max-y 200)
; Three vertexes of the triangle, expressed relative to a starting x and y location.
(define triangle-vertexes [list 
(list 10 0) 
(list 0 20) 
(list 20 20)])
(define triangle-x 20)
(define triangle-y 20)
; Move a triangle by a (delta-x, delta-y) pair
(define (move-triangle adjust)
(set! triangle-x (+ triangle-x (first adjust)))
(set! triangle-y (+ triangle-y (second adjust))))
; Adjust the location of a vertex by adding an (x,y) adjustment to it.
; Could also be defined using map.
(define (triangle-adjust adjust vertex)
(list (+ (first adjust) (first vertex))
(+ (second adjust) (second vertex))))
; Create the paint-callback function.
; It should:
; - draw a triangle at the current location
(define (draw-triangle dc)
(let ((vertex1 (triangle-adjust (list triangle-x triangle-y) (first  triangle-vertexes)))
(vertex2 (triangle-adjust (list triangle-x triangle-y) (second triangle-vertexes)))
(vertex3 (triangle-adjust (list triangle-x triangle-y) (third  triangle-vertexes))))
(send dc draw-line (first vertex1) (second vertex1) (first vertex2) (second vertex2))
(send dc draw-line (first vertex2) (second vertex2) (first vertex3) (second vertex3))
(send dc draw-line (first vertex3) (second vertex3) (first vertex1) (second vertex1))))

(define frame (new frame%
[label "Animation Example"]
[width 800]
[height 800]))
(define triangle-canvas (new canvas% [parent frame]
[paint-callback
(lambda (canvas dc)
(display "callback called")
(draw-triangle dc))]))
(send frame show #t)
; run a thunk (a procedure of zero arguments) n times
; only useful if thunk has side-effects
(define (loop n thunk)
(cond 
((> n 0) (thunk)
(loop (- n 1) thunk))
(else false)))
; Animate the triangle.  We have to either run this in a different thread from
; the event loop or yield each time we want something to be drawn.
(define (animate-triangle)
(loop 30 
(lambda ()
(move-triangle (list 10 10))
(send triangle-canvas refresh-now)
;      (send triangle-canvas flush)
(yield)
;      (sleep 0.1)
)))

这不是关于refresh-now的问题的答案,但显式线程和循环的更好替代方案是timer%类:

;; This goes after (send frame show #t), replacing loop and animate-triangle
(define timer-counter 0)
(define timer
  (new timer%
       (interval 100)  ;; update every 100 ms
       (notify-callback
        (lambda ()
          (cond [(< timer-counter 30)
                 (set! timer-counter (add1 timer-counter))
                 (move-triangle (list 10 10))
                 (send triangle-canvas refresh)]
                [else
                 (send timer stop)])))))

如果根据三角形的状态重新定义停止条件,就可以去掉辅助timer-counter;我把它放进去是为了模仿你原来代码的行为。

计时器是在与帧相同的事件空间中创建的,并且事件空间有一个事件处理线程,这就是为什么您不必显式创建自己的线程。

如何设计程序,第二版有另一种动画方法,其中画布和更新是自动管理的。您只需使用函数调用big-bang即可(从功能上)更新"世界状态"并将"世界状态"渲染为图像。根据你所教的内容,它可能对你有用,也可能对你没有用处。

相关内容

  • 没有找到相关文章

最新更新