Computer Science Canada Snake Wall Collision |
Author: | Mr. T [ 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 |
Author: | Naveg [ Fri Mar 18, 2005 10:47 pm ] |
Post subject: | |
Its because of the way youre increments work. The centre of the circle never actually touches the frame, just skips over it. |
Author: | Mr. T [ Fri Mar 18, 2005 10:51 pm ] |
Post subject: | |
how would I fix that problem? |
Author: | Token [ Fri Mar 18, 2005 10:53 pm ] |
Post 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 |
Author: | Mr. T [ Fri Mar 18, 2005 10:56 pm ] |
Post subject: | |
I dont understand how to work with radii, can u explain. with actual code |
Author: | Token [ Fri Mar 18, 2005 11:02 pm ] | ||
Post 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
|
Author: | Mr. T [ Fri Mar 18, 2005 11:06 pm ] |
Post 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 |
Author: | Token [ Fri Mar 18, 2005 11:16 pm ] | ||
Post 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
|
Author: | Mr. T [ Fri Mar 18, 2005 11:53 pm ] |
Post subject: | |
lol, thats very glitchy |
Author: | Cervantes [ Sat Mar 19, 2005 8:29 am ] | ||
Post 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.
|