For example, compare the ‘Hello World’ programs in C# below: public class HelloWorld { public static void Main { System.Console.WriteLine"Hello from C#!"; } } To a Boo equivalent: print"
Trang 1What is Boo?
Boo is a programming language that runs on the same platform that C# does It was created around 2004 by Rodrigo Barreto de Oliveira to fix some of the perceived issues with existing programming languages on the Common Language Runtime (that’s the underlying infrastructure that runs C#) You can find out much more about the language by reading theBoo manifesto (PDF)
The Syntax of Boo is directly inspired from thePythonprogramming language, from which it takes the syntactically
significant whitespace What this means is that indenting has meaning in Boo and is how you define blocks of
code This has the benefit of making all boo code look similar and forcing programmers to use good indenting habits
In practice it has the look and feel of python with the majority of the concepts from C#
You can learn much more about Boo on the project webpageand by readingThe Language Guide
Why Use Boo?
Boo is a very programmer-friendly language Like Python, it gives you the power to express your programs simply and in a readable syntax, and mostly gets out of the way For example, compare the ‘Hello World’ programs in C# below:
public class HelloWorld {
public static void Main() {
System.Console.WriteLine("Hello from C#!");
}
}
To a Boo equivalent:
print("Hello from Boo!")
Combine this with complete access to all the same constructs as C# programs, and many useful syntactic features that make a programmer’s life easier and it’s easy to see why Boo might be the language of choice for your next game
Translating our first script
In these translation notes and subsequent translation notes, we will concentrate on feature that are unique to boo and highlight the differences between the scripts that we have not encountered before Since this is the first time
we have translated to Boo, we will go through theNumberWizard.csfile in some details Future translation notes will simply highlight new or interesting features that were introduced by the project instead of going over familiar ground You should also take a look at the differences between Boo and C# onThe Boo Website There is alsoAn Online Translatoravailable that will translate from C# to Boo
Don’t forget that the Entire project file is translated into boo and is attached to this lecture!
General differences
There are some clear changes and equivalences that will be most obvious from first reading Boo code
The first of these is how type declaration works in Boo Instead of declaring types by using theType VariableName syntax, Boo uses theVariableName as Typekeyword For example:
This does not affect the use of the private, protected, and public keywords, which can still be used when declaring variables in the same way:
Trang 2In addition it can be seen from the above snippets that Boo does not separate statement using semicolons, instead, new statements are newline delimited This, besides saving us some typing and reducing visual noise in our code, also eliminates a very common source of errors
Importing other modules
Just like in C#, modules we use have to be imported By default, new Boo scripts will includeimport UnityEngine
at the top This will import all Unity specific features that are commonly used, for example, theMonoBehaviour class The namespaces and module names are the same in both languages, and are in fact the same underlying code and libraries
C#
using UnityEngine;
using UnityEngine.UI;
Boo
import UnityEngine
import UnityEngine.UI
Declaring classes
The syntax in Boo for class declaration directly corresponds to C#, however, the parent class from which our class
is derived is passed in as an argument within parentheses For example:
C#
// NumberWizard inherits from Monobehaviour
public class NumberWizard : MonoBehaviour {
//
}
Boo
# NumberWizard inherits from Monobehaviour
public class NumberWizard(MonoBehaviour):
#
In addition, instead of using{and}to define the limits of the class’s code, Boo uses a colon after the class declaration followed by indenting Anything with the same indent level as the first line after a colon will be considered in the same block by the Boo parser
We can also see another difference between the two languages in the snippets above Boo single line comments will start with#rather than//
Declaring Methods
The method syntax for Boo is similar to the class definition syntax You can find out all the gory details on theBoo wiki on Github
In general, Boo methods and functions are defined like this:
access def MethodName(arg1 as Type, arg2 as Type) as ReturnType:
return Something
Key differences to note with C#:
• Methods/functions are defined using thedefkeyword
• The return type is optional, declared using the askeyword and comes after the function arguments but before the colon
• Method/function names use the same convention as in C#, that is capitalised camel-case
• The access keywordspublic, privateandprotectedhave the same meaning as in C# and are placed before def
• Argument types are declared using theaskeyword as well
Trang 3• Since whitespace and newlines are syntactically significant, statements do not need to be terminated with semicolons, and the language does away with semicolons altogether
From the NumberWizard game, we can see all the differences when looking at theNextGuess()function:
C#
void NextGuess () {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}
Boo
def NextGuess():
guess = ((max + min) / 2)
print(('Higher or lower than ' + guess))
print('Up = higher, down = lower, return = equal')
And because Boo declares blocks of code using indentation, we do not need a closing brace before the next function,
we can simply de-indent Putting this together gives us the followingNumberWizard.boofile for our game:
import UnityEngine
public class NumberWizards(MonoBehaviour):
max as int
min as int
guess as int
def Start():
StartGame()
def StartGame():
max = 1000
min = 1
guess = 500
max = (max + 1)
print('========================')
print('Welcome to Number Wizard')
print('Pick a number in your head, but don\'t tell me!')
print(('The highest number you can pick is ' + max))
print(('The lowest number you can pick it ' + min))
print(('Is the number higher or lower than ' + guess))
print('Up = higher, down = lower, return = equal')
def Update():
if Input.GetKeyDown(KeyCode.UpArrow):
min = guess NextGuess()
elif Input.GetKeyDown(KeyCode.DownArrow):
max = guess NextGuess()
elif Input.GetKeyDown(KeyCode.Return):
print('I won!') StartGame()
def NextGuess():
guess = ((max + min) / 2)
print(('Higher or lower than ' + guess))
print('Up = higher, down = lower, return = equal')
Trang 4Notice also here how space is used to delimit functions from each other The convention is to leave a blank line between the end of a function and the next one, while leaving two between classes
See you in the next translation notes for more Boo features and code :)