Game Tutorial OOP 1

We will make the same game again in OOP using Text Game Manager as a framework. TM.IObject, TM.ILoopObject and TM.IProgram will be used. Please check the document first before you start.


  • Create Frame class extending TM.IObject class.


**View source code: game-tutorial-oop1.js

Only the setting is the same from the previous program.

var screenSetting = {
  column: 55,
  row: 22,
  fontFamily: 'Nanum Gothic Coding',
  fontSource: '',

var charGroups = {
  wall: {
    chars: '■',
    isFullwidth: true,
    sizeAdj: 1.2,
    xAdj: -0.05,
    yAdj: 0.03,

var debugSetting = {
  devMode: true,

var TMS = new TM.ScreenManager(screenSetting,charGroups),
    TMI = new TM.InputManager(screenSetting.canvasId,debugSetting.devMode),
    TMD = new TM.DebugManager(debugSetting);


TMS, TMI and TMD are created and cursor is hidden.

// Frame
// Object Type: TM.IObject
var Frame = function(data){ = {
    x: undefined,
    y: undefined,
    width: 44,
    height: 18,
    score: 0,
Frame.prototype = Object.create(TM.IObject.prototype);
Frame.prototype.constructor = Frame;

Frame class is created extending TM.IObject class. Frame takes data as a parameter. shows the structure of data. data will be merge to in,data). undefined in x, y means they should be passed in data. width, height and score does not need to be passed from data since they already have value.

// TM.IObject functions implementation
Frame.prototype._init = function(){
Frame.prototype._inactivate = function(){

TM.IObject has init and inactivate functions. init has code for initializing the instance and executed  when an instance created, inactivate has code for inactivating the instance. init can be called to re-activate/initialize the instance after it is inactivated.

To extend init and inactivate functions of TM.IObject, do not extends them directly but create _init and _inactivate functions. _init and _inactivate will be executed when init, inactivate are executed.

_init: calling other functions to draw frame and score.

_inactive: calling the same functions with true. let's see them.

// Frame functions
Frame.prototype.drawFrame = function(remove){
  for(var i=0; i<; i++){
    for(var j=0; j<; j++){
      if((i===0 || || j===0 || === 0)){
        var frameText = "■";
        if(remove) TMS.deleteTextAt(,,frameText);
        else TMS.insertTextAt(,,frameText);
Frame.prototype.drawScore = function(remove){
  var scoreText = "Score: ";
  else {
Frame.prototype.addScore = function(){ += 100;

drawFrame function draws frame but also removes them if it get true as remove parameter.
drawScore function also works the same way.
addScore function adds score and draws score.

var frame;
  frame = new Frame({x:5,y:2});

Create frame instance using Frame class. {x:5,y:2} is passed as data.

Run Example

Type these commands into the browser console on this page to test.

frame.inactivate() //inactivates frame.(frame and score will be deleted)
frame.init() //re-activates frame.(frame and score will be drawn)
frame.addScore() //adds score and draw score.


Add Comment

Login with SNS account to write comments. see details