

* Make the snake longer than just 1 segment!

* Add comments above each line explaining what it does in plain old english. this is a one line comment, here we're creating an array Here's what comments look like in JavaScript: This is called a comment, and it is only intended for humans to read, the computer knows to ignore them.
#JAVA SCRIPT SCREEN SNAKE CODE#
Sometimes it's nice to leave a clarifying note to future readers of the code (this could very well be you!) in plain English. We'll explore functions and arguments in future lessons. We do this because our CHUNK engine is looking for a list (an array) or objects to draw.įinally, we call the CHUNK library's built-in function 'draw' and pass it the argument 'drawableObjects' which was declared on the previous line. The third line declares a variable 'drawableObjects', which we assign the value of an array whose only data is the 'drawableSnake' declared on the previous line. 'snake' is the variable we defined on the previous line, which means 'drawableSnake.pixels' will return us the object we created on the first line. The object has two pieces, the first has the key 'color' and the value '"green"', and the second has the key 'pixels' which has the value 'snake'. On the second line, we declare the variable 'drawableSnake' and assign an object to its value. So in our example, if we need to know what the current 'left' value of snake is, we type 'snake.left' which will return a value of '0'. The key is used to get the data from the object. In our code, we have two pieces of data in the object, a 'top' value and a 'left' value. These curly-braces represent another Javascript data type called an Object which is also referred to as a 'hash map'.ĭata stored in an hash map object involves two parts, a key and a value. Within that array is one item which is defined by a set of curly-braces. The first line declares the variable 'snake' and assigns an array to it. Source_code :javascript, Now that we have this amazing snake we want to show it off, right? TheĬHUNK game engine makes it easy to draw objects on the screen. Add the following line to the beginning of game/snake.js:" Message "To get started, we want to create a variable that represents the snake. Goal "How to use a third party library as an 'engine' to power your game"
