By Paul Farr
How the game works on the backend is simple. There are four methods with a small set of helper functions. A ‘setup’ method, ‘display’ method, and a ‘mainLoop’ method.
The setup method would be the contractor in OOP. It sets the default values, all of which are constants at the top of the program and starts the “setInterval()” function that runs the “mainloop” method every game cycle. The setup method also displays some simple instruction to the user about what buttons to press to start the game.
The display method is very straight forward. It draws the screen based on the gameObject. It starts by filling the background; drawing the hub, food, and the full body. When the game ends, it will still draw everything but, will add an end of the game overlay.
The “mainloop” method is called by the “setInterval()” function in from the “setup” function. This method is where all the logic of the program happens. It adds body parts to the snake when the player gets the food; it moves all the body parts forward and checks to see if the player ran into himself or off the map.
This was a fun project, and I also had fun playing it too.