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

Username:   Password: 
 RegisterRegister   
 Snake Wall Collision
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: Fri Mar 18, 2005 10:18 pm   Post subject: Snake Wall Collision

I want my snake to exit when it collides with the black walls surrounding the screen. What am I doing wrong in my whatdotcolour collision detection? Please Explain. Thanks.
----------------------------------
%variable declaration
View.Set ("graphics:400;300")
var box1 : int := 0
var box2 := maxy
var box3 := maxx
var box4 := 0
var col : int := 31
var countdown : int := 6
var ballx1 : int := 200
var bally1 : int := 150
var ballsize : int := 5
var move, move2, move3, move4 := false
var key : array char of boolean
move3 := true %starting direction of snake

%gives time for user to get ready
loop
locate (9, 20)
put "Starting in...", countdown - 1
delay (300)
countdown := countdown - 1
cls
exit when countdown = 0
end loop

%ball movement
loop
drawbox (box1, box2, box3, box4, 7)
drawfilloval (ballx1, bally1, ballsize, ballsize, col)
if move then
bally1 += 5
elsif move2 then
bally1 -= 5
elsif move3 then
ballx1 += 5
elsif move4 then
ballx1 -= 5
end if

Input.KeyDown (key)
if key (KEY_UP_ARROW) then %if up arrow is pressed
move2 := false
move3 := false
move4 := false
move := true
elsif key (KEY_DOWN_ARROW) then %if down arrow is pressed
move := false
move3 := false
move4 := false
move2 := true
elsif key (KEY_RIGHT_ARROW) then %if right arrow is pressed
move := false
move2 := false
move4 := false
move3 := true
elsif key (KEY_LEFT_ARROW) then %if left arrow is pressed
move := false
move2 := false
move4 := true
move3 := false
end if
drawfilloval (ballx1, bally1, ballsize, ballsize, 12) %deletes the tail
delay (100)
if whatdotcolour (ballx1, bally1) = black then
exit
end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Naveg




PostPosted: Fri Mar 18, 2005 10:47 pm   Post subject: (No subject)

Its because of the way youre increments work. The centre of the circle never actually touches the frame, just skips over it.
Mr. T




PostPosted: Fri Mar 18, 2005 10:51 pm   Post subject: (No subject)

how would I fix that problem?
Token




PostPosted: Fri Mar 18, 2005 10:53 pm   Post subject: (No subject)

yah, the problem is that
if whatdotcolour (ballx1, bally1) = black then
dosent touch the color, its the center of the circle, you have to do

if whatdotcolour (ballx1, bally1) = black then
and then add/subtract the radius +1 to the axis that the snake is moving
Mr. T




PostPosted: Fri Mar 18, 2005 10:56 pm   Post subject: (No subject)

I dont understand how to work with radii, can u explain. with actual code
Token




PostPosted: Fri Mar 18, 2005 11:02 pm   Post subject: (No subject)

I had to change the way you drew the borded because it moved 5 pixels per move, look at the if statement at the bottom, thats the colision

code:

%variable declaration
View.Set ("graphics:400;300")
var box1 : int := 0
var box2 := maxy
var box3 := maxx
var box4 := 0
var col : int := 31
var countdown : int := 6
var ballx1 : int := 200
var bally1 : int := 150
var ballsize : int := 5
var move, move2, move3, move4 := false
var key : array char of boolean
move3 := true %starting direction of snake

%gives time for user to get ready
loop
    locate (9, 20)
    put "Starting in...", countdown - 1
    delay (300)
    countdown := countdown - 1
    cls
    exit when countdown = 0
end loop

%ball movement
    drawfillbox (0, 0, maxx, maxy, black)
    drawfillbox (5, 5, maxx - 5, maxy - 5, white)
