Help

Step

To make the monkey "step" a certain distance we have to write "step X", X representing the number of steps we want him to take, for example "step 10".
"step" is a function: a set of instructions that performs a specific task. "step 20" is a statement that is composed of the function's name (step) and an argument. The argument for step is the number representing the amount of steps we want the monkey to take. For example, in "step 20", the argument is 20.
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "step" function work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.
Click on the "step" button to write the word "step" in your code.

Stepgif

Turn

"Turn" should be accompanied by a direction (left/right) or degrees (45, 90, 180).
Example: "turn right", "turn 90".
"turn" is a function: a set of instructions that performs a specific task. "turn left" or "turn 45" are statements that are composed of the function's name (turn) and an argument. The argument for turn is either a direction ("right" or "left") or a number representing degrees (45, 90, 180). For example, in "turn left", the argument is left.
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "turn" function work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.
Click on the "turn" button to write the word "turn" in your code.

Turnrightgif

Left / Right

"left" and "right" are used as arguments to the function "turn" to make the monkey turn in the desired direction.
Strictly speaking, left and right are constants representing the values 90 and -90 respectively. Hence, "turn left" is equivalent to to "turn 90", and "turn right" is equivalent to "turn -90".
Click on the "left" or "right" buttons to write the word "left" or "right" in your code respectively.

TurnTo

"turnTo" is another way of turning, instead of using direction or degrees, we are asking the monkey to turn to a specific object, for example: "turnTo banana".
When using "turnTo" the computer identifies that there is another object present, besides our beloved monkey, and by calling its name, it knows which way to turn.
Objects are everything in the scene we can interact with, like the bush, bridge, banana, and turtle.
"turnTo" is a function: a set of instructions that performs a specific task. "turnTo banana" is a statement that is composed of the function's name (turnTo) and an argument. The argument for turnTo is the object we want the monkey to turn to. For example, in "turnTo banana", the argument is banana.
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "turnTo" function work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.
Click on the "turnTo" button to write the word "turnTo" in your code.

Turntogif

Times Loop

A simple loop is a sequence of instructions that repeats a specified number of times. Here is an example:
Loopexample
In this example the monkey will repeat "step 5, turn left" 3 times. The instructions we write in the loop should be written underneath it with an indentation (shown on the screen as ....). You can do that by pressing the "tab" key on the keyboard.
Programming is not only about writing the correct statements in the right order; it is also about knowing how to write code that is clear, short and maintainable. So instead of writing this-
step 10
turn left
step 10
turn left
step 10
turn left
step 10
turn left

We can write this:
4.times->
    step 10
    turn left

Both will do the same; only the second piece of code is written as a loop. The meaning is that "step 10, turn left" would repeat four times, and then the loop would be over. Once the loop is over, the computer moves to the next statement.
Click the "times" button to insert a construct of a times loop in your code: "3.times - >"

Timesgif

Variables

Variable

Assignments to variables. A Variable is like a storage unit: we store data in it, and we use it only when we need it. An assignment to a variable is constructed out of an identifier, the equals sign = and a value.
X is the identifier; it can be any other letter or word. The identifier is the variable's name. When we want to use the value of the variable, we write its name, for example, "Step X". In this example, we want the monkey to step a distance equal to the value of the variable; in this case, 10
This separation of name and value allows the name to be used independently of the information it represents. We can use x when writing the program, without knowing what its value will be when the instructions will be carried out.
In CodeMonkey variables are used to call a function without a constant value. A function is a set of instructions that performs a specific task. Each function has a name and an argument. We can use a variable as the argument for step or turn, or even for the amount of times a times loop should run.
Another example of how we can use a variable as an argument is by using the two statements: "x = distanceTo banana", and "step x". In this example, the computer will measure the distance between the monkey and the banana and store it in the variable x. Then in the second statement it will replace the identifier X with the resulting number, which will serve as the argument for "step".
Click here to read more about distanceTo.

Variablesgif

Say

"say" will make a speech bubble to appear next to the monkey with the text we entered, for example:
Say1
will make the monkey say "Boo!".
we use quotes ("") around the phrase we want the monkey to say in order for the computer to understand that the text we entered isn't the name of a variable or function.
Try using say when there's a rat around.
Click on the "say" button to write the word "say" in your code.

Saygif

distanceTo

"distanceTo" is usually used with another statement like "step" or "say", and an object. Using "distanceTo" is like asking a question: "What is the distance to the banana?" the answer is a number, calculated by the computer, that represents the distance.
Example:
Distanceto1
When used, the computer will measure the distance between the monkey to the object (banana), then it will use the resulting number to carry out as instructed, using the measured value as the argument for "step". This will save us time and will also help if we don't know in advance the objects' locations.
Another way of using "distanceTo" is with a variable:
x = distanceTo banana
step x

In this example, the computer will measure the distance between the monkey and the banana, and will then replace the identifier X with the resulted number, which will serve as the argument for "Step".
"distanceTo" is a function: a set of instructions that performs a specific task. "distanceTo banana" is a statement that is composed of the function's name (distanceTo) and an argument. The argument for distanceTo is the the object we want to measure the distance to, like the banana or a bush. For example, in "step distanceTo banana", the argument for distanceTo is banana, and the resulting number will be the argument for step.
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "distanceTo" function work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.
Click on the "distanceTo" button to write the word "distanceTo" in your code.

Xdistancetogif

For loop

