Skip to content

Instantly share code, notes, and snippets.

@erkgmz
Created November 13, 2014 16:36
Race Game
<script>
function Animal(name, speed, health){
this.name = name;
this.speed = speed;
this.health = health;
this.position =0;
this.isFocused = function(){
return Math.floor(Math.random() * 10) < this.health;
}
this.advance = function(){
if(this.isFocused()){
this.position += this.speed;
}
}
this.status = function(){
return this.name + " is at: " + this.position
}
}
var rabbit = new Animal("Bugs Bunny", 8, 5);
var turtle = new Animal("Leonardo", 3, 10);
var meters = 50;
while(rabbit.position < meters && turtle.position < meters){
rabbit.advance();
turtle.advance();
alert(rabbit.status() + " - " + turtle.status());
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment