我一直在写一些简单的racket
GUI程序,为我今年秋天要上课的课程做准备。我在动画方面遇到了一些问题。我正在使用基本画布,并使用动画模型,通过调用绘制过程,每个帧刷新整个画布。下面是一个示例程序。
我的问题是我必须将动画作为单独的thread
运行,或者在yield
的每个实例之后调用refresh-now
。为什么是这样?我希望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)
)))
答案 0 :(得分:4)
这不是您对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
;我把它放在模仿原始代码的行为中。
计时器是在与框架相同的事件空间中创建的,并且事件空间具有事件处理线程,这就是您不必显式创建自己的线程的原因。
How to Design Programs, 2nd ed还有另一种动画方法,即自动管理画布和更新。您只需使用函数调用big-bang
来(功能上)更新“世界状态”并将“世界状态”渲染为图像。根据您正在教授的内容,它可能对您有用,也可能没用。