Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help in Snake thing...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lebigcheese




PostPosted: Sat Jan 08, 2005 12:24 am   Post subject: Help in Snake thing...

Hi...Please help me with my snake game....i checked the already posted snake game by LiquidDragon. However, I hav NO idea what he was doing with his code. Anyways....i got the snake moving and eating its food, but i cant seem to make the snake build up everytime it eats something...please help as soon as possible...

Here's my code:
code:

setscreen ("offscreenonly,title:Snake,graphics:300;300")

var lengtx : int := 0
var lengty : int := 0
var x, y : int := 20
var x2 : int := x
var y2 : int := y

proc snake (x2, y2, clr : int)
    drawfillbox (x, y, x2 + 9, y2 + 9, clr)
end snake

var key : array char of boolean
var move : int := 0
var dir : string := "up"

proc maze2
    drawline (9, 9, 9, 290, 7)
    drawline (9, 9, 290, 9, 7)
    drawline (290, 9, 290, 290, 7)
    drawline (9, 290, 290, 290, 7)
end maze2

maze2

var foodx, foody, food : int := 0

proc up
    y += 10
    dir := "up"
end up

proc down
    y -= 10
    dir := "down"
end down

proc left
    x -= 10
    dir := "left"
end left

proc right
    x += 10
    dir := "right"
end right

loop
    if food = 0 then
        loop
            randint (foodx, 20, 270)
            randint (foody, 20, 270)
            if intstr (foodx) (*) = "0" then
                if intstr (foody) (*) = "0" then
                    if whatdotcolor (foodx, foody) = 0 then
                        if x + 10 <= foodx and x >= foodx and y + 10 <= foody and y + 10 >= foody then
                            drawfillbox (foodx, foody, foodx + 9, foody + 9, 0)
                            exit
                        else
                            drawfillbox (foodx, foody, foodx + 9, foody + 9, 7)
                            exit
                        end if
                    end if
                end if
            end if
        end loop
        food := 1
    end if

    snake (x, y, 0)

    Input.KeyDown (key)

    if key (KEY_UP_ARROW) then  %up arrow
        if dir not= "down" then
            up
        else
            down
        end if
    elsif key (KEY_DOWN_ARROW) then          %down
        if dir not= "up" then
            down
        else
            up
        end if
    elsif key (KEY_LEFT_ARROW) then                  %left
        if dir not= "right" then
            left
        else
            right
        end if
    elsif key (KEY_RIGHT_ARROW) then          %right
        if dir not= "left" then
            right
        else
            left
        end if
    else
        if dir = "up" then
            y += 10
        elsif dir = "down" then
            y -= 10
        elsif dir = "left" then
            x -= 10
        elsif dir = "right" then
            x += 10
        end if
    end if

    exit when x <= 5| x >= 290| y <= 5| y >= 290

    snake (x, y, 1)
    View.Update

    if whatdotcolor (foodx, foody) = 1 then
        food := 0
    end if

    delay (30)

end loop

maze2
Sponsor
Sponsor
Sponsor
sponsor
SuperGenius




PostPosted: Sat Jan 08, 2005 9:35 am   Post subject: (No subject)

i would suggest that you use flexible arrays to store the coords of each piece of the snake's body. when it eats some food or whatever increase the size of the arrays by 1.

the thing about this is that it would be best to use a flexible 2-d array, but last time i checked turing does not support this.
Cervantes




PostPosted: Sat Jan 08, 2005 10:25 am   Post subject: (No subject)

Turing does support two-dimensional flexible arrays, though it is limited. You can only change the upper bounds of the FIRST portion of the array, not the second. You can read up oin Flexible Arrays here.
lebigcheese




PostPosted: Sat Jan 08, 2005 4:18 pm   Post subject: (No subject)

I can't use flexible arrays because this is a project and we didn't learn that in school yet, and teacher doesn't allow us to use things we didn't learn...we DID learn about arrays n 3d arrays but thats it...no flexible ones
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: