
-----------------------------------
cpu_hacker4.0
Wed Oct 17, 2007 5:57 pm

I need help with snake game
-----------------------------------
Ok I am making a snake game for turing and this is what I have so far.  Basically, you control one ball with the arrow keys and have to "eat" other circles that get added on to the tail.  I'm trying to use collision detection (to "eat" other balls), but it doesn't seem to be working.  I put the code that's causing problems in green, just so you can see what I am trying to do.  Fiirst run it as is, then remove the /* and */ to see what the problem is.  Help would be greatly appreciated.

const RADIUS : int := 10

var chars : array char of boolean
var num : int := 1
var x, y : array 1 .. num of int
var xInc, yInc : int := 0
var rx, ry : int

proc RandBall
    randint (rx, 0 + RADIUS, maxx - RADIUS)
    randint (ry, 0 + RADIUS, maxy - RADIUS)
end RandBall

for a : 1 .. num
    x (a) := maxx div 2
    y (a) := maxy div 2
end for

View.Set ("Offscreenonly")
RandBall
loop
    for a : 1 .. num
        x (a) := x (a) + xInc
        y (a) := y (a) + yInc
        drawfilloval (x (a), y (a), RADIUS, RADIUS, black)
        drawfilloval (rx, ry, RADIUS, RADIUS, black)
        View.Update
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            xInc := 0
            yInc := 1
        elsif chars (KEY_DOWN_ARROW) then
            xInc := 0
            yInc := -1
        elsif chars (KEY_RIGHT_ARROW) then
            xInc := 1
            yInc := 0
        elsif chars (KEY_LEFT_ARROW) then
            xInc := -1
            yInc := 0
        end if
    end for
    /*if x(a) > rx - 10 or x(a) < rx + 10 and y(a) > ry - 10 or y(a) < ry + 10 then
        RandBall
     end if*/
    cls
end loop


-----------------------------------
HeavenAgain
Wed Oct 17, 2007 6:26 pm

Re: I need help with snake game
-----------------------------------
the commented part is not in your for loop  :shock: 
that's the "problem", and change the conditions to = rather than bigger or less, although that doesnt fix the dectection.
try searching through the turing forum, im sure there are a lot of snake games example >>snake game rx - 15 and x (a) < rx + 15 and y (a) > ry - 15 and y (a) < ry + 15 then
            RandBall
        end if
    end for 
    cls
end loop

-----------------------------------
Nick
Thu Oct 18, 2007 6:56 am

RE:I need help with snake game
-----------------------------------
just make the x and y = the old x and y of ur snake and have each attaching part = the old x and y of the precedding part so:

old x:=x
oldy:=y
x+=5
y+=0
%snake is moving right
oldballx(1):=ballx(1)
oldbally(1):=bally(1)
ballx(1):=oldx
bally(1):=oldy
ballx(2):=oldballx(1)
bally(2):=oldbally(1)


i havent actually tried this nor made a snake game but i did make a trailing string bounce program ill post so u can see how this works

var word : array 1 .. 5 of string
put "Enter the word or sentence to bounce:"
get word (1) : *
var row, col : array 1 .. 5 of int
var len : int := length (word (1))
var a, b : int := 1
var timer : int
put "how fast (1,2 or 3)"
get timer
if timer = 1 then
    timer := 10
elsif timer = 2 then
    timer := 30
elsif timer = 3 then
    timer := 50
else
    timer := 1000
end if

for i : 1 .. 5
    word (i) := word (1)
    row (i) := 10
    col (i) := 10
end for
cls
loop
    locate (row (1), col (1))
    color (red)
    put word (1) ..
    locate (row (2), col (2))
    color (green)
    put word (2) ..
    locate (row (3), col (3))
    color (brown)
    put word (3) ..
    locate (row (4), col (4))
    color (black)
    put word (4) ..
    locate (row (5), col (5))
    color (blue)
    put word (5) ..
    col (5) := col (4)
    col (4) := col (3)
    col (3) := col (2)
    col (2) := col (1)
    col (1) := col (1) + a
    row (5) := row (4)
    row (4) := row (3)
    row (3) := row (2)
    row (2) := row (1)
    row (1) := row (1) + b
    if row (1) >= 25 then
        b := -1
    end if
    if col (1) >= 80 - len then
        a := -1
    end if
    if row (1)  0 then
            square (nodes (x).x, nodes (x).y) := 0
            nodes (x).x := nodes (x - 1).x
            nodes (x).y := nodes (x - 1).y
        end if
    end for
    if nodes (0).x + xInc > 0 and nodes (0).x + xInc < 31 then
        nodes (0).x += xInc
    end if
    if nodes (0).y + yInc > 0 and nodes (0).y + yInc < 31 then
        nodes (0).y += yInc
    end if
    if square (nodes (0).x, nodes (0).y) = 2 then
        addNode
    end if
    for x : 0 .. upper (nodes)
        square (nodes (x).x, nodes (x).y) := 1
    end for
    reDraw
    delay (100)
end loop

