Created
March 3, 2014 09:32
-
-
Save arayi/9321464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an example of JS prototypes in a simple, | |
// incomplete game about keeping pet triangles. | |
// Triangles are rats. | |
// They go beep and trundle (among other things). | |
// Just roll with it. | |
// Here's our prototype for a standard triangle! | |
// As you can see, they each have a name, | |
// make beeping sounds when poked, trundle around, | |
// and can interact with food or take naps when they've | |
// eaten too much. | |
function Triangle(name) { | |
this.name = name; | |
this.sound = "beep"; // Default beeping sound. | |
this.speed = 1; // Default triangle speed. | |
this.trundle = function() { | |
// Allows triangle to move. | |
} | |
this.poke = function() { | |
// Plays beeping sound when triangle is clicked. | |
} | |
this.naptime = function() { | |
// Checks if triangle has eaten too much lately. | |
// If so, moves triangle to back corner & | |
// drops speed to 0 for 20 ticks. | |
} | |
this.getFood = function() { | |
// Checks if triangle is hungry. | |
// If so, triangle will eat. | |
// If not, triangle will pick up nearest food | |
// & put it in a pile in the corner. | |
} | |
} | |
// Just DRYing this code off a little over here. | |
function randomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function randomCoord(size) { | |
return randomInt(0, size - 1); | |
} | |
// This game randomizes the size of the triangle cage, | |
// showing the versatility of constructor functions. | |
// When the triangle keeper scatters food in the cage, | |
// it has to stay within the bounds of the cage, so all | |
// coordinates are calculated from the initial size of | |
// the instantiated triangle cage. | |
// This allows us to make any number of cages without | |
// being clumsy and scattering food outside the cage. :) | |
function Cage() { | |
this.size = randomInt(5,10); // Size between 5-10 sq. units. | |
// Initializes array for foods. | |
this.foods = new Array(this.size); | |
for (i = 0; i < this.size; i++) { | |
this.foods[i] = new Array (this.size); | |
} | |
// Scatters food inside the cage. | |
this.scatterFood = function() { | |
for (i = 1; i < (this.size * 3); i++) { | |
var x = randomCoord(this.size); | |
var y = randomCoord(this.size); | |
if (this.foods[x][y] = null) { | |
this.foods[x][y] = 1; | |
} else { | |
this.foods[x][y] += 1; | |
} | |
} | |
} | |
} | |
// So let's start our game... | |
function newGame() { | |
// We start by giving the triangles a house to live in. | |
var triangleHouse = new Cage(); | |
// Once the triangles have a house, we need to allow any | |
// new triangles to have coordinates within the bounds of that | |
// cage, so we modify the prototype based on the cage size. | |
Triangle.prototype.position = function() { | |
return [randomCoord(triangleHouse.size), randomCoord(triangleHouse.size)]; | |
} | |
// Then we scatter some food in the new cage, | |
// and we put the triangles in it! | |
triangleHouse.scatterFood(); | |
var lumi = new Triangle("Lumi"); | |
var eowyn = new Triangle("Eowyn"); | |
} | |
// Since this is just an example of how useful | |
// prototypes are, this game is VERY incomplete. | |
// I hope you enjoyed reading it! <:3 )~~~~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment