How freeCodeCamp’s Basic JavaScript Lessons can be Applied on the Snakes Game.

How freeCodeCamp’s Basic JavaScript Lessons can be Applied on the Snakes Game.

·

3 min read

nokia-6110.jpg Image by Matteo. M

Does the image above look familiar? I am a 90's girl, so it is one I cannot miss. This is the very popular Snakes game that came with my first Nokia phone.

The Snakes game starts off as a single square block moving within a perimeter. Circular pellets (one at a time) are shown in random places and the snake has to eat them to grow longer and get a score usually 1.

The more pellets the snake eats, the longer it becomes. After eating some circular pellets, a bigger pellet which gives a higher score: 10. Unfortunately, this bigger pellet is timed and the snake must eat this within the time frame or miss out on getting the score.

When the head of the snake touches any part of its body, GAME OVER!.

How does this popular game relate to lessons in freeCodeCamp’s Basic JavaScript course. I will write out my pseudocode for the game while including the various lessons that relate to my points:

  1. Create an enclosure within which the snake can move about
  2. Using the length of the array + Math.floor() + Math.random(), select a location within the enclosure where the snake will first show up. Generate Random Whole Numbers with JavaScript
  3. Using the a similar script as 2, show a circular pellet on the enclosure WHILE it is not in the same location as the snake Generate Random Whole Numbers with JavaScript and Iterate with JavaScript While Loops
  4. When a snake moves and eats a pellet, increase its score by 1 and add a single square to the end of the snake so it increases in length Increment a Number with JavaScript and Manipulate Arrays With push()
  5. When a bigger circular pellet is eaten, increase the score by 10. Compound Assignment With Augmented Addition
  6. Remember to show another pellet in a different location (preferably) after it has been eaten using the length of array + Math.floor() + Math.random() combination Generate Random Whole Numbers with JavaScript

The above may sound very basic, there are other things to make it full but my point here is to see how applicable the lessons learnt are used in an everyday app very common to us.