Computer Science Canada

Snake Help

Author:  upthescale [ Thu May 04, 2006 2:45 pm ]
Post subject:  Snake Help

You know when you hit a key and the snake wil move...well for me the snake only moves when i hit the key, how can i do it so when i key an arrow key, he will go...because he only goes when i hold down!

Author:  Clayton [ Thu May 04, 2006 3:03 pm ]
Post subject: 

think about it, if it is going to move continuously without input, what do you need? a loop. have an if condition then go into the loop. simple

Turing:

var keys:array char of boolean
loop
    Input.KeyDown(keys)
    if keys(%whatever arrow) then
        loop
            %make coordinate changes in the loop(continuous without input)
        end if
        exit when hasch
end loop

Author:  upthescale [ Thu May 04, 2006 3:05 pm ]
Post subject: 

my box doesn't even move now that it is in a loop!!!

Author:  upthescale [ Thu May 04, 2006 3:09 pm ]
Post subject: 

ok it werks,,, thanks man +5 bits!!!!

Author:  Clayton [ Thu May 04, 2006 3:14 pm ]
Post subject: 

Turing:

var keys : array char of boolean
View.Set ("graphics,offscreenonly")
var x, y : int := 0
const size : int := 20
y := maxy - size
loop
    Input.KeyDown (keys)
    if keys (KEY_DOWN_ARROW) then
        loop
            y -= 1
            Draw.Cls
            Draw.FillBox (x, y, x + size, y + size, black)
            View.Update
            delay (200)
            exit when hasch
        end loop
    end if
end loop

Author:  Delos [ Thu May 04, 2006 3:17 pm ]
Post subject: 

SuperFreak82 wrote:

Turing:

var keys:array char of boolean
loop
    Input.KeyDown(keys)
    if keys(%whatever arrow) then
        loop
            %make coordinate changes in the loop(continuous without input)
        end if
        exit when hasch
end loop


A nested loop eh? Not a bad idea, and functional. However, using this approach you'll be left with 4 instances of such loops within your main loop. This might just me being picky, but I would far prefer to use a vectors-and-components approch.
Simply break down your snake movement into its components (in this case very, very simple). If the left button is pressed, the movement components would become (-1, 0); this can then be used in the main loop to change the position of the snake.
And yes, you would end up with 4 instances of these assignments - but this just strikes me as a simpler route to creating the constant motion here.

Author:  Clayton [ Thu May 04, 2006 3:41 pm ]
Post subject: 

i was thinking more of a procedural approach where you could get the input and pass that to a procedure and run the loop, i just did that to show him the concept of what to do really


: