Student Guide
Unit 1 - Escape Room
Lesson 1 - Functions
Placing . after a function lists its subcommands: Terminal.WriteLine()
Text variables or messages go into “”. Terminal.WriteLine(“Hello, World!”);
Each line ends with a ;
Functions take () ---inside the () you can put in variables/arguments/messages to pass
Lesson 2 - Functions
Functions should start with void Function()
The things that functions do go inside { }:
void FunctionName()
{
the things function does;
}
Lesson 3 - Variables
Variables
Types: int string bool float
Structure: (scope) type name (= value;)
int door;
public int door;
public int door = 1;
Once declared, you can call the functions in place of values that you would normally type in. For example:
move.Forward(50); can be rewritten:
int speed = 50;
move.Forward(speed);
Lesson 4 - Functions w/ Arguments
Functions with Arguments: MoveUp (int speed)
If a function accepts arguments, it needs to receive it when they are first called. For example, if you call this function from void Update:
void Update()
{
MoveUp(5);
}
void MoveUp(int speed)
{
go.Up(speed);
}
Lesson 5 - C# operators
This a very useful resource to explain the different types: https://www.w3schools.com/cs/cs_operators.asp
Lesson 6 - Conditionals
If statement
if (condition == “something”)
{
The thing that you want to do;
}
If & else statement:
if (condition == “something”)
{
The thing that you want to do;
}
else
{
The other thing you want to do;
}
If & else if statement
if (condition == “something”)
{
The thing that you want to do;
}
else if (condition == “something”) //you can add more else ifs
{
The other thing you want to do;
}
else
{
The thing you want to if nothing else is true;
}
Lesson 8 - Booleans
Logical operators are also called boolean operators. They operate with truth(boolean) values and the result is always a truth value (true or false). These operators are useful if you want to combine several conditions into a single statement.
Since we want a boolean variable available from all script functions, we need a global(member) bool:
public bool main; // (it is assumed to be false if not assigned true)
With this bool, you can switch it on or off and do different things based on its condition.
If (main == true)
{
Do A
}
else
{
Do B
}
Lesson 9 - Refactoring
As we keep adding new functionality and code, our code gets long. It becomes something coders refer to as: Spaghetti Code:
We refactor code so that we can easily follow our code. Each Function does only one thing, and we can easily change functionality in our game by editing that specific function, instead of hunting it down.
Lesson 11 - Arrays
To declare an array, define the variable type with square brackets:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars[0]);
// Outputs Volvo
Lesson 12 - Random
We can add randomness by using Random
password = door1Passwords[Random.Range(0, door1Passwords.Length)];
Starts from item 0
arrayName.Length means the number of the last item
Random.Range() picks one number from the range.
Lesson 13 - Adding multiple lines into Terminal.WriteLine
Use @"
Terminal.WriteLine(@”Line 1 here
Line 2 here”);
Lesson 14 - Password.Anagram()
This is function provided inside our asset only
void StartGame(string input)
{
Terminal.WriteLine("You have chosen door " + door);
Terminal.WriteLine("Please enter your password: " + password.Anagram());
pass = true;
if (input == password)
{
Terminal.WriteLine (“WELL DONE!!);
DisplayWinScreen();
}
else
{
Terminal.WriteLine (“Sorry, wrong password!”);
}
}