Skip to content

Instantly share code, notes, and snippets.

@C0D4-101
Created November 9, 2020 08:05
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 C0D4-101/ba7b97e64511d1defd790fedaf30300c to your computer and use it in GitHub Desktop.
Save C0D4-101/ba7b97e64511d1defd790fedaf30300c to your computer and use it in GitHub Desktop.
i cant even
<!DOCTYPE html>
<html>
<head>
<title>A cleaner Game</title>
</head>
<body>
<label id ="goldText">0</label>
<label id ="healthText">0</label>
<label id ="damageText">0</label>
<button id="quest">Quest</button>
<button id="upgrade">Upgrade</button>
</body>
<script src="main.js" type="text/javascript"></script>
</html>
// set global scope
let goldLabel = document.getElementById('goldText');
let healthLabel = document.getElementById('healthText');
let damageLabel = document.getElementById('damageText');
let health;
let gold;
let damage;
const damagePrice = 10;
const damageUpgrade = 10;
/**
* Initialse global values once the browser has finished loading
**/
function init(){
health = 100;
gold = 0;
damage = 1;
renderStats();
}
/**
* get current stats and return them
*
*/
function getStats(){
let stats = {
'health': health,
'gold': gold,
'damage': damage
};
return stats;
}
/**
* fetch stats and output to DOM
*/
function renderStats(){
let stats = getStats();
goldLabel.innerText = stats.gold;
healthLabel.innerText = stats.health;
damageLabel.innerText = stats.damage;
}
/**
* handle upgrades and take gold from player
**/
function upgradeDamage(){
if(gold >= damagePrice){
gold = gold - damagePrice;
damage = damage + damageUpgrade;
}
}
/**
* Increase Gold and injure the player
* on health reaching 0 enter the kill screen and reset game
**/
function doQuest(){
gold = gold + 1;
health = health - damage;
if(health <= 0){
killScreen();
}
renderStats();
};
function doUpgrade(){
upgradeDamage();
renderStats();
};
function killScreen(){
document.body.innerHTML = 'YOU DIED!';
init();
}
init();
document.getElementById('quest').addEventListener('click', doQuest);
document.getElementById('upgrade').addEventListener('click', doUpgrade);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment