I am making a game that uses a text map to draw obstacles and courses for the player to navigate. However, I am concerned that my method of looping through the text map to draw objects on the screen isn't as efficient as it could be.
View the playable project on Khan Academy.
Here is my code dealing with the text map:
var platforms = [];
platforms.add = function(xPosition, yPosition, width, height, colour) {
platforms.push( new Block(xPosition, yPosition, width, height, colour) );
};
platforms.display = function() {
for (var i = 0; i < platforms.length; i++) {
platforms[i].display();
}
};
var levelOne = [
// each symbol is equivalent to 40 pixels
["##########"], // # - block
["# #"], // S - starting position
["# #"],
["# #"],
["# #"],
["# # #"],
["# ##"],
["# # #"],
["#S # #"],
["##########"],
];
var symbol;
var playerX;
var playerY;
for(var i = 0; i < levelOne.length; i++) {
for(var j = 0; j < levelOne[i].length; j++) {
for(var v = 0; v < levelOne[i][j].length; v++) {
var symbol = levelOne[i][j][v];
if(symbol === "#") {
// Platform arguments: X, Y, Width, Height, Color
platforms.add(v*width/10, i*height/10, width/10, height/10, color(0, 174, 255));
} else if (symbol === "S") {
playerX = v*width/10;
playerY = i*height/10;
}
}
}
}
How can I make this code more efficient?