Skip to content

Instantly share code, notes, and snippets.

@macikokoro
Created June 11, 2014 21:19
Show Gist options
  • Save macikokoro/c9da16ef64aadcf05de8 to your computer and use it in GitHub Desktop.
Save macikokoro/c9da16ef64aadcf05de8 to your computer and use it in GitHub Desktop.
Fun JavaScript game based on the rabbit and the turtle story.
<script>
var turtle, rabbit,monkey, km;
function Animal(name, speed, focus) {
this.name = name;
this.speed = speed;
this.position = 0;
this.focus = focus;
this.run = function() {
if (Math.random() * 10 < this.focus) {
this.position = this.position + this.speed;
}
}
}
turtle = new Animal("Beth", 1, 10);
rabbit = new Animal("Peter", 4, 2);
monkey = new Animal("George", 6, 1);
km = 20;
while (rabbit.position < km && turtle.position < km && monkey.position < km) {
rabbit.run();
turtle.run();
monkey.run();
console.log(rabbit.name + ": " + rabbit.position + ", " + turtle.name + ": " + turtle.position + ", " + monkey.name + ": " + monkey.position);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment