gs:key-event

Started by electrifiedspam, November 13, 2010, 08:08:31 PM

Previous topic - Next topic

electrifiedspam

Could anyone post something or point me to an example where this is in use?



I am trying to make a simple snake game and I can do all of it except tell when a certain key is pressed.



Thank you in advance for your help.



Chris

cormullion

#1
Try here



http://www.nodep.nl/downloads/newlisp/worm.lsp">http://www.nodep.nl/downloads/newlisp/worm.lsp

Lutz

#2
unfortunately worm.lsp is broken in versions 10.0 and after but the following works:



http://www.newlisp.org/code/ybrick.lsp">http://www.newlisp.org/code/ybrick.lsp



with added UP/DOWN for speed change.

electrifiedspam

#3
Thank you all. I have made it this far.



Those examples helped my understand what was going on.

Thank you again.



Chris

----------------------------------------------------------------------------------------------------------------------

(set 'up 38)

(set 'down 40)

(set 'left 37)

(set 'right 39)

(set 'delay 20000)



(set-locale "C")

(load (append (env "NEWLISPDIR") "/guiserver.lsp"))

(gs:init)



(define (key-action id type code modifiers) (set 'KEY code) (println "key: " KEY))





;;Define the Gui

(gs:frame 'snakeGame 100 100 600 600 "New Snake Game")

(gs:canvas 'C)

(gs:set-background 'C gs:white)

(gs:fill-rect 'r 280 280 10 10 gs:red)

(gs:key-event 'C 'key-action)



(gs:add-to 'snakeGame 'C)

(gs:set-visible 'snakeGame true)



(while (gs:check-event delay)

;;required for key event

(gs:request-focus 'C)

;;Move Red Square

(cond ((= KEY up) (gs:move-tag 'r 0 -5) (set 'KEY 0))

        ((= KEY down) (gs:move-tag 'r 0 5 ) (set 'KEY 0))

        ((= KEY right) (gs:move-tag 'r 5 0) (set 'KEY 0))

        ((= KEY left) (gs:move-tag 'r -5 0) (set 'KEY 0))

)

(gs:update)

)

cormullion

#4
it works on my computer too! :)

electrifiedspam

#5
Is there a way in GS to get the color of a point on the canvas?



I was just going to check the colors in front of the head of the snake to know if there has been a collision.



Thank you.



Chris

cormullion

#6
I don't think you can read pixels from a canvas (or write to them either). You'd probably have to keep a record of which areas have been painted so that you can test against them later.

electrifiedspam

#7
Well, this is as far as I have gotten so far.



Problems:

1) Snake does not detect when it has hit itself.

2) 'Apples' can instantiate on the snake causing an effective end to game play.



I just thought that you all would like to see how far I have come with your help.



Thank you

Chris

------------------------------------------------------------------------------------------------------------------------

(set 'up 38)

(set 'down 40)

(set 'left 37)

(set 'right 39)

(set 'delay 60000)

(set 'x 280 'y 280)

(set 'xBound 570 'yBound 550)

(set 'SNAKE (list (list x y)))

(set 'applState false)

(set 'SCORE 0)



(set-locale "C")

(load (append (env "NEWLISPDIR") "/guiserver.lsp"))

(gs:init)



(define (key-action id type code modifiers) (set 'DIRECTION (if (= code up) "UP" (= code down) "DOWN" (= code right) "RIGHT" (= code left) "LEFT")))



(define (apple) (set 'xCord (* 10 (rand 50)) 'yCord (* 10 (rand 50)))(gs:fill-rect 'a xCord yCord 10 10 gs:red) (setq applState true) (obstruction xCord yCord))

(define (obstruction xapp yapp) (set 'APPPOS (list (list xapp yapp))))

(define (tail xPos yPos) (gs:fill-rect 'r xpos ypos 5 5 gs:white))

(define (snake CORD)

            (setq SNAKE (append CORD SNAKE)))





;;Define the Gui

(gs:frame 'snakeGame 100 100 600 600 "New Snake Game")

(gs:canvas 'C)

(gs:set-background 'C gs:white)

(gs:fill-rect 'r x y 10 10 gs:green)

(println SNAKE)

(gs:key-event 'C 'key-action)



(gs:add-to 'snakeGame 'C)

(gs:set-visible 'snakeGame true)



(while (gs:check-event delay)

;;required for key event

(gs:request-focus 'C)



;;Move Red Square

(cond ((= DIRECTION "UP") (setq y (- y 10)) (snake (list (list x y))

(gs:fill-rect 'r x y 10 10 gs:green)))

        ((= DIRECTION "DOWN") (setq y (+ y 10)) (snake (list (list x y))

(gs:fill-rect 'r x y 10 10 gs:green)))

        ((= DIRECTION "RIGHT") (setq x (+ x 10)) (snake (list (list x y))

(gs:fill-rect 'r x y 10 10  gs:green)))

        ((= DIRECTION "LEFT") (setq x (- x 10)) (snake (list (list x y))

(gs:fill-rect 'r x y 10 10  gs:green)))

)

;;check border

(cond ((>= (first (first SNAKE)) 575) (setq DIRECTION "UP"))

         ((>= (last (first SNAKE)) 555) (setq DIRECTION "RIGHT"))

         ((= (first (first SNAKE)) 0) (setq DIRECTION "DOWN"))

         ((= (last (first SNAKE)) 0) (setq DIRECTION "LEFT"))

)

;;draw new apple

(if (= applState false) (apple))

;;colission detection

(cond

   ((= (first SNAKE) (first APPPOS)) (set 'applState false 'SCORE (inc SCORE))(println SCORE))

)

(if (index (fn (X) (= (first SNAKE) x)) (rest SNAKE)) (exit))

;;erase end of tail

(if (and (= applState true) (> (length SNAKE) 1)) ((gs:fill-rect 'r (first (last SNAKE)) (last (last SNAKE)) 10 10  gs:white) (setq SNAKE (chop SNAKE))))

(gs:update)

)

electrifiedspam

#8
I have found a fix for the aforementioned problems.



I won't post the code again until I have some major revisions.



Thank you for your help

Chris

Lutz

#9
Each time you call gs:fill-rect a new rectangle object is created in Guiserver and eventually you will run out of memory, if you play long enough ;-)



You animate by using gs:xxx-tag operations on shapes created with gs:fill-yyy or gs:draw-yyy. You could do a gs:delete-tag on the old tail piece, just painting it white will not delete it.