Skip to content

Latest commit

 

History

History
47 lines (43 loc) · 1.65 KB

README.md

File metadata and controls

47 lines (43 loc) · 1.65 KB

#JavaScript Review: Review Session 2 This past week, we looked at JavaScript, we looked at NodeJS, and we developed a JukeBox app.

Let's take what we've learned and apply it to what we know about OOP. That is, let's translate our Pacman, Ghost, and Ball classes from Ruby into JavaScript.

Ball Class

Our Ball class should have a ball_type attribute whose value can be either regular or super,

// ball.js
var Ball = function(ballType) {
  this.ballType = ballType === undefined ? "regular" : "super";
};

Ghost Class

Now that we are comfortable (somewhat) with classes in JavaScript, let's implement a Ghost class which sets its color to red, pink, white, or yellow randomly on instantiation. Also, let's use javascript's prototype in our code.

// ghost.js
var Ghost = function() {
  this.color = ["pink", "red", "yellow", "white"][Math.floor(Math.random() * 4)]
}

PacMan Class

Using what we've learned from the previous examples, let's implement a PacMan class that can eat ghosts and balls, and also has some attributes such as points and lives.

Use our iRuby version as an example.

// pacman.js
var Pacman = function() {
  this.extraLives = 2;
  this.points = 0;
  this.superTime = 0;
}

Pacman.prototype = {
  eat: function(obj) {},
  eatBall: function(ball) {
    this.superTime -= 1;
    this.points += ball.ballType === "regular" ? 1000 : 10;
    if (ball.ballType === "super") {
      this.superTime = 10;
    }
  },
  eatGhost: function(ghost) {},
  die: function() {},
  gameOver: function() {}
}