Code: Select all
default roll_history = []
default dice_type = 6
default dice_count = 1
default modifier = 0
default result = 0
init python:
import json
import random
# Function for rolling dice
def roll_dice(dice_type, dice_count, modifier):
# access result variable from renpy
global result
# then update it with new roll
result = sum(random.randint(1, dice_type) for _ in range(dice_count)) + modifier
save_roll(dice_type, dice_count, modifier, result)
# Function for saving the results of a roll to history
def save_roll(dice_type, dice_count, modifier, result):
roll_history.append({
"dice_type": dice_type,
"dice_count": dice_count,
"modifier": modifier,
"result": result
})
with open("roll_history.json", "w") as f:
json.dump(roll_history, f)
# Dice roll screen
screen roll_dice_screen:
default txt_to_show = "No previous rolls."
vbox:
xalign 0.5
yalign 0.5
spacing 10
# Select a die type
text "Select dice type:"
hbox:
spacing 10
textbutton "d4" action SetVariable("dice_type", 4)
textbutton "d6" action SetVariable("dice_type", 6)
textbutton "d8" action SetVariable("dice_type", 8)
textbutton "d10" action SetVariable("dice_type", 10)
textbutton "d12" action SetVariable("dice_type", 12)
textbutton "d20" action SetVariable("dice_type", 20)
textbutton "d100" action SetVariable("dice_type", 100)
# Select the number of dice
text "Select the number of dice:"
hbox:
spacing 10
textbutton "-1" action If(dice_count > 1, SetVariable("dice_count", dice_count - 1))
text "[dice_count]d[dice_type]"
textbutton "+1" action SetVariable("dice_count", dice_count + 1)
# Select modifier
text "Select modifier:"
hbox:
spacing 10
textbutton "-1" action SetVariable("modifier", modifier - 1)
text "[modifier]"
textbutton "+1" action SetVariable("modifier", modifier + 1)
# Dice roll button
textbutton "Roll" action [
Function(roll_dice, dice_type, dice_count, modifier),
] xalign 0.5 yalign 0.9
if result:
text "Result: [result]" xalign 0.5 yalign 0.5
if roll_history:
$ txt_to_show = "Your last roll: {dice_count}d{dice_type}+{modifier} = {result}".format(**roll_history[-1])
text txt_to_show
label start:
# Attempt to load the roll history from a file
python:
try:
with open("roll_history.json", "r") as f:
roll_history = json.load(f)
except FileNotFoundError:
pass
call screen roll_dice_screen
return