-
-
Save C0D4-101/ba7b97e64511d1defd790fedaf30300c to your computer and use it in GitHub Desktop.
i cant even
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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