Kontra.js is a mini JS game engine. Good to implement ideas. This program tries to implement snake game's movement logic.
Just clone this directory and serve it via server, no installation required
- Snake is a grid based movement i.e. if you move in slow motion it will move in box, This is evident when you take a turn. The whole first block will move then body will follow. Implmenet a virtual grid of XX boxes, your snake body should be XX as well for easy understanding
- Once game starts , check for direction in which snake needs to move, mark the movement in that direction, ( + for right and up, - for down and left etc. you can choose what is easy to understand)
- In the direction, move the tail infront of its head,
- Move position of head to 2, 2 to 3 etc. that way whole body will move back. I implemented this logic using different colors, this way you can see colors cycling back and moving in front.
- So grid by grid our snake will move in correct direction
have tried to add in comments, feel free to fork and play around.
📫 Feel free to drop me a line twitter @blackarck, blackarck@gmail.com ✉️ [github][https://github.com/blackarck]