Computer Science Canada

Check for collisions in new snake game

Author:  xHoly-Divinity [ Tue Jan 04, 2005 7:57 pm ]
Post subject:  Check for collisions in new snake game

I am not sure how to check if the line has collided with itself. I can do it with the wall and the ball, but not with the line itself. I have tried using Math.Distance, however, it does not help.

code:

drawbox (10, 10, maxx - 10, maxy - 10, black)
var x, y, xx, yy : int
x := 100
y := 100
xx := 200
yy := 100
Draw.ThickLine (x, y, xx, yy, 4, brightgreen)
var h, k : int
randint (h, 15, maxx-15)
randint (k, 15, maxy-15)
drawfilloval (h, k, 5, 5, brightred)
loop
    delay (10)
    var chars : array char of boolean
    var chars2 : array char of boolean
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        loop
            delay (10)
            xx := xx + 1
            Draw.ThickLine (x, y, xx, yy, 4, brightgreen)
            if xx > maxx - 12 then
                delay (200)
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx-15)
                randint (k, 15, maxy-15)
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_UP_ARROW) or chars2 (KEY_DOWN_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_UP_ARROW) then
        loop
            delay (10)
            x := xx
            yy := yy + 1
            y := yy
            Draw.ThickLine (x, y, xx, yy, 4, brightgreen)
            if yy > maxy - 12 then
                delay (200)
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx-15)
                randint (k, 15, maxy-15)
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_RIGHT_ARROW) or chars2 (KEY_LEFT_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_LEFT_ARROW) then
        loop
            delay (10)
            xx := xx - 1
            Draw.ThickLine (x, y, xx, yy, 4, brightgreen)
            if xx < 12 then
                delay (200)
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx-15)
                randint (k, 15, maxy-15)
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_UP_ARROW) or chars2 (KEY_DOWN_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_DOWN_ARROW) then
        loop
            delay (10)
            x := xx
            yy := yy - 1
            y := yy
            Draw.ThickLine (x, y, xx, yy, 4, brightgreen)
            if yy < 12 then
                delay (200)
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx-15)
                randint (k, 15, maxy-15)
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_RIGHT_ARROW) or chars2 (KEY_LEFT_ARROW) then
                exit
            end if
        end loop
    end if
end loop

Author:  Jonny Tight Lips [ Tue Jan 04, 2005 8:25 pm ]
Post subject: 

might I suggest using whatdotcolor. If the point is green then you know the lines have touched. You need the point being checked to always be right infront of the line. I think you can figure it out. Thats the general idea.

Author:  xHoly-Divinity [ Tue Jan 04, 2005 8:30 pm ]
Post subject: 

I've tried using that on a few occassions, however, none turned out how I wanted. Confused . I was just wondering if someone can send me an example using whatdotcolour?? Thank you

Author:  xHoly-Divinity [ Tue Jan 04, 2005 8:45 pm ]
Post subject: 

Never mind, i think i've got it Very Happy


: