If you want to expose yourself to different programming tasks and challenge yours skills, you should start with the game programming. By now I think you are familiar with JavaScript enough to be able to run it in some form. JavaScript is really the basic of this modern programming era. In this last part of the article we will show you how to do game programming using JavaScript especially because many people today are interested in game development.
In these particular examples we are using JavaScript which is easy to use and requires no extra tools, just simple text editor for writing. If you are beginner I recommend you to learn more from Code Academy’s online JavaScript course to help you get more prepared. Our examples are for creating HTML5 games, so keep this in mind.
Primitive Variables
You may be familiar with the saying a variable is a container, which means variables are used for storing data and here is example
var playerName = "Jesse Smith";
You may come acroos three basic types of primitive variables. String which are textual variables which are always in quotes. Number variables are postitive, negative and whole numbers and fractions. Third type of primitive variable are booleans which are true and false statements. Booleans can use numbers for making statements, the value of 1 can represent true and value od 0 can represent false.
This is example of storing player’s name and score
//String var playerName = "Jesse Smith"; //Number var score = 0; //Boolean var isAlive = true;
Here you can see player’s status, whether player is dead or alive. Besides these simple information, you can store anything within variable.
Variables and Arrays
Similar to the variables are arrays which can contain more than one value unlike variables. Particularly in game development we can use arrays as lists. For example for tracking items in inventory we are using arrays. This is example of setting up empty array
var inventory = [];
If you want to create array with items inside do it like this
var inventory = ["item0", "item1", "item2"];
In this particular example we have three items within inventory arrays but besides using strings you can add numbers and booleans as well. Next step is to get the value aftering accessing the index of array. This value represents unique ID which is representation of the item in the particular array. This is example of getting value of item 0
inventory[0];
This is example of adding something new to the array
inventory.push("item4");
Array are holding different kinds of structure material and data. Table is the most common array structure. Here is example of table structure array with rows and columns
var map = [[1,1,1,1,1], [1,0,0,0,1], [1,0,1,0,1], [1,0,0,0,1], [1,1,1,1,1]];
You should keep in mind that arrays can store strings as well as booleans, not only numbers.
Objects and Functions
Objects are used for storing more complex data structures unlike arrays and variables. We can imagine objects as huge container storing great number of variables, arrays and functions. This is example of creating an object
var player = {};
Here is shown how to put name variable on the single player
player.name = "Jesse Smith";
Attaching a variable to the object, we make that variable property of an object. In order to access the player’s name you should follow this step
player.name;
Functions allows you to control timing of execution of block codes and to design container consisted of reusable instructions. Look at this following example
var firstName = "Jesse"; var lastName = "Smith"; var fullName = firstName + " " + lastName;
Here you can see we combined two variables, in this case we combined first and last name os player. I am hoping some of these examples provided you with much needed information and will help you to move in the right direction.