Before we continue with the drawing - I've been thinking about the structure of this blog. I think it is important first that we talk about how we write code in java-script. Java-script is powerful. Java-script can easily become a big mess. A lot of programmers don't understand object-oriented programming.
I will try and explain it. It takes a lot of experience to really, really get it. So the sooner we start, the better.
First of all, there is a reason why object-oriented programming has supplanted procedural programming. If you're new to this all, procedural programming and modular programming (excuse the grouping together) is what people used to do. It is what comes most natural to all of us.
A procedural program typically looks like.
step 1. do something
step 2. do something else
step 3. go to step 5
step 4. show some output
step 5. go to step 4
They are instructions, commands for the computer to do. It comes natural, and kids love it because "they" get to give the commands. So whats wrong with that? The problem is one of scale. The first computers were small compared to modern computers. People found out fast that as computers grew so did their programs. The problem with procedural programs is usually called "spaghetti-mess". The more you need to do, the more instructions you need to add, and at some point, you lose control.
Same happened with my first big Indy game. I ended up with a situation where whenever I made one small change to one part of the program, another part would break unintentionally. I had lost control over the code.
Don't despair, there is a solution. Quite a nice one. Its called object-oriented programming. Object-oriented actually means "data-oriented". But since
data already meant so many other things, the inventors decided to name it
object-oriented instead.
The main difference between the two styles is that you focus on the data first, not the instructions. The WHAT, not the HOW. To do that you need to figure out what it is you're "modelling". In a game typically, you have objects like "people", "vehicles", "weapons", "buildings" etc. All these things become their own "objects". In business applications you might have "invoices", "people", "addresses", and "bank accounts" as your objects.
Java-script is an object-oriented language (as well as a procedural language if you wish to use it that way). Therein lies the danger. If you decide not to use it in an object-oriented manner, you run the danger of creating spaghetti-mess. My advice, be disciplined, and don't be lazy. It will save you from going insane in the end.
Java-script is simple in what it contains as a language. Its main data-types (different kinds of items) are:
strings (things within single or double quotes like "Peter" or 'the mouse')
numbers (integers and floating point like 1, 2, 0, -10, 1.5, 3.14159265)
null (a special word meaning 'no value')
true,
false (Boolean logic values)
undefined (a special word meaning 'not defined' / 'unknown')
function (a thing that groups instructions on what to do)
this (a very special word in java-script that refers to an object itself)
Furthermore we have collections called arrays e.g. [1,2,3,4] is a set of four numbers grouped into an array. Finally we have hash-maps and collections inside curly brackets - but they are for later.
So suppose we wanted to model a space-ship, as we will be doing in this game, we'd first need to think of what defines it. Here is my first attempt.
// this is a comment - it starts with "//" and anything after it
// is completely ignored by java-script
function Ship()
{
var x = 0;
var y = 0;
this.playerName = "Peter";
this.draw = function()
{
// do some drawing
};
}
We've defined our first "object" (called a "Class" in the object-oriented world). The object is called "Ship". The word "function" in front of it defines the "class" in java-script. This class has three variables in it called "x", "y", and "playerName". "x" and "y" are both "initialised" to 0 (the number zero).
this.draw is defined as a "function". This makes java-script a little confusing in my opinion. The "this" makes the "draw" visible to things outside the "Ship" class. Its a java-script convention. "draw" is a grouping of instructions. At the moment "draw" is empty - it just contains one comment.
Note as well that every "instruction" or "statement" ends with a semi-colon ( ; ). This is important to java-script it marks where things end (like a full-stop in an English sentence).
It might help too, in your mind, to replace { with the English word "begin" and } with the English word "end".
Any instruction inside the "Ship" curly brackets "{" and "}" can access the "x" , "y" and "playerName" variables. However, both "x" and "y" are private (because of the "var" keyword) to "Ship", and the outside world will be unaware of "x" and "y". "this.playerName" is a different story and can be changed by any other part of the program.
You don't need to write java-script like this, you can write it procedurally like a lot of people do. However, you get massive advantages writing it as a "class".
The "ship" class becomes a blue-print for all "ships" in the game. You can have a 100 ships all acting independently using the one class as a template. Doing that in a procedural language is a lot more difficult and requires a lot more effort.
Furthermore, the "x" and "y" variables inside the "ship" are hidden from outside code. This is called "encapsulation". The advantage of that particular little trick is that things outside ship can't just change these values - they're protected. It makes ship more "robust" and controllable. It makes the ship a black box too in certain respects. As you grow as a developer you can make use of this. You can hide a lot of complexity and make things look a lot simpler.
Is object-oriented programming the end-all of things? Short answer, "NO". Object-oriented has weaknesses too. As programs continue to grow, and as things are getting more "parallel", object-oriented programming too is starting to show cracks. There are attempts at the moment to mix object-oriented with functional programming (e.g. the Scala language). Functional programming on its own is very weak and suffers from procedural flaws. However, functional programming is great for writing code that can be used in parallel. The Scala language is a very good attempt at mixing object-oriented with functional and shows promise. Scala's main weakness is that it is a complex language, possibly too complex for mass-adaptation.
The main object-oriented languages of today are (in no particular order) Java, C#, C++, Perl, Python, Scala, Ruby, and Java-script.
It is also important to note that "Java" is not related to "java-script". They are two completely different languages.
peace-out
NB. Peter's maxim;
"code should be no more complex than what it needs
to express succinctly and clearly."
-- pmgdv