EasyCode Foundations Reference Card
|To make the monkey step to a certain distance, we have to write step X using the number of steps we want him to take, for example: step 10. Pressing the step button will write the word step in your code.|
|Pressing the run button will make the code on the right run. You can see the outcome by looking at the scene on the left.
|The reset button will erase everything you wrote in the code on the right and will reset the code to how it was at the beginning of the challenge.
|The ruler is a tool to help you measure the distance between different objects in the game, such as the distance between the monkey and the banana. The ruler can also help you measure angles that the monkey or turtle has to turn in order to face another object on the screen, like a banana. To use the ruler, click it once and then use your mouse to move the ruler to the point you want it to start measuring from. Click the mouse again and then drag it to the endpoint. A number will appear at the endpoint to indicate the distance. Use this number with the step statement. Another number will appear near the starting point, this number indicates the angle from the first object to the second one. Use it with the turn statement.
|After each challenge, you’ll receive a star rating for your solution.
The stars are distributed as so:
• First star is given if you got all bananas
• Second star is given if you used what you learned
• Third star is given if your code is short and to the point
|turn should be accompanied by a direction (left/right) or degrees (45, 90, 180). Pressing the turn button will write the word turn in your code.
Examples: turn right, turn 90
|left and right are used after the statement turn to make the monkey turn in the desired direction. Pressing the left or right buttons will write the word left or right in your code accordingly.
|turnTo is another way of turning. Instead of using direction or degrees, we are asking the monkey to turn to a specific object. Pressing the turnTo button will write the word turnTo in your code. Example: turnTo banana
|A simple loop is a sequence of instructions that repeats a specified number of times.
Example: 3.times -> step 5
In this example, the monkey will repeat step 5 and turn left three times. The instructions we write in the loop should be written underneath it with an indentation. You can do that by pressing the Tab key on the keyboard. Pressing the times button will write the beginning of a simple loop in your code:
|x = 10|
|This is an assignment to a variable. 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 and a value. 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.|
|say will make a speech bubble appear next to the monkey with the text we entered. For example, say "Boo!" will make the monkey say “Boo!” We use quotes ("") around the phrase we want the monkey to say so that the computer understands that the text we entered isn’t a variable. Try using say when there is a rat around. Pressing the say button will write the word say in your code.
|distanceTo is used with another statement like step or say and an object. Using distanceTo is like asking a question, for example, “What is the distance to the banana?” The answer is a number calculated by the computer that represents the distance. Here is an example:
step distanceTo banana
The computer will measure the distance between the monkey and the object (banana). Then it will use the resulting number to carry out as instructed, using the measured value as the argument for step. Pressing the distanceTo button will write the word distanceTo in your code.
|This is a 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 are done on all the objects in our collection (array). When the computer executes this loop, it replaces the variable name with the first item in the collection. After it is done with the first item, it moves on to the second, and so on.
We can also use a for loop inside of a for loop; the example on the left is taken from challenge #70. Pressing the for button will write the following text in your code. Note the comment line:
for name in array
# Your code here
|grab() and drop() are functions without an argument that are used to help the rat collect matches. Functions without an argument perform some action but do not require us to pass any input. Pressing the grab or drop buttons will write the words grab() or drop() in your code, respectively.
|# This is a comment|
# It starts with a '#'
|This is a comment line. It is marked in the code with the pound symbol (#) at the beginning. The computer doesn’t treat this line as an instruction. Rather, it is used by programmers who write and read the code in order to understand each other.
|A function is a set of instructions that performs a specific task. The computer will only execute the function when we’ll call it, meaning use it in our code. This is an example of how we define a function:
goto = (t) -> turnTo t
step distanceTo t
Once we define a function, a new empty button will appear with the name of the function. When we want to call the function in the code, we pair the name of the function with the name of the object we want the actions inside of the function to be performed on:
Goto match grab()
goto pile drop()
Pressing the function button will write the following text in your code. Note the comment line:
functionName = (argument) ->
# Your code goes here
until near match
|The 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. Pressing the until button will write the following text in your code. Note the comment line:
# Your code here
|We can use the function near with a loop condition. The value returned by the function near will determine when the until loop will stop executing. Pressing the near button will write the word near in your code.|
|In The Sands of Until, the cat is introduced. The cat will attack the rat if he sees him, so we have to wait for him to fall asleep. sleeping() is another function we can use with a loop condition. wait() is a function without an argument. When using sleeping(), we need to write:
|The goto function makes the monkey turnTo an object and step distanceTo that certain object. Using the function goto will make your code shorter and more readable. Use it with any object on the screen as an argument, such as banana in goto banana and bridge in goto bridge. Pressing the goto button will write the word goto in your code.
|In challenge #101, we are introduced to the goat who will help us break the ice around frozen bananas. The function hit() will tell the goat to hit the frozen banana so the monkey can get it, but be careful not to use it on unfrozen bananas. To use hit() we have to write goat.hit(). Note that the two are separated by a dot (.). Pressing the hit button will write .hit() in your code.|
|if is used when we want our code to decide what to do based on a certain condition. The state might be unknown to us when we write our code and the computer will have to decide what to do while the code is executed. Pressing the if button will write the following in your code:
# Your code here
|frozen() is a function without an argument that is used to ask “is the banana frozen?” We use it by writing banana.frozen(). The function then returns a value that is yes or no according to the state of the object. Use frozen() as a condition for the if statement like so:
This tells the computer to do the things after it only if the banana is frozen. Pressing the frozen button will write .frozen() in your code.
|green() is a function without an argument that can be used as a condition to the if statement. This function will ask “is the banana green?”, then returns a value that is yes or no according to the state of the object. We use green() like so:
Pressing the green button will write .green() in your code.
|An if statement helps the computer decide what to do based on a certain condition. When we use an if-else statement, we are telling the computer what to do in case the condition is true (under the if), and what to do in case the condition wasn’t met, meaning it is false (under the else):
# code to be executed if true else
#code to be executed if false
The state might be unknown to us when we write our code, but we are telling the computer what to do in both cases, and it will decide what to do while the code is executed. Pressing the if-else button will write the following in your code:
# Your code here else
# Your code here
|In challenge #120 we are introduced to the tiger and the bear. They will attack our monkey if they see him, so we have to wait for them to fall asleep or be distracted by playing. playing() is another function we can use with a loop condition. When using playing(), we need to write:
until tiger.playing() wait()
Pressing the playing button will write .playing() in your code.
|and is a logical operator. We use it with an until loop to make a loop condition that is made of two conditions. That way, we can make the computer wait until both conditions are true before executing the code below. Pressing the and button will write and in your code.|
|or is a logical operator. We use it with an until loop to make a loop condition that is made of two conditions. That way we can make the computer wait until at least one of the conditions is true, before executing the code below. Pressing the or button will write or in your code.|
|not is a logical operator. When using the not operator we basically turn yes or no to its opposite. In other words, not turns yes to no and no to yes. By using not we can tell the monkey to go to a banana if it’s not rotten. It will look like this:
if not banana.rotten() goto banana
Pressing the not button will write the word not in your code.
|Some bananas are yellow and tasty, but some bananas are rotten and we don’t really want to eat them. rotten() is a function without an argument that returns yes if the banana is rotten, and no otherwise. This function can be used as a condition with an if statement. We can also use rotten() with not, to tell the monkey to go to a banana that is not rotten as follows:
if not b.rotten() goto b
Pressing the rotten button will write rotten() in your code.
|health() is a function without an argument. It is used starting from challenge #142 where the monkey battles the gorilla. Up to this point, we fail to complete a challenge if we don’t collect all the bananas. But starting from this point, we can also fail if our health runs out. The health() function is used with the operators == (equals to) and < (less than). We can also see the status of our health by looking at the upper right corner of the stage, where we’ll see a counter marked with a blue heart. Pressing the health button will write health() in your code.|
|The equality operator (==) is used to make a comparison between values, such as in an if statement.
For example: if a == 3
reads as: if a is equal to three.
Comparing values in CoffeeScript is used as follows:
if health() == 100
Pressing the equals button will add == to your code.
|Less than (<) is an operator used to compare numeric values such as distances or health values. We can use it to let the computer decide which healthZone to go to by letting him calculate which healthzone is closer. For example:
if d0 < d1
goto healthZones else
Pressing the less than button will add < to your code.
|return statements allow a function to specify a return value to be passed back to the code that called the function as follows:
yummy = (x) ->
return not x.rotten()
The function yummy asks the computer to return a value of yes or no for the question: “Is x not rotten?”. Pressing the return button will write the word return in your code.
|In challenge #160, we are introduced to the crows. They are sitting high up and watching us, and if we are not careful they will attack us. To get through them, we have to scare them away. We use the function watching() to make sure they indeed got scared and aren’t watching us anymore as follows:
until not crow.watching() say "Boo!"
Pressing the watching button will write watching() in our code.
|The onKey function is called each time we press on a key on the keyboard. The argument of this function is the key that we pressed on the keyboard. Pressing the onKey button will add the syntax of the onKey function to your code with comments as an example of how to use the argument in your code as follows:
onKey = (key) ->
#if key == 'w'
# step 1
|onMouseMove is called each time we move the mouse. The argument of this function is the position of the mouse (where the cursor is). The position of the mouse includes the x position (pos.x) and the y position (pos.y). Pressing the onMouseMove button will add the syntax of the onMouseMove function to your code with comments as an example of how to use the argument in the code as follows:
onMouseMove = (pos) ->
|The functions setX and setY set the x and y positions, respectively, based on the argument. The argument is the new x or the new y position. The setX and setY functions apply to the bat character. When we call these functions we need to write bat.setX and bat.setY. Pressing the setX and setY buttons will write setX and setY in your code, respectively.|
|onClick is a function with no argument. The onClick function is called when we click on the mouse. We define the function for the character we want to click on. For example, if we want to click on the monkey and crocodile, we will define two functions:
Pressing on the onClick button will add the syntax of the onClick function to your code as follows:
.onClick = () ->
# Your code here
Be sure to add the character for whom you are defining the onClick before the dot.
|toss() is a function with no arguments that is used in the last chapter to throw coconuts at the hippo and the gorilla. Pressing
on the toss button will write toss() in your code.