Skip to content

Instantly share code, notes, and snippets.

@marcodulog
Last active November 30, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcodulog/a554d1cb6fa3b71ef3c2210f5ff67af3 to your computer and use it in GitHub Desktop.
Save marcodulog/a554d1cb6fa3b71ef3c2210f5ff67af3 to your computer and use it in GitHub Desktop.
Character Game Santa vs Buddy the Elf
<html>
<title>Character Game</title>
<head>
<audio id="audio" src="beep-07.wav" autostart="false"></audio>
</head>
<body>
<h1>Santa vs Buddy the Elf</h1>
<hr>
<p>
This game is about the Santa and his quest to bring back Buddy the Elf.
</p>
<p>
Santa will be battling Buddy the Elf. Santa is the computer are buddy. You have 2 special abilities that you get to choose from to make sure that you stay in New York.
</p>
<h2>Santa</h2>
<select id="sAbility">
</select>
<input type="button" id="btTest" value="Attack" onClick="btnPlayer1()">
<h2>Buddy</h2>
<select id="pAbility">
</select>
<input type="button" id="btTest" value="Attack" onClick="btnPlayer2()">
<script>
//variables
var sound = document.getElementById("audio");
var santa = {
name: "santa",
abilities: ["Hammer", "Magic"],
damage: [40, 50],
health: 200
};
var buddy = {
name: "buddy",
abilities: ["Maple", "SugarCane"],
damage: [40, 50],
health: 200
};
window.onload = function() {
var selPlayer1 = document.getElementById("sAbility");
var selPlayer2 = document.getElementById("pAbility");
var htmlString = "";
//load player 1 abilities and names
for (i = 0; i < santa.abilities.length; i++) {
console.log(santa.abilities[i]);
htmlString += '<option value="' + santa.damage[i] + '">' + santa.abilities[i] + '</option>';
}
selPlayer1.innerHTML = htmlString;
htmlString = ""; //blank out the html string
//load player 1 abilities and names
for (i = 0; i < buddy.abilities.length; i++) {
console.log(buddy.abilities[i]);
htmlString += '<option value="' + buddy.damage[i] + '">' + buddy.abilities[i] + '</option>';
}
selPlayer2.innerHTML = htmlString;
};
function btnPlayer1() {
sound.play();
var choice = document.getElementById("sAbility");
var damage = rollDice(choice.value);
buddy.health = buddy.health - damage;
alert("Santa lands a " + damage + " hit with " + choice[choice.selectedIndex].innerText + ". Buddy's health is now: " + buddy.health);
}
function btnPlayer2() {
var choice = document.getElementById("pAbility");
var damage = rollDice(choice.value);
santa.health = santa.health - damage;
alert("Buddy lands a " + damage + " hit with " + choice[choice.selectedIndex].innerText + ". Santa's health is now: " + santa.health);
}
//this is the roll dice function to handle random events
function rollDice(side) {
return Math.floor(Math.random() * side);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment