Skip to content

Instantly share code, notes, and snippets.

@CrowderSoup
Created December 9, 2015 07:29
Embed
What would you like to do?
Hour Of Code Star Wars Game
var game = function () {
setBackground("random");
setMap("random");
setDroid("R2-D2");
setDroidSpeed("normal");
playSound("R2-D2random");
var moved = false;
var points = 0;
var pointsToWin = 1000;
function handleMoved() {
moved = true;
// Start the game with a random number of pigs (between 10 and 2)
startGame(getRandomNumber(2, 10));
}
function startGame(numberOfPigs) {
addCharacters("PufferPig", numberOfPigs);
}
this.gotPig = function() {
playSound("R2-D2sound1");
playSound("PufferPigSound2");
points += 100;
addPoints(100);
if(points >= pointsToWin){
playSound("applause");
endGame("win");
} else {
var min = 1,
max = 2;
addCharacters("Mynock", getRandomNumber(min, max));
addCharacters("PufferPig", getRandomNumber(min, max));
}
}
this.gotMynock = function() {
playSound("R2-D2sound6");
playSound("MynockSound1");
points -= 50;
removePoints(50);
if(points > 0) {
var min = 1,
max = 2;
addCharacters("Mynock", getRandomNumber(min, max));
addCharacters("PufferPig", getRandomNumber(min, max));
} else {
endGame("lose");
}
}
// Movements
this.up = function() {
if(!moved){
handleMoved();
}
goUp();
}
this.down = function() {
if(!moved){
handleMoved();
}
goDown();
}
this.left = function() {
if(!moved){
handleMoved();
}
goLeft();
}
this.right = function() {
if(!moved){
handleMoved();
}
goRight();
}
// Utils
function getRandomNumber(min, max){
return randomNumber(min, max);
}
function addCharacters(type, number) {
for(var i = 0; i < number; i++) {
addCharacter(type);
}
}
};
var game = new game();
// Map movements
function whenUp() {
game.up();
}
function whenDown() {
game.down();
}
function whenLeft() {
game.left();
}
function whenRight() {
game.right();
}
// Map getting characters
function whenGetPufferPig(){
game.gotPig();
}
function whenGetMynock() {
game.gotMynock();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment