
-----------------------------------
dervy
Tue May 20, 2008 12:39 pm

Snake Help!
-----------------------------------
I am making a snake game and i cant seem to get the tail to follow it properly. If you see the code and try it you'll see what i mean. Any suggestions? 


var x, y : array 1 .. 999 of int
var lastx, lasty:array 1 .. 999 of int
var dx, dy : int
var direction : int
var count : int := 1
var ch : string (1)
var folx, foly : int
x (1) := 100
y (1) := 100
View.Set ("offscreenonly")
var chars : array char of boolean
color (red)
put "Welcome to Mr. Line! Press a key."
getch (ch)
direction := 1
loop
    randint (dx, 0, maxx)
    randint (dy, 0, maxy)
    loop
        drawfilloval (x (1), y (1), 4, 4, blue)
        if count > 1 then
            for i : 2 .. count
                x (i) := x (i - 1) + folx
                y (i) := y (i - 1) + foly
                drawfilloval (x (i), y (i), 4, 4, blue)
            end for
        end if
        drawfilloval (dx, dy, 4, 4, red)
        exit when View.WhatDotColour (x (1), y (1) - 4) = red or View.WhatDotColour (x (1), y (1) + 4) = red or View.WhatDotColour (x (1) + 4, y (1)) = red or View.WhatDotColour (x (1) - 4, y (1)) =
            red
        View.Update
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) and direction not= 4 then
            direction := 2
            %y (1)+= 4
        end if
        if chars (KEY_LEFT_ARROW) and direction not= 3 then
            %x (1):= x(1) - 4
            direction := 1
        end if
        if chars (KEY_RIGHT_ARROW) and direction not= 1 then
            %x (1):= x (1)+ 4
            direction := 3
        end if
        if chars (KEY_DOWN_ARROW) and direction not= 2 then
            %y (1) := y (1)- 4
            direction := 4
        end if
        if direction = 1 and x (1) = 0 then
            y (1) := y (1) - 4
foly:=8
folx:=0
        else
            Music.Sound (1000, 50)
            Music.SoundOff
            x (1) := maxx div 2
            y (1) := maxy div 2
            count := 1
        end if
        delay (25) 
        cls
    end loop
    count += 1
end loop

-----------------------------------
minime
Tue Apr 28, 2009 7:58 pm

RE:Snake Help!
-----------------------------------
well i was bored and tried making it as well, and i got most of it, all except when the tail hits itself it ends the game, so just see what you can get from my code.

var balls, space : int
space := 10
balls := 1 * space
var sx : flexible array 1 .. balls of int
var sy : flexible array 1 .. balls of int
var sr : int
var x,y,r : int
var speedy : array 1 .. balls of int
var speedx : array 1 .. balls of int
var keys : array char of boolean
var score : int

View.Set ("graphics:600;600,offscreenonly") 

sx (1) := 300
sy (1) := 300
sr := 10
r := 7
speedy (1) := 0
speedx (1) := 0
score := 0

randint (x, 0 + r, maxx - r) 
randint (y, 0 + r, maxy - r)

for i : 1 .. balls - 1
    speedx (i + 1) := speedx (i)
    speedy (i + 1) := speedy (i)
end for

for i : 2 .. balls
    sx (i) := sx (i - 1) - speedx (i - 1)
    sy (i) := sy (i - 1) - speedy (i - 1)
end for

loop

    cls
    put score

    if sx (1) + sr >= x - r and sx (1) - sr = y - r and sy (1) - sr 