[racket] Manipulation in World teachpack
When he said world, he meant a piece of data that describes the state of the program, for example a data structure that stores the positions of “myself” and “shark1”, etc in a structure:
(define-struct world (myself-posn food1-posn food2-posn shark1-posn))
And when you use big-bang and on-tick-event, etc, you should use instances of this world structure:
(define initial-world
(make-world myself-initialposition
food1-initialposition
food2-initialposition
shark1-initialposition))
(define (world-move-on-tick w) ; w should be an instance of the world structure
(make-world (myself-move-xy-on-tick (world-myself-posn w))
(world-food1-posn w)
(world-food2-posn w)
(shark1-move-xy-on-tick (world-shark1-posn w))))
(big-bang width height 1/30 initial-world)
(on-tick-event world-move-on-tick)
Does that help?
On Apr 17, 2014, at 4:09 PM, Zee Ken <udaybhasker552009 at gmail.com> wrote:
> 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?????
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140417/b0625afc/attachment-0001.html>