Dice games like D&D and Fighting Fantasy were very popular when I was growing up, so I thought I'd try to make a text adventure in Python based on The Warlock of Firetop Mountain. Most of the game is based around dice rolls so it is pretty easy to replicate.
The interesting part is how to store and use the data effectively, and this ended up as a really useful exercise in lists, dictionaries, classes with Python. I have set up a project on Bitbucket with specific scripts for the game and the repo can be accessed here.
The function rollDice will print two numbers between 1 annd 6.
import random def rollDice(): print random.randint(1, 6) print random.randint(1, 6)
This became n number of dice, all D6. It uses diceInput as a range and rolls a D6 for every number in that range.
import random def chooseDice(): diceInput = input('How many dice? 1-n: ') for i in range(diceInput): print random.randint(1,6) raw_input('press ENTER')
And that became an input for n dice with n sides.
import random def nDiceSides(): diceInput = input('How many dice? ') sideInput = input('How many sides for each dice? ') for i in range(diceInput): print random.randint(1,sideInput) raw_input('press ENTER')
I wouldn't include import random with every function. It's only bc I tested these as individual scripts.
The two D6 roll can be modified to give min/max settings for the number of sides, and to sit inside a while loop so it keeps prompting after every roll.
def rollDiceLoop(): min = 1 max = 6 rollEm = raw_input("Roll two D6? Y/N: ") while rollEm == "y" or rollEm == "Y": print random.randint(min, max) print random.randint(min, max) rollEm = raw_input("Roll two D6? Y/N: ") else: print ('Nothing rolled.') raw_input('press ENTER')
One and two D6 will be fine for a text game, but to turn them into something useful they need to be modified slightly. Print is OK to see the result, but the function should return the value of the roll. With two D6, the total of the two rolls should be returned.
def rollDie(): roll = random.randint(1, 6) return roll def rollDice(): rollOne = random.randint(1, 6) rollTwo = random.randint(1, 6) return rollOne + rollTwo
Fighting Fantasy uses attributes of SKILL, STAMINA and LUCK in a dice-based combat system. This can be setup in a simple simple script with two entities, the player and the enemy. Both have SKILL and STAMINA attributes determined with D6 rolls. In combat, the player rolls a D6 and adds it to their skill. This is their attack strength. Then player then does the same for the enemy. The greater attack strength wins, and 1 point is subtracted from the loser's STAMINA until it reaches 0 and someone is dead.
import random def rollDie(): roll = random.randint(1,6) return roll raw_input('Press ENTER to determine levels... ') pSkill = rollDie() eSkill = rollDie() pStamina = rollDie() eStamina = rollDie() print print 'Player skill = ', pSkill print 'Player stamina = ', pStamina print 'Enemy skill = ', eSkill print 'Enemy stamina = ', eStamina print print 'FIGHT!' combat = 1 while combat == 1: print raw_input('Press ENTER to attack... ') print pAttack = pSkill + rollDie() eAttack = eSkill + rollDie() print 'player attack = ', pSkill, '+', pAttack-pSkill, '=', pAttack print 'enemy attack = ', eSkill, '+', eAttack-eSkill, '=', eAttack if pAttack > eAttack: eStamina -= 1 print 'YOU WOUNDED IT' else: pStamina -=1 print 'YOU GOT HIT' print 'Player stamina = ', pStamina print 'Enemy stamina = ', eStamina if eStamina == 0: print print 'VICTORY!' exit() elif pStamina == 0: print print 'YOU ARE DEAD' exit()
The initial values are set up before the combat, and then it's a simple while loop to simulte the rolling of dice for each round of combat. The loop continues until the stamina of either player or enemy reaches 0.