admin管理员组文章数量:1429067
I'm implementing a Snake game in javascript for fun, I have successfully implemented the snake, its movements and the snake-growth thing as it eats an apple.
To calculate the apple position I'm currently following these steps:
- create a new apple object
- create random coordinates (X and Y, between game-container boundaries) for the apple
- check if the coordinates of the apple are equal to one of the snake-blocks coordinates
- if step #3 is TRUE, recalculate the position of the apple, else draw the apple in the game-container
Unfortunately I found out that this algorithm is very weak.. let's say I have a 10 x 10 game container, the red square is the apple, the green square is my snake head (initial game state)
as the game progresses the snake eats more and more apples, increasing its length and leaving less and less empty cells to place an apple
Now suppose that the snake reaches a length equals to 99 while eating an apple. This means that there's only one square left to place the next apple. My algorithm (this is the worst case obviously) could take forever in order to randomize the correct value, as it discards any randomized position that it's already taken by the snake's head or tail, not caring at all to randomize the new position in a range of "empty cells" only but instead randomizing on the whole 10 x 10 game canvas.
How should I proceed to solve my busillis? Can you give me any advice on a good algorithm that I can use?
Thank you
I'm implementing a Snake game in javascript for fun, I have successfully implemented the snake, its movements and the snake-growth thing as it eats an apple.
To calculate the apple position I'm currently following these steps:
- create a new apple object
- create random coordinates (X and Y, between game-container boundaries) for the apple
- check if the coordinates of the apple are equal to one of the snake-blocks coordinates
- if step #3 is TRUE, recalculate the position of the apple, else draw the apple in the game-container
Unfortunately I found out that this algorithm is very weak.. let's say I have a 10 x 10 game container, the red square is the apple, the green square is my snake head (initial game state)
as the game progresses the snake eats more and more apples, increasing its length and leaving less and less empty cells to place an apple
Now suppose that the snake reaches a length equals to 99 while eating an apple. This means that there's only one square left to place the next apple. My algorithm (this is the worst case obviously) could take forever in order to randomize the correct value, as it discards any randomized position that it's already taken by the snake's head or tail, not caring at all to randomize the new position in a range of "empty cells" only but instead randomizing on the whole 10 x 10 game canvas.
How should I proceed to solve my busillis? Can you give me any advice on a good algorithm that I can use?
Thank you
Share Improve this question asked Feb 23, 2015 at 23:11 BeNdErRBeNdErR 17.9k21 gold badges77 silver badges106 bronze badges 8- 4 The easiest solution I can think now is to make a list of free coordinates and choose randomly from them? – mkabanen Commented Feb 23, 2015 at 23:15
- @kabanen: In fact that's the best solution :-) – Bergi Commented Feb 23, 2015 at 23:17
- I think this is not optimal as I have to refresh this list every time the snake moves, removing the occupied cells (I have to scan the list in order to find the cell) and adding the freed ones. Am I wrong? – BeNdErR Commented Feb 23, 2015 at 23:17
- Do you add apples on every step? If not, then you can calculate free coordinates only if needed. – mkabanen Commented Feb 23, 2015 at 23:19
- 2 Even if it wasn't the best solution, it's still a good answer, and you should always feel free to add those. :) – Shomz Commented Feb 23, 2015 at 23:29
1 Answer
Reset to default 6As said in ments, the easiest solution I can think is to make a list of free coordinates and then just choose randomly from them. And you can calculate free coordinates only if needed(when you need to add an apple).
本文标签: javascriptSnake gamehow to calculate the next apple positionStack Overflow
版权声明:本文标题:javascript - Snake game - how to calculate the next apple position - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745421724a2657930.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论