I’m currently working on an AppleScript text adventure game using “display dialog”, and recently came up with a simple battle system. The script basically defines properties for the player’s HP and the monster’s HP (in this case a goblin). It then calculates damage output to both you and the monster using “random number”.
I’m thinking of having different weapons with different damage outputs (e.g. a dagger would deal damage between 1–10, a longsword would deal damage between 10–20, etc.). Further customization is very possible.
Any input on the code is most welcome. I hope someone finds this useful!
property yourHP : 20
property goblinHP : 20
property yourHPHurt : 0
property enemyHPHurt : 0
on loseGoblin()
display dialog "The goblin hits you with a mortal wound. You die..." & return & return & "Your HP: " & yourHPHurt & "/" & yourHP & return & "Enemy HP :" & enemyHPHurt & "/" & goblinHP buttons {"Game over..."} default button 1
set yourHPHurt to yourHP
set enemyHPHurt to goblinHP
end loseGoblin
on winGoblin()
display dialog "You hit the goblin with a mortal wound. It dies..." & return & return & "Your HP: " & yourHPHurt & "/" & yourHP & return & "Enemy HP :" & enemyHPHurt & "/" & goblinHP buttons {"Victory!"} default button 1
set yourHPHurt to yourHP
set enemyHPHurt to goblinHP
end winGoblin
on tieGoblin()
display dialog "You hit the goblin with a mortal wound, but before it succumbs, it manages to counterattack and kill you. You both die..." & return & return & "Your HP: " & yourHPHurt & "/" & yourHP & return & "Enemy HP :" & enemyHPHurt & "/" & goblinHP buttons {"Game over..."} default button 1
set yourHPHurt to yourHP
set enemyHPHurt to goblinHP
end tieGoblin
on combatGoblin()
set damageDoneToEnemy to (random number from 1 to 10)
set damageDoneToYou to (random number from 1 to 10)
if yourHPHurt = yourHP and enemyHPHurt = goblinHP then -- beginning of battle
set enemyHPHurt to goblinHP - damageDoneToEnemy
set yourHPHurt to yourHP - damageDoneToYou
display dialog "You hit the goblin for " & damageDoneToEnemy & " damage! The goblin hits you for " & damageDoneToYou & " damage!" & return & return & "Your HP: " & yourHPHurt & "/" & yourHP & return & "Enemy HP :" & enemyHPHurt & "/" & goblinHP buttons {"Flee", "Attack!"} default button 2
if button returned of the result is "Attack!" then
combatGoblin()
else -- reset properties
set yourHPHurt to yourHP
set enemyHPHurt to goblinHP
end if
else if yourHPHurt ≠ yourHP and enemyHPHurt ≠ goblinHP then -- damage has been done
set enemyHPHurt to enemyHPHurt - damageDoneToEnemy
set yourHPHurt to yourHPHurt - damageDoneToYou
if yourHPHurt ≤ 0 and enemyHPHurt > 0 then
loseGoblin()
else if yourHPHurt > 0 and enemyHPHurt ≤ 0 then
winGoblin()
else if yourHPHurt ≤ 0 and enemyHPHurt ≤ 0 then
tieGoblin()
else if yourHPHurt > 0 and enemyHPHurt > 0 then
display dialog "You hit the goblin for " & damageDoneToEnemy & " damage! The goblin hits you for " & damageDoneToYou & " damage!" & return & return & "Your HP: " & yourHPHurt & "/" & yourHP & return & "Enemy HP :" & enemyHPHurt & "/" & goblinHP buttons {"Flee", "Attack!"} default button 2
if button returned of the result is "Attack!" then
combatGoblin()
else -- reset properties
set yourHPHurt to yourHP
set enemyHPHurt to goblinHP
end if
end if
end if
end combatGoblin
on encounterGoblin()
display dialog "COMBAT!" & return & return & "You encounter a goblin. It wields a viscious-looking scimitar." & return & return & "Your HP: " & yourHP & "/" & yourHP & return & "Enemy HP: " & goblinHP & "/" & goblinHP buttons {"Flee", "Attack!"} default button 2 with icon 0
if button returned of the result is "Attack!" then
set yourHPHurt to yourHP
set enemyHPHurt to goblinHP
combatGoblin()
end if
end encounterGoblin
encounterGoblin()