|
| 1 | +let xp = 0; |
| 2 | +let health = 100; |
| 3 | +let gold = 50; |
| 4 | +let currentWeapon = 0; |
| 5 | +let fighting; |
| 6 | +let monsterHealth; |
| 7 | +let inventory = ["stick"]; |
| 8 | + |
| 9 | +const button1 = document.querySelector('#button1'); |
| 10 | +const button2 = document.querySelector("#button2"); |
| 11 | +const button3 = document.querySelector("#button3"); |
| 12 | +const text = document.querySelector("#text"); |
| 13 | +const xpText = document.querySelector("#xpText"); |
| 14 | +const healthText = document.querySelector("#healthText"); |
| 15 | +const goldText = document.querySelector("#goldText"); |
| 16 | +const monsterStats = document.querySelector("#monsterStats"); |
| 17 | +const monsterName = document.querySelector("#monsterName"); |
| 18 | +const monsterHealthText = document.querySelector("#monsterHealth"); |
| 19 | +const weapons = [ |
| 20 | + { name: 'stick', power: 5 }, |
| 21 | + { name: 'dagger', power: 30 }, |
| 22 | + { name: 'claw hammer', power: 50 }, |
| 23 | + { name: 'sword', power: 100 } |
| 24 | +]; |
| 25 | +const monsters = [ |
| 26 | + { |
| 27 | + name: "slime", |
| 28 | + level: 2, |
| 29 | + health: 15 |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "fanged beast", |
| 33 | + level: 8, |
| 34 | + health: 60 |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "dragon", |
| 38 | + level: 20, |
| 39 | + health: 300 |
| 40 | + } |
| 41 | +] |
| 42 | +const locations = [ |
| 43 | + { |
| 44 | + name: "town square", |
| 45 | + "button text": ["Go to store", "Go to cave", "Fight dragon"], |
| 46 | + "button functions": [goStore, goCave, fightDragon], |
| 47 | + text: "You are in the town square. You see a sign that says \"Store\"." |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "store", |
| 51 | + "button text": ["Buy 10 health (10 gold)", "Buy weapon (30 gold)", "Go to town square"], |
| 52 | + "button functions": [buyHealth, buyWeapon, goTown], |
| 53 | + text: "You enter the store." |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "cave", |
| 57 | + "button text": ["Fight slime", "Fight fanged beast", "Go to town square"], |
| 58 | + "button functions": [fightSlime, fightBeast, goTown], |
| 59 | + text: "You enter the cave. You see some monsters." |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "fight", |
| 63 | + "button text": ["Attack", "Dodge", "Run"], |
| 64 | + "button functions": [attack, dodge, goTown], |
| 65 | + text: "You are fighting a monster." |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "kill monster", |
| 69 | + "button text": ["Go to town square", "Go to town square", "Go to town square"], |
| 70 | + "button functions": [goTown, goTown, easterEgg], |
| 71 | + text: 'The monster screams "Arg!" as it dies. You gain experience points and find gold.' |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "lose", |
| 75 | + "button text": ["REPLAY?", "REPLAY?", "REPLAY?"], |
| 76 | + "button functions": [restart, restart, restart], |
| 77 | + text: "You die. ☠" |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "win", |
| 81 | + "button text": ["REPLAY?", "REPLAY?", "REPLAY?"], |
| 82 | + "button functions": [restart, restart, restart], |
| 83 | + text: "You defeat the dragon! YOU WIN THE GAME! 🎉" |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "easter egg", |
| 87 | + "button text": ["2", "8", "Go to town square?"], |
| 88 | + "button functions": [pickTwo, pickEight, goTown], |
| 89 | + text: "You find a secret game. Pick a number above. Ten numbers will be randomly chosen between 0 and 10. If the number you choose matches one of the random numbers, you win!" |
| 90 | + } |
| 91 | +]; |
| 92 | + |
| 93 | +// initialize buttons |
| 94 | +button1.onclick = goStore; |
| 95 | +button2.onclick = goCave; |
| 96 | +button3.onclick = fightDragon; |
| 97 | + |
| 98 | +function update(location) { |
| 99 | + monsterStats.style.display = "none"; |
| 100 | + button1.innerText = location["button text"][0]; |
| 101 | + button2.innerText = location["button text"][1]; |
| 102 | + button3.innerText = location["button text"][2]; |
| 103 | + button1.onclick = location["button functions"][0]; |
| 104 | + button2.onclick = location["button functions"][1]; |
| 105 | + button3.onclick = location["button functions"][2]; |
| 106 | + text.innerHTML = location.text; |
| 107 | +} |
| 108 | + |
| 109 | +function goTown() { |
| 110 | + update(locations[0]); |
| 111 | +} |
| 112 | + |
| 113 | +function goStore() { |
| 114 | + update(locations[1]); |
| 115 | +} |
| 116 | + |
| 117 | +function goCave() { |
| 118 | + update(locations[2]); |
| 119 | +} |
| 120 | + |
| 121 | +function buyHealth() { |
| 122 | + if (gold >= 10) { |
| 123 | + gold -= 10; |
| 124 | + health += 10; |
| 125 | + goldText.innerText = gold; |
| 126 | + healthText.innerText = health; |
| 127 | + } else { |
| 128 | + text.innerText = "You do not have enough gold to buy health."; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +function buyWeapon() { |
| 133 | + if (currentWeapon < weapons.length - 1) { |
| 134 | + if (gold >= 30) { |
| 135 | + gold -= 30; |
| 136 | + currentWeapon++; |
| 137 | + goldText.innerText = gold; |
| 138 | + let newWeapon = weapons[currentWeapon].name; |
| 139 | + text.innerText = "You now have a " + newWeapon + "."; |
| 140 | + inventory.push(newWeapon); |
| 141 | + text.innerText += " In your inventory you have: " + inventory; |
| 142 | + } else { |
| 143 | + text.innerText = "You do not have enough gold to buy a weapon."; |
| 144 | + } |
| 145 | + } else { |
| 146 | + text.innerText = "You already have the most powerful weapon!"; |
| 147 | + button2.innerText = "Sell weapon for 15 gold"; |
| 148 | + button2.onclick = sellWeapon; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +function sellWeapon() { |
| 153 | + if (inventory.length > 1) { |
| 154 | + gold += 15; |
| 155 | + goldText.innerText = gold; |
| 156 | + let currentWeapon = inventory.shift(); |
| 157 | + text.innerText = "You sold a " + currentWeapon + "."; |
| 158 | + text.innerText += " In your inventory you have: " + inventory; |
| 159 | + } else { |
| 160 | + text.innerText = "Don't sell your only weapon!"; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +function fightSlime() { |
| 165 | + fighting = 0; |
| 166 | + goFight(); |
| 167 | +} |
| 168 | + |
| 169 | +function fightBeast() { |
| 170 | + fighting = 1; |
| 171 | + goFight(); |
| 172 | +} |
| 173 | + |
| 174 | +function fightDragon() { |
| 175 | + fighting = 2; |
| 176 | + goFight(); |
| 177 | +} |
| 178 | + |
| 179 | +function goFight() { |
| 180 | + update(locations[3]); |
| 181 | + monsterHealth = monsters[fighting].health; |
| 182 | + monsterStats.style.display = "block"; |
| 183 | + monsterName.innerText = monsters[fighting].name; |
| 184 | + monsterHealthText.innerText = monsterHealth; |
| 185 | +} |
| 186 | + |
| 187 | +function attack() { |
| 188 | + text.innerText = "The " + monsters[fighting].name + " attacks."; |
| 189 | + text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; |
| 190 | + health -= getMonsterAttackValue(monsters[fighting].level); |
| 191 | + if (isMonsterHit()) { |
| 192 | + monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1; |
| 193 | + } else { |
| 194 | + text.innerText += " You miss."; |
| 195 | + } |
| 196 | + healthText.innerText = health; |
| 197 | + monsterHealthText.innerText = monsterHealth; |
| 198 | + if (health <= 0) { |
| 199 | + lose(); |
| 200 | + } else if (monsterHealth <= 0) { |
| 201 | + if (fighting === 2) { |
| 202 | + winGame(); |
| 203 | + } else { |
| 204 | + defeatMonster(); |
| 205 | + } |
| 206 | + } |
| 207 | + if (Math.random() <= .1 && inventory.length !== 1) { |
| 208 | + text.innerText += " Your " + inventory.pop() + " breaks."; |
| 209 | + currentWeapon--; |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +function getMonsterAttackValue(level) { |
| 214 | + const hit = (level * 5) - (Math.floor(Math.random() * xp)); |
| 215 | + console.log(hit); |
| 216 | + return hit > 0 ? hit : 0; |
| 217 | +} |
| 218 | + |
| 219 | +function isMonsterHit() { |
| 220 | + return Math.random() > .2 || health < 20; |
| 221 | +} |
| 222 | + |
| 223 | +function dodge() { |
| 224 | + text.innerText = "You dodge the attack from the " + monsters[fighting].name; |
| 225 | +} |
| 226 | + |
| 227 | +function defeatMonster() { |
| 228 | + gold += Math.floor(monsters[fighting].level * 6.7); |
| 229 | + xp += monsters[fighting].level; |
| 230 | + goldText.innerText = gold; |
| 231 | + xpText.innerText = xp; |
| 232 | + update(locations[4]); |
| 233 | +} |
| 234 | + |
| 235 | +function lose() { |
| 236 | + update(locations[5]); |
| 237 | +} |
| 238 | + |
| 239 | +function winGame() { |
| 240 | + update(locations[6]); |
| 241 | +} |
| 242 | + |
| 243 | +function restart() { |
| 244 | + xp = 0; |
| 245 | + health = 100; |
| 246 | + gold = 50; |
| 247 | + currentWeapon = 0; |
| 248 | + inventory = ["stick"]; |
| 249 | + goldText.innerText = gold; |
| 250 | + healthText.innerText = health; |
| 251 | + xpText.innerText = xp; |
| 252 | + goTown(); |
| 253 | +} |
| 254 | + |
| 255 | +function easterEgg() { |
| 256 | + update(locations[7]); |
| 257 | +} |
| 258 | + |
| 259 | +function pickTwo() { |
| 260 | + pick(2); |
| 261 | +} |
| 262 | + |
| 263 | +function pickEight() { |
| 264 | + pick(8); |
| 265 | +} |
| 266 | + |
| 267 | +function pick(guess) { |
| 268 | + const numbers = []; |
| 269 | + while (numbers.length < 10) { |
| 270 | + numbers.push(Math.floor(Math.random() * 11)); |
| 271 | + } |
| 272 | + text.innerText = "You picked " + guess + ". Here are the random numbers:\n"; |
| 273 | + for (let i = 0; i < 10; i++) { |
| 274 | + text.innerText += numbers[i] + "\n"; |
| 275 | + } |
| 276 | + if (numbers.includes(guess)) { |
| 277 | + text.innerText += "Right! You win 20 gold!"; |
| 278 | + gold += 20; |
| 279 | + goldText.innerText = gold; |
| 280 | + } else { |
| 281 | + text.innerText += "Wrong! You lose 10 health!"; |
| 282 | + health -= 10; |
| 283 | + healthText.innerText = health; |
| 284 | + if (health <= 0) { |
| 285 | + lose(); |
| 286 | + } |
| 287 | + } |
| 288 | +} |
0 commit comments