Reputation: 9
I am having a problem with rotating an animated image on key. I do not quite understand big-bang functions and have lost hope.
This is my code:
(require 2htdp/image)
(require 2htdp/universe)
;; Found the png of the rocketship online. Couldn't find anything smaller so, we adjust the background to not make it look gigantic.
(define SHIP "rocketship png")
;; Restricts the landing to the bottom of the scene's boundary
(define (create-rocket-scene height)
(place-image SHIP 150 (min height 550) (rectangle 300 600 'solid 'black)))
(define (key-press w ke)
(cond
\[(key=? ke "u") (make-posn 0)\]
\[(key=? ke "d") (make-posn (rotate 180))\]
\[else w\]))
(big-bang 0
\[to-draw render\]
\[on-key key-press\])
This results in numerous errors. I need to have the rocketship descend down the scene and land (this is all correct). However, once I add in the on-key and big-bang functions to allow me to rotate the image 180 degrees, it does not work. I am very frustrated. Please advise.
Upvotes: 0
Views: 81
Reputation: 31145
In order to draw a rocket both in the correct position and the correct orientation (rotation), you need to two pieces of information: 1) the position of the rocket and 2) the orientation of the rocket.
Currently your world consists of the position only, so you are you using a posn
structure that holds an x
and an y
value.
What's missing is a somewhere to store the orientation.
One option is:
(define-struct world [x y r])
Here x
and y
works as before. The r
is the angle of the rocket measured in radians.
Another option is:
(define-struct world [p r])
Here p
will hold a position in form of a posn
structure and
r
will be the angle of the rocket in radians.
Upvotes: 0