Flappy Tim - Part Two

From Contraptionmaker Wiki
Revision as of 21:06, 5 September 2014 by Keith (Talk | contribs)

Jump to: navigation, search

If you are just getting started with mods, make sure to first check out Getting Started, and Flappy Tim - Part One.

Picking up where Flappy Tim - Part one left off, this is what our level looks like right now:

And this is the entire Flappy Tim script:

// onStart called when play is hit 
function onStart()
{
}

// onUpdate called every frame
function onUpdate()
{
  if (FlappyTim.getVelX() < 50)
  {
     FlappyTim.addForce(CM.DIR_RIGHT,1);
  }
}

function onKeyDown(keyCode)
{
  if (keyCode == CM.KEY_SPACEBAR)
  {
     FlappyTim.addForce(CM.DIR_UP,5);
  }
}

FlappyTim.onCollision = function(otherPart)
{
   partType = otherPart.getType();
   if (partType==CM.PART_BIG_PIPE)
   {
       CM.showGameOver();
   }
}

The biggest problem with this version of Flappy Tim right now is that once you flap your way off the screen - you cannot see Flappy Tim any more. In Flappy Bird, the camera follows the bird throughout the level so the level can be as large as you want it to be. Let’s do the same for Flappy Tim.

Fortunately, we have a global CM function that can do just this. It allows you to center the camera on a point and set the zoom level. We need to make sure this happens every frame, so we’ll put this into the onUpdate function:

function onUpdate()
{
   var x = FlappyTim.getPosX();
   var y = FlappyTim.getPosY();
   CM.zoomAtPoint(x, y, 2);

  if (FlappyTim.getVelX() < 50)
  {
     FlappyTim.addForce(CM.DIR_RIGHT,1);
  }
}

Now when you hit play, the camera will follow right alongside FlappyTim. Now we can make our level a little bigger by adding some more pipes. We’ll also add a pipe at the top so they can’t just fly over the whole level.


That’s more like it! It’s starting to feel like a real game now. One thing that is missing is a score. In Flappy Bird, you get points by making it through each gap. Internally in Contraption Maker we can add invisible collision zones but we have not yet exposed this through the Make Mode tools yet. Instead, we will take advantage of the property of one of the existing Contraption Maker parts, the billiard ball. It is not affected by gravity so we can put them wherever we want. Instead of getting points for passing through the gaps, we’ll let the player get points for hitting one of these billiard balls.

The first thing we’ll do is add a global score variable up at the top of the script:

var score = 0;

Now in the collision function, we’ll check for collisions with the billiard balls and increase the score. For now we’ll just print the score out.

FlappyTim.onCollision = function(otherPart)
{
   partType = otherPart.getType();
	
   if (partType==CM.PART_BILLIARDBALL)
   {
       score++;
       print(“Score:”+score);
   }
   if (partType==CM.PART_BIG_PIPE)
   {
       CM.showGameOver();
   }
}

There is only one problem with this function - when FlappyTim hits a Billiard Ball - the ball is still there and he keeps bumping into it. We need a way to remove the ball from the level. Contraption Maker provides a function to do just that on every part: inactivate. The collision function looks like this now:

FlappyTim.onCollision = function(otherPart)
{
   partType = otherPart.getType();
	
   if (partType==CM.PART_BILLIARDBALL)
   {
       otherPart.inactivate();
       score++;
       print(“Score:”+score);
   }
   if (partType==CM.PART_BIG_PIPE)
   {
       CM.showGameOver();
   }
}

Now let’s display the score so the player can see how they are doing while they play. In the onStart method, you can create a label that displays while the game is running like so:

function onStart()
{
   GameText = UI.createLabel();
   GameText.setPosition(20,20);
   GameText.setPosition(“Ready!”);
}

Now in our collision function where we increase the score, we can just update that text by calling:

GameText.setText(“Score: “+score);

Finally - let’s give Tim something to shoot for to end the game succesfully. Let’s put a house at the end of the level that he can enter.