[racket] Manipulation in World teachpack

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Apr 18 13:48:20 EDT 2014

On Apr 17, 2014, at 11:31 PM, Zee Ken <udaybhasker552009 at gmail.com> wrote:

> Where do I use the place-image option to place the myself, shark, food1, food2 images at required positions? Do I give that in the initial-world?
> The define-struct has to be exactly as you showed right? Or do I have to give the positions by using the make-posn there?
> I am a total amateur to Racket. I hope you understand my lack of knowledge. Sorry for the inconvenience.
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

1. You are using the wrong world teachpack. This one has been deprecated for eight years. 

2. Your program didn't work even as far you tried. See below for something that moves "you" on 
both clock ticks and key events. 

3. Danny's advice is 100% on target. But see 


for a more extensive introduction. See sections on designing (interactive world) programs and designing functions. 

4. If this is homework, you are probably using the above text and you are not following the design recipe. 

5. If it is not homework, please do scan the above so we can help you to help yourself. 


(require 2htdp/image)
(require 2htdp/universe)

;;; Initializations
(define newinitialposition (make-posn 550 550))

(define myself (circle 200 'solid 'red))
(define food (square 110 'solid 'green))
(define shark1 (triangle 100 'solid 'navy))
(define background (empty-scene 600 600))

(define (mynewposition-xy p)
  (place-image myself (posn-x p) (posn-y p) 
               (place-image food 200 250 
               (place-image food 100 550 
               (place-image shark1 (posn-x p) 400 background)))))

;;; Movement handlers

(define shark1-move-x 20)
(define oppshark-move-x 1)
(define (shark1-move-x-on-tick x1) (make-posn (- (posn-x x1) oppshark-move-x) (posn-y x1)))
(define (shark1-move-xy-on-tick p)
  (make-posn (shark1-move-x-on-tick (posn-x p)) (posn-y p)))

(define myself-move-x 10)
(define (myself-move-on-tick x) (- x myself-move-x))
(define (myself-move-xy-on-tick p)
  (make-posn (myself-move-on-tick (posn-x p)) (myself-move-on-tick (posn-y p))))

(define (move-myself-xy-on-keypress p ke)
    [(key=? "left" ke) (make-posn (- (posn-x p) 10) (posn-y p))]
    [(key=? "right" ke) (make-posn (+ (posn-x p) 10) (posn-y p))]
    [else p]))


(define (main x)
  (big-bang newinitialposition
            [on-tick shark1-move-x-on-tick]
            [to-draw mynewposition-xy]
            [on-key move-myself-xy-on-keypress]))

Posted on the users mailing list.