[racket] Manipulation in World teachpack
How can I make 2 different images move in different ways inside only 1
world.
I placed "myself" at (posn 550 550) = right end of the screen
Placed "food" at respective fixed position.
I want "shark1" to move only in x-direction automatically on clock-tick.
I want to control "myself" via key-press.
;--------------------------------------------------------------------------------------------------------------------------------------------------
;;; Initializations
(define newinitialposition (make-posn 550 550))
(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 (shark1-move-x-on-tick x1) (- x1 oppshark-move-x))
(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))))
;--------------------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------------------
;;; Running the game
;1
(big-bang width height 1/30 newinitialposition)
;2
(on-tick-event shark1-move-x-on-tick)
;3
(on-key-event move-myself-xy-on-keypress)
;4
(on-redraw mynewposition-xy) ;; ---> I guess something should be done here
;--------------------------------------------------------------------------------------------------------------------------------------------------
1) Both shark1 and myself keep moving in the x-direction because of the
on-tick-event.
2) If I don't include the ;2, and I press a key, both of them move. (I did
the key pressing part separately). <left right> controls both the images
but <up down> controls only myself which is obvious because of the way I
wrote "mynewposition-xy".
I can't seem to combine these two ideas of automatic and control into 1
world.
How do I proceed?????
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140417/cdfa8f5f/attachment-0001.html>