loop
    drawfilloval (ballx1, bally1, ballsize, ballsize, col)
    if move then
        bally1 += 5
    elsif move2 then
        bally1 -= 5
    elsif move3 then
        ballx1 += 5
    elsif move4 then
        ballx1 -= 5
    end if

    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then %if up arrow is pressed
        move2 := false
        move3 := false
        move4 := false
        move := true
    elsif key (KEY_DOWN_ARROW) then %if down arrow is pressed
        move := false
        move3 := false
        move4 := false
        move2 := true
    elsif key (KEY_RIGHT_ARROW) then %if right arrow is pressed
        move := false
        move2 := false
        move4 := false
        move3 := true
    elsif key (KEY_LEFT_ARROW) then %if left arrow is pressed
        move := false
        move2 := false
        move4 := true
        move3 := false
    end if
    drawfilloval (ballx1, bally1, ballsize, ballsize, 12) %deletes the tail
    delay (100)

    if move = true and whatdotcolour (ballx1, bally1 + 6) = black then
        exit
    elsif move2 = true and whatdotcolour (ballx1, bally1 - 6) = black then
        exit
    elsif move3 = true and whatdotcolour (ballx1 + 6, bally1) = black then
        exit
    elsif move4 = true and whatdotcolour (ballx1 - 6, bally1 + 6) = black then
        exit
    end if
end loop
Mr. T




PostPosted: Fri Mar 18, 2005 11:06 pm   Post subject: (No subject)

thnx for the help.
i decided to change how my game would work in order to avoid whatdotcolour.
originally i wanted my black border to shrink.
but now instead, ill juss adjust View.Set after every level.
thanks anyways
Token




PostPosted: Fri Mar 18, 2005 11:16 pm   Post subject: (No subject)

for games like this whatdotcolor is really effective, i wouldent avoid it if i were you, the shrinking of the border could be done really easily just by putting the drawbox that draws the border inside the loop and adding a counter to make it shrink, also you could have little dots randomly apearing and when u hit one it makes the border open up more, so give whatdotcolor a shot because its the easiest approach i see

check this out

code:
%variable declaration
View.Set ("graphics:400;300,offscreeononly")
var box1 : int := 0
var box2 := maxy
var box3 := maxx
var box4 := 0
var col : int := 31
var countdown : int := 6
var ballx1 : int := 200
var bally1 : int := 150
var ballsize : int := 5
var move, move2, move3, move4 := false
var key : array char of boolean
var bordercount : real := 0
var randx, randy : int
move3 := true %starting direction of snake


process dots
    loop
        randx := Rand.Int (5 + round (bordercount), maxx - 5 - round (bordercount))
        randy := Rand.Int (5 + round (bordercount), maxy - 5 - round (bordercount))

        drawfilloval (randx, randy, 15, 15, brightred)
        View.Update
        delay (5000)
        cls
    end loop
end dots

%gives time for user to get ready
loop
    locate (9, 20)
    put "Starting in...", countdown - 1
    View.Update
    delay (300)
    countdown := countdown - 1
    cls
    exit when countdown = 0
end loop

%ball movement
drawfillbox (0, 0, maxx, maxy, black)
drawfillbox (5 + round (bordercount), 5 + round (bordercount), maxx - 5 - round (bordercount), maxy - 5 - round (bordercount), white)

fork dots
loop
    bordercount += .25
    drawbox (5 + round (bordercount), 5 + round (bordercount), maxx - 5 - round (bordercount), maxy - 5 - round (bordercount), black)

    drawfilloval (ballx1, bally1, ballsize, ballsize, col)
    View.Update
    if move then
        bally1 += 5
    elsif move2 then
        bally1 -= 5
    elsif move3 then
        ballx1 += 5
    elsif move4 then
        ballx1 -= 5
    end if

    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then %if up arrow is pressed
        move2 := false
        move3 := false
        move4 := false
        move := true
    elsif key (KEY_DOWN_ARROW) then %if down arrow is pressed
        move := false
        move3 := false
        move4 := false
        move2 := true
    elsif key (KEY_RIGHT_ARROW) then %if right arrow is pressed
        move := false
        move2 := false
        move4 := false
        move3 := true
    elsif key (KEY_LEFT_ARROW) then %if left arrow is pressed
        move := false
        move2 := false
        move4 := true
        move3 := false
    end if
    drawfilloval (ballx1, bally1, ballsize, ballsize, 12) %deletes the tail

    delay (50)

    if move = true and whatdotcolour (ballx1, bally1 + 6) = black then
        exit
    elsif move2 = true and whatdotcolour (ballx1, bally1 - 6) = black then
        exit
    elsif move3 = true and whatdotcolour (ballx1 + 6, bally1) = black then
        exit
    elsif move4 = true and whatdotcolour (ballx1 - 6, bally1 + 6) = black then
        exit
    end if





    if move = true and whatdotcolour (ballx1, bally1 + 6) = brightred then
        cls
        bordercount -= 20
    elsif move2 = true and whatdotcolour (ballx1, bally1 - 6) = brightred then
        cls
        bordercount -= 20

    elsif move3 = true and whatdotcolour (ballx1 + 6, bally1) = brightred then
        cls
        bordercount -= 20
    elsif move4 = true and whatdotcolour (ballx1 - 6, bally1 + 6) = brightred then
        cls
        bordercount -= 20
    end if

