scrimba
Build an RPG
Build a role-playing game part 3 - The gameplay
Back to one monster
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Back to one monster
Expand for more info
index.js
run
preview
console
import characterData from './data.js'
import Character from './Character.js'

/*
Challenge
1. See if you can get the app to work with just
one monster again.
**hint.md for help!!**
*/


let monstersArray = ["orc", "demon", "goblin"]

function getNewMonster() {
const nextMonsterData = characterData[monstersArray.shift()]
return nextMonsterData ? new Character(nextMonsterData) : {}
}

function attack() {
wizard.getDiceHtml()
orc.getDiceHtml()
wizard.takeDamage(orc.currentDiceScore)
orc.takeDamage(wizard.currentDiceScore)
render()

if(wizard.dead || orc.dead){
endGame()
}
}

function endGame() {
const endMessage = wizard.health === 0 && orc.health === 0 ?
"No victors - all creatures are dead" :
wizard.health > 0 ? "The Wizard Wins" :
"The Orc is Victorious"

const endEmoji = wizard.health > 0 ? "🔮" : "☠️"
document.body.innerHTML = `
<div class="end-game">
<h2>Game Over</h2>
<h3>${endMessage}</h3>
<p class="end-emoji">${endEmoji}</p>
</div>
`
}

document.getElementById("attack-button").addEventListener('click', attack)

function render() {
document.getElementById('hero').innerHTML = wizard.getCharacterHtml()
document.getElementById('monster').innerHTML = orc.getCharacterHtml()
}

const wizard = new Character(characterData.hero)
let monster = getNewMonster()
render()
Console
!
ReferenceError: orc is not defined
,
/index.html
-2:50