Coding Adventure Part 3 - Coding Concepts

This article will provide you with in-depth explanations of the coding concepts taught on Coding Adventure Part 3, how and when to use them.

Written by Livnat Hershkovitz
Updated over a week ago

## These are the coding concepts that are taught on Coding Adventure Part 3 and will be reviewed in this article:

1. Boolean Operator (not) - challenges 146 - 151

2. Comparison Operators (==, <, >) - challenges 152 - 160

3. Functions with return - challenges 161 - 175

# Boolean Operator (not)

The boolean operator taught in Coding Adventure part 3 is not.

The operator not reverses the result of the condition: yes becomes no, and no becomes yes.

The not operator can be used in an until loop or in an if statement. The syntax to use the not operator (with an if statement) is:

`if not conditionA`

`# Your code here`

If conditionA returns yes, then the not operator changes the condition of the if statement to no and the code inside the if statement will not be executed.

If conditionA returns no, then the not operator changes the condition of the if statement to yes and the code inside the if statement will be executed.

In Coding Adventure, the not operator is used to prevent the monkey from going to rotten bananas. The function` banana.rotten() `returns yes if the banana is rotten. To make the monkey go to only the good bananas, use the operator not in an if statement:

`if not banana.rotten()`

` goto banana`

Another example: if we have a challenge with green and yellow bananas, the monkey needs to go the bananas that are not green.

# Comparison Operators

The comparison operators that are used in Coding adventure part 3 are ==, <, >.

Comparison operators are used to compare between two values. The operators returns yes or no.

The comparison operators can be used in an if statement or an until loop. In Coding Adventure, the comparison operators are used mainly in until loops.

The operator == (equals to) compares the values on both of its sides. The result is yes when both values are exactly the same, otherwise it is no.

For example, if you score 10 points you get a bonus of 5 points:

`if score == 10`

` bonus = 5`

Only if the score equals 10, the value of bonus will be 5.

The operator < (less than) compares the values on both of its sides. The result is yes when the value on its left is less than the value on its right. Otherwise, the result is no.

For example, if my lives is less than zero, then the game is over:

`if lives < 0`

` say "GAME OVER"`

The operator > (greater than) compares the values on both of its sides. The result is yes when the value on its left is greater than the value on its right. Otherwise, the result is no.

For example, if you score more than your opponent, you win:

`if my_score > your_score`

` say "I win :-)"`

`else`

` say "I lose :-("`

In Coding Adventure, the comparison operators are used in:

1. an until loop to wait until the monkey's health is 100 (`until health() == 100`)

2. an if statement to check if the monkey's health is low (`if health() < 60`)

3. an until loop to let the cow eats until its weight reaches the condition on the gate (once the condition is met, the gate opens) - this is used in the skill challenges only (`until cow.weight() > 28`)

Here is a challenge that uses these three examples of comparison operators.

# Functions with return

The topic of functions is explained in details in the article Coding Adventure - Coding Concepts part 2. In this article we will explain the return statement.

A function can return a value. This value can be used in the code.

Usually, you will use a variable to save the value returned from the function.

For example, if a function returns the sum of two numbers:

`sum = (a, b) ->`

` return a+b`

Use a variable to save the returned value:

`my_sum = sum 4, 6`

In this example, the value of `my_sum` is 10.

If the function returns yes/no, it is common to use it in an if statement or until condition. In this case, you do not need to save the value returned, but use the function in the condition it self.

For example, here is a function that returns yes if a number is positive, and no otherwise:

`positive = (a) ->`

` return a > 0`

Use the function in an if statement:

`if positive -5`

` turn right`

`else`

` turn left`

In Coding Adventure there are several functions that return values:

1. distanceTo - returns the distance between the monkey and an object.

2. near - returns yes or no, depending if the monkey/rat is near an object

3. frozen()/green()/rotten() - returns yes or no, depending if the banana is frozen/green/rotten or not

4. sleeping()/playing() - returns yes or no, depending if the cat/tiger/bear is sleeping/playing or not

5. health() - returns the monkey's health

6. watching() - returns yes or no, depending if the crow is watching or not

When the code encounters a return statement, it exits the function and continues from where the function was called.

Note - in Coding Adventure, if a function returns a value, you can not use "say" in the function's code. If you use "say", you will get the error "Can't use return in a function that takes time.".

Below is an example of a function that returns yes/no:

Below is an example of a function that returns a number (0 or 1):

# Events

Events are actions triggered by the user that take place while the code

is running. As a result, events affect what is happening.

The following events are used in Coding Adventure part 3:

1. Pressing a key on the keyboard - keyboard event

2. Moving the mouse - mouse movement event

3. Clicking on the mouse - mouse click event

In order for something to happen when a user triggers an event, a designated event function (event handler) should be defined in the code. This predefined function is invoked when the event occurs.

When the designated event function is not defined in the code, the event will not trigger any action.

In Coding Adventure, the following are the event functions:

1. `onKey` - is triggered any time a key is pressed on the keyboard

2. `onMouseMove` - is triggered any time the mouse is moved

3. `onClick` - is triggered any time the mouse is clicked on

## Keyboard Event

The function (in Coding Adventure) that handles the keyboard event is onKey. It is called each time a key is pressed on the keyboard.

The syntax to define the onKey function is:

`onKey = (key) ->`

` # Your code here`

The argument of the `onKey` function is `key`, it holds the key that was pressed on the keyboard and is passed by the event. You can check if the key is a specific letter and do something based on it, like in this example:

`onKey = (key) ->`

` if key == 'w'`

` step 1`

In Coding Adventure, the keyboard event is mainly used to move the characters in the game. It is also used to open the gate by pressing on the keys that appear on the gate and making the monkey say those keys.

In this example, the user presses on the keyboard in order to solve the challenge.

## Mouse Movement Event

The function (in Coding Adventure) that handles the mouse movement event is onMouseMove. It is called each time the mouse is moved.

The syntax to define the onMouseMove function is:

`onMouseMove = (pos) ->`

` # Your code here`

The argument of the `onMouseMove` function is `pos`, it holds the the position of the mouse's cursor and is passed by the event. The position of the cursor is represented by the position on the x axis and on the y axis. Use:

`pos.x`

`pos.y`

You can also use `pos`, for example to turn to the position of the cursor. Like in this example:

`onMouseMove = (pos) ->`

` turnTo pos`

In Coding Adventure, the mouse movement event is mainly used to move the bat. The bat moves by setting its x and y position (using the pos argument). You need to lead the bat by moving the cursor to the banana, grab the banana, and then lead the bat by moving the cursor to monkey.

In this example, the user moves the mouse to get the bananas (and also presses on the keyboard to grab the bananas).

## Mouse Click Event

The function (in Coding Adventure) that handles the mouse click event is onClick. It is called each time the mouse is clicked on. The function onClick is defined per object, meaning if you want something to happen when you click on the monkey and something else when you click on the banana, you need to define two separate functions.

The function is called when clicking on the specific object for which it was defined. If you click anywhere else, nothing will happen (unless another onClick function is defined).

The syntax to define the onClick function is:

`object.onClick = () ->`

` # Your code here`

In Coding Adventure, the mouse click event is used to choose which animal to move. This is done using the variable mover. Clicking on an animal sets the value of this variable to that animal. This allows defining only one set of keys that control all the animals. Click on the animal, before moving it.

The mouse click event is also used to throw coconuts at the hippos which makes the hippos move out of the way.

In this example, the user clicks on the different animals they want to move.

Here is an example of using all three events together:

1. onKey - to step

2. onMouseMove - to turn to the position of the cursor

3. onClick - to choose which animal will move/turn