Computer Science Canada

Snake game - How can i make the snake grow?

Author:  nivlack [ Fri Jun 05, 2009 7:11 pm ]
Post subject:  Snake game - How can i make the snake grow?

Hi I'm relatively new here, and like many other people, I have an ISU due in about a week.

Im making a snake game, but my only problem is I JUST CANT MAKE MY SNAKE GROW.

Here is my code so far...

Turing:


setscreen ("nobuttonbar")
var chars : array char of boolean
var x, y : int := 10
var direction : string := "up"
var change : int := 0
var foodx : int := 325
var foody : int := 205
var count : int := 0
var lvl, speed : int
put "Enter the level of difficulty"
get lvl
if lvl = 1 then
    speed := 90
elsif lvl = 2 then
    speed := 60
elsif lvl = 3 then
    speed := 30
end if
setscreen ("graphics,offscreenonly,nobuttonbar")
loop
    drawfillbox (x, y, x + 10, y + 10, black)
    View.Update
    cls
    exit when hasch
end loop
loop
    locate (1, 40)
    put "Score ", count
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and not direction = "down" then
        direction := "up"
    elsif chars (KEY_DOWN_ARROW) and not direction = "up" then
        direction := "down"
    elsif chars (KEY_RIGHT_ARROW) and not direction = "left" then
        direction := "right"
    elsif chars (KEY_LEFT_ARROW) and not direction = "right" then
        direction := "left"
    end if
    if x = 0 or x = maxx + 1 then
        exit
    end if
    if y = 0 or y = maxy + 1 then
        exit
    end if
    if direction = "up" then
        y := y + 10
    elsif direction = "down" then
        y := y - 10
    elsif direction = "right" then
        x := x + 10
    elsif direction = "left" then
        x := x - 10
    end if
    drawfilloval (foodx, foody, 5, 5, brightred)
    drawfillbox (x, y, x + 10, y + 10, black)
    if whatdotcolor (foodx, foody) not= brightred then
        count := count + 10 * (lvl)
        randint (foodx, 1, maxx)
        randint (foody, 1, maxy)
    end if
    delay (speed)
    View.Update
    cls
end loop
put "Game Over"



From searching through the forum, someone said I should use an array to do it, but i've read the tutorial on how use arrays, but I still have no clue what to do Sad
Forgive me if my coding seems very primitive, and please help me asap cuz i dont wanna fail Laughing

Author:  Kharybdis [ Fri Jun 05, 2009 9:00 pm ]
Post subject:  RE:Snake game - How can i make the snake grow?

well, you should have either a linked list or an array.

i'm assuming you'll be using an array. well, everytime you get whatever it is your snake is getting, assign the next part of your array (your array starts at 1) to be "filled" with a picture/ w/e that represents part of your snake. You have to set an array of predetermined size, say 100 for the maximum size of the snake.

Author:  Siavash [ Fri Jun 05, 2009 10:10 pm ]
Post subject:  RE:Snake game - How can i make the snake grow?

actually you could just use a flexible array.
var mycoolarray:flexible array 1..0

Author:  tjmoore1993 [ Fri Jun 05, 2009 11:15 pm ]
Post subject:  Re: RE:Snake game - How can i make the snake grow?

Turing:
var NAME : flexible array 1 .. 0 of string
loop
    cls
    new NAME, upper (NAME) + 1
    get NAME (upper (NAME))
    put upper (NAME), ": ", NAME (upper (NAME))
end loop


Flexible arrays are very useful in your situation.

Author:  nivlack [ Sat Jun 06, 2009 7:49 am ]
Post subject:  RE:Snake game - How can i make the snake grow?

thanks for the fast replies guys!

ok, I guess my weekend task is to learn how to put flexible arrays into my game.


: