Professional Documents
Culture Documents
NW Console To Boo PDF
NW Console To Boo PDF
What 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 (thats the underlying infrastructure that runs C#). You can find out much more about the
language by reading the Boo manifesto (PDF).
The Syntax of Boo is directly inspired from the Python programming 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 webpage and by reading The Language Guide.
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 programmers life easier and its easy to see why Boo might be the language of choice for your next
game.
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 the Type VariableName
syntax, Boo uses the VariableName as Type keyword. 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:
In 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.
Boo
using UnityEngine;
using UnityEngine.UI;
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#
Boo
In addition, instead of using { and } to define the limits of the classs 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 the Boo
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
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 the NextGuess() function:
C#
Boo
void NextGuess () {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}
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 following NumberWizard.boo file 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')
Notice 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 :)