A for loop is used when we have a collection of objects and we want to repeat an action that relates to each one of them specifically. The for loop will keep going until all the actions were done on all the objects in our collection (array).
Forloopgif
The for loop relates to two objects: loop variable and array. The loop variable is a name we assign; it can be any name we want, like in variable assignments. It is common to name it after the first letter of the array. An array is a collection containing objects. For example, we can have an array containing six bananas. Each banana gets its own name, such as bananas[0] and all the bananas together have one name, which is the name of the array, such as bananas.
When the computer executes a for loop, it replaces the variable name with the first item in the collection. After it's done with the first item, it moves on to the second, and so on. The loop will stop only after all actions were done on all the items in the collection.
For example, we have this "for" loop:
for b in bananas
....turnTo b
....step distanceTo b

The first time this loop will run, the computer will "read" it like this:
....turnTo banana[0]
....step distanceTo banana[0]

Next, it would start over with the next banana:
....turnTo banana[1]
....step distanceTo banana[1]

and so on, until all bananas are caught.

The difference from the times.loop
A times.loop repeats the statements a specified number of times, carrying out the exact same action each time.
The for loop, however, matches the number of items in a collection, and will stop only after all actions were done on all the items in the collection.

How to read a for loop
for (this tells the computer that there is a "for" loop here)
b (the loop variable)
in (part of the "for" loop)
bananas (the name of the array)
....turnTo b (b refers to an object in the array)
....step distanceTo b (b refers to an object in the array)
Instead of writing the full name of the collection next to every function, we must use the name of the loop variable.

Nested loops
Double_forloop
We can also use a for loop inside of a for loop. The example above is taken from challenge #70.
A nested loop is a loop inside of a loop. When using a nested for loop, the statements inside the inner loop are executed repeatedly. For every element of the outer loop, the inner loop is executed from start to end, and then that whole process repeats for the next element in the outer loop, and so on
Click on the "for" button to write the following text in your code. Note the comment line:
Forcode

Grab()/Drop()

"grab()" and "drop()" are functions without an argument that are used in chapter 3 to help the rat collect matches.
A function is a set of instructions that performs a specific task.
A Function without an argument performs some action, but do not require us to pass any input, meaning we don't need to write a number or a name of an object after the function name. Instead, we put empty parentheses ().
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "grab" and "drop" functions work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.
Click on the grab or drop buttons to write the words "grab()" or "drop()" in your code, respectively.

Grabdropgif

Comment


Comment

This is a comment line, it is marked in the code with the symbol # at the beginning. The computer doesn't treat this line as an instruction. Rather, it is used by the programmers who write and read the code in order to understand each other. In CodeMonkey comment lines are used to give hints, instructions and guidance.

Function

A function is a set of instructions that performs a specific task. The computer will only execute the function when we will call it, meaning use it in our code.
Functions that take an argument are written like this:
function_name = (argument) ->
Underneath we will write the statements we want the computer to perform if and when we call this function.
The is an example for defining the function "goto", this function will make the monkey go towards an object:
Gotofunction
The name of the function is used for identification. Programmers give the function a name that represents what it does, which helps make the code readable.
When we call a function in our code, we need to pair the name of the function with the name of the object we want the actions inside of the function to be performed on, for example "goto banana" will make the monkey go towards the banana. Once we called the function with the argument banana, the computer replaced the argument "t" with "banana".
Functiongif
In CodeMonkey, once we defined a new function, a new empty button will appear with the name of the new function.
Click on the "function" button to write the following text in your code. Note the comment line:
function_name = (argument) ->
....# Your code here

Until loop

An until loop contains a block of code that will continue to run until a specific condition is met, this condition is called a loop condition. The computer checks the condition at the beginning, if the answer is false, the loop will keep going. It will only stop once the answer is true.
If we don't pay attention, we might give the "until" loop a condition that will not be met. This will cause the loop to keep going forever and may even cause the program to crash.
Untilgif
In CodeMonkey we use two loop conditions: near, and cat.sleeping.
Here is an example of an until loop with a "near" condition:
Untilnear
Click here to read more about the near condition.

Here is an example of an until loop with a "cat.sleeping" condition:
Untilcat
Click here to read more about the cat.sleeping condition.

What is the difference between a for loop and an until loop?
A for loop performs a set of actions on all objects in a collection, and then stops.
An until loop will keep going until a certain condition is met, and if we are not careful, can go on forever.

Click on the "until" button to write the following text in your code. Note the comment line:
until condition
....# Your code here

Near

Near is a function, typically used as a loop condition for an until loop. The value returned by the function "near" will determine when the until loop will stop executing.
Untilnear
"near" is a function: a set of instructions that performs a specific task. "until near match" is a statement that is composed of the until keyword, a function's name (near) and an argument. The argument for near is the object we want the monkey or the rat to be near to. For example, in "until near match", the argument is match.
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "near" function work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.
Click on the "near" button to write the word "near" in your code.

Untilgif

Sleeping/wait()

In chapter 3 we are introduced to the cat, the cat will attack the rat if he sees him, so we have to wait for him to fall asleep. We do that by writing "cat.sleeping()", we have to use "cat" and a dot (.) so the computer will know that we are waiting for the cat to fall asleep.
Untilcatgif
"sleeping" is a function without an argument, which we can use as a loop condition.
A function is a set of instructions that performs a specific task.
A Function without an argument performs some action, but do not require us to pass any input, meaning we don't need to call an object or give a number along with the function. Instead, we put empty parenthesis.
In this case, the programmer who designed CodeMonkey saved us some work and already defined the instructions that make the "sleeping" function work. In programming, it is relatively common to use functions that someone else defined.
Click here to read more about defining functions.

wait() is also a function without an argument. It will cause the monkey to wait for 1 second and then continue to the next instruction. In a loop, the next instruction would be to check if the loop condition is true. According to the loop condition, the computer will exit the loop, or wait again, and so on. Example:
Untilcat

Still need help? Contact us

© Codemonkey Studios 2017. All rights reserved.
Play Now!