0
votes

I'm using DrRacket 6.4, and I want to show images using the 2htdp/image library. If the user types in "start", an image will be printed. However, I can't get images to show while trying to show them from inside a loop; it seems that images only display when the procedure has "returned".

This works:

(define (showimg p) (place-image
   (triangle 32 "solid" "red")
   24 24
   p))

;; prints an image
(define (a p)
  (define command (read-line))
  (cond [(string=? command "start") (showimg p)]))

(a (empty-scene 160 90))

This does not:

;; should print an image indefinitely
(define (a p)
  (define command (read-line))
  (cond [(string=? command "start") (showimg p)])
  (a (empty-scene 160 90))
)

(a (empty-scene 160 90))

Is there any way I can get this to work using recursion (as it currently is), or if needed, some other way to get user input continuously, while showing images?

Thank you.

1

1 Answers

2
votes

place-image returns an image, it doesn't display one.
The displaying is done by the REPL when a function returns an image.

All

(cond [(string=? command "start") (showimg p)]) 

is doing is (possibly) creating an image which is immediately thrown away.

What you can do is use print to display the image, as DrRacket is very clever with printing.

The following has the effect I believe you're after:

(define (a p)
  (define command (read-line))
  (cond [(string=? command "start") (print (showimg p))])
  (a p))