[racket] WORLD manipulation doubt

From: Marco Morazan (morazanm at gmail.com)
Date: Fri May 2 00:52:39 EDT 2014

Your on-key-event handler is:

;Movement of Myself in XY-direction on key-press
(define (alter-myself-xy-on-key w key)
  (cond
    [(key=? key 'up)
     (make-world (make-posn (posn-x (world-myself w))
                            (- (posn-y (world-myself w)) key-distance))
                 (world-shark1 w)
                 (world-shark2 w)
                 (world-food1 w))]
    [(key=? key 'down)
     (make-world (make-posn (posn-x (world-myself w))
                            (+ (posn-y (world-myself w)) key-distance))
                 (world-shark1 w)
                 (world-shark2 w)
                 (world-food1 w))]
    [(key=? key 'left)
     (make-world (make-posn (- (posn-x (world-myself w)) key-distance)
                            (posn-y (world-myself w)))
                 (world-shark1 w)
                 (world-shark2 w)
                 (world-food1 w))]
    [(key=? key 'right)
     (make-world (make-posn (+ (posn-x (world-myself w)) key-distance)
                            (posn-y (world-myself w)))
                 (world-shark1 w)
                 (world-shark2 w)
                 (world-food1 w))]
    [else w]))

This suggests that your reasoning is that only myself is affected by key
events and only 'left and 'right key events change the state of the game.
However, you want the 'space to also affect the state of the game.
Therefore, write a key-processing function that according to they key
pressed calls the appropriate auxiliary function. I am suggesting something
along the lines of:

; world key --> world
(define (process-key w k)
  (cond [(key=? k 'space) (try-to-eat-food w)]
           [(or (key=? k 'right) (key=? k 'left) (key=? k 'up) (key=? k
'down)) (alter-myself-xy-on-key w key)]
           [else w]))

I would also ask you:

(define (game-over? w)
  (cond
    [(overlapping? (world-myself w)
                   (shark-info-position (world-shark1 w)))
     true]
    [(overlapping? (world-myself w)
                   (shark-info-position (world-shark2 w)))
     true]
    [else false]))

Why do you use a cond? Consider simply using or.

Hope this helps!


On Thu, May 1, 2014 at 1:26 PM, Zee Ken <udaybhasker552009 at gmail.com> wrote:

> Hi,
>
> I am creating a game which runs in the below world.
>
> (define-struct world (myself monster1 monster2 food1 food2))
>
> The objective of the game is to move yourself in the world, not get hit by
> monsters and eat the food.
>
> I am done with
> 1) Keyboard manipulation of *myself*,
> 2) Stopping the game when I hit any of the monsters (monsters move
> randomly in the world).
>
> *Question1:* Placing the food is also an easy task. But, how do I modify
> the world when I eat the food?
>
> I am thinking of writing a procedure that checks if the user hit a SPACE
> button and is also near the food. If both of these are true, then that
> particular food image at the location should disappear and the game should
> continue.
>
> *Question2:* Also, are there any timer events in racket where I can
> include a stop clock counting down from a number to zero?
>
> I am a bit confused as to how I should tackle the idea.
>
>
> Best,
> Zeek
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>


-- 

Cheers,

Marco

Have a´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·´ * wonderful day! :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140502/c0b387ad/attachment.html>

Posted on the users mailing list.