end loop
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Fri Mar 18, 2005 11:53 pm   Post subject: (No subject)

lol, thats very glitchy
Cervantes




PostPosted: Sat Mar 19, 2005 8:29 am   Post subject: (No subject)

Yep. Token, you shouldn't be using a process for that. What's worse, because you're using a process, you've got TWO View.Updates. That's never good. It gives you some flickering (especially because you don't know when the View.Update in the process actually works).

I cleaned up the code. Namely, I took out the process, and eliminated whatdotcolour detection. Token, with you're approach to whatdotcolour detection, you've effectively got four points around the circle that you are checking for collisions. You're not checking anything to do with diagonals. See, if you use the Math.Distance approach for circular collisions, it's as though you've got an infinite amount of checking points around the player. I don't know if you want your snake to be circular, Pwned, but if you do, this is the way to go.I also changed the variables around so that the player and the clear ball (the big red one) are in record format. Lastly, I changed around the loop structure such that all input and input analyzing is grouped together, all drawing is grouped together, and all the other, necessary stuff (ie. collision detection) is grouped together.
Turing:

%variable declaration
View.Set ("graphics:400;300,offscreenonly")
colourback (black)

var key : array char of boolean
var bordercount : real := 0


var player :
    record
        x, y : real
        DIR : string
        radius : int
        speed : int  %number of pixels the player moves each time through the loop
    end record
player.x := maxx div 2
player.y := maxy div 2
player.DIR := "U"
player.radius := 9
player.speed := 5

var clearBall :
    record
        x, y : real
        radius : int
    end record
clearBall.x := -100
clearBall.y := -100       %offscreen
clearBall.radius := 15


var timeLast := 0 %time that the last big red circle was created.
var timeDelay := 5000 %delay in producing the red circles.  Measured in milliseconds
loop

    exit when player.x - player.radius < bordercount or player.x + player.radius > maxx - bordercount or player.y - player.radius < bordercount or player.y + player.radius > maxy - bordercount

    if Math.Distance (player.x, player.y, clearBall.x, clearBall.y) < player.radius + clearBall.radius then %collide with clearBAll
        bordercount -= 20
        clearBall.x := -100 %move it offscreen
        clearBall.y := -100
    end if

    if Time.Elapsed > timeLast + timeDelay then
        timeLast := Time.Elapsed
        clearBall.x := Rand.Int (5 + round (bordercount), maxx - 5 - round (bordercount))
        clearBall.y := Rand.Int (5 + round (bordercount), maxy - 5 - round (bordercount))
    end if

    bordercount += .25

    %---- input and movement
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then %if up arrow is pressed
        player.DIR := "U"
    elsif key (KEY_DOWN_ARROW) then %if down arrow is pressed
        player.DIR := "D"
    elsif key (KEY_RIGHT_ARROW) then %if right arrow is pressed
        player.DIR := "R"
    elsif key (KEY_LEFT_ARROW) then %if left arrow is pressed
        player.DIR := "L"
    end if

    case player.DIR of
        label "U" :
            player.y += player.speed
        label "D" :
            player.y -= player.speed
        label "L" :
            player.x -= player.speed
        label "R" :
            player.x += player.speed
        label :
    end case

    %drawing
    cls
    Draw.FillBox (round (bordercount), round (bordercount), round (maxx - bordercount), round (maxy - bordercount), white)
    drawfilloval (round (clearBall.x), round (clearBall.y), 15, 15, brightred)     %the big red circle
    drawfilloval (round (player.x), round (player.y), player.radius, player.radius, 12)     %player
    View.Update
    delay (50)
end loop
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  [ 10 Posts ]
Jump to:   


Style:  
Search: