shaol
shaol

Reputation: 13

DrRacket tutorial help using multiple images in using animate

New to programming but i'm following a tutorial from Racket and Im trying to have a rocket land on a landing base. Got the rocket to work but I wanted to add some more objects like a landing base. Here is what I have:

; constants
(define width 100)
(define height 100)
(define background "sky blue")
(define mtscn (empty-scene width height background))
(define rocket.)
(define rocket-center-to-bottom
  (- height (/ (image-height rocket) 2)))
(define base.)
(define base-center-to-bottom
  (- height (/ (image-height rocket) 2)))

  ; functions
  (define (create-rocket-scene.v6 h)
    (cond
    [(<= h rocket-center-to-bottom)
  (place-image rocket 50 h mtscn)]
    [(> h rocket-center-to-bottom)
     (place-image rocket 50 rocket-center-to-bottom mtscn)]
    [(<= h base-center-to-bottom)
   (place-image base 50 h mtscn)]
    [(> h base-center-to-bottom)
     (place-image base 50 base-center-to-bottom mtscn)]))


(animate create-rocket-scene.v6)

basically copied and paste the rocket code then renamed rocket to base then made a base image. It says its working but the base doesn't show up. I want the base image to stay at the bottom while the rocket comes from the top to the bottom where the base is. Thanks for any help

Upvotes: 1

Views: 1935

Answers (1)

soegaard
soegaard

Reputation: 31147

This the where the problem is:

(cond
    [(<= h rocket-center-to-bottom) (place-image rocket 50 h mtscn)]
    [(> h rocket-center-to-bottom)  (place-image rocket 50 rocket-center-to-bottom mtscn)]
    [(<= h base-center-to-bottom)   (place-image base 50 h mtscn)]
    [(> h base-center-to-bottom)    (place-image base 50 base-center-to-bottom mtscn)])

A cond-expression finds the first "question" that is true, and uses the "answer" that goes with it.

If (<= h rocket-center-to-bottom) is true, then the first clause is used, and (place-image rocket 50 h mtscn) is used. This means that the third clause where you use base is never reached.

Instead you need to draw both the mountain scene and the base to the same picture:

(place-image base 50 h 
  (place-image rocket 50 h mtscn))

This places the rocket on top of the mountain scene, and on top of that the base.

That is, you need only two clauses in the cond:

(cond
    [(<= h rocket-center-to-bottom)     (place-image base 50 h 
                                          (place-image rocket 50 h mtscn))]
    [(> h rocket-center-to-bottom)      ...draw-both-here...])

Upvotes: 1

Related Questions