Reputation: 12048
I was watching the ThinkVitamin screencasts about building a canvas game, but it seemed the last part was missing and I had to figure out a lot of stuff myself.
I succeeded in building the game, however there are two bugs in there that keep popping up.
EDIT: MOVED THE FIRST BUG INTO ANOTHER QUESTION since this one kinda got answered!
Another bug is that the food sometimes doesn't appear. I'm clueless as to why this happens, but the only thing I can come up with is the food actually appearing inside the snake. However, I have implemented a check for this aswell and it still happens:
inSnake = (x, y) ->
for part in Snake.position # check if the food is being placed inside the snake
if x == part.x && y == part.y
true
false
placeFood = ->
x = Math.round Math.random() * MAX_X - 1
y = Math.round Math.random() * MAX_Y - 1
if inSnake x, y # if so, run placeFood() again
placeFood()
Food.position = { x: x, y: y }
The food should be placed randomly INSIDE my canvas (I specify MAX_X and MAX_Y), so I can only think of it being inside the snake.. I'm having trouble debugging this problem.
A demo of the application can be found here.
Thanks in advance!
Upvotes: 2
Views: 181
Reputation: 262794
inSnake = (x, y) ->
for part in Snake.position
if x == part.x && y == part.y
true
false
That looks like you want to say return true
. Otherwise it will always run through the whole loop and return false
at the end of the function.
Upvotes: 4