Code: Select all
init python:
import json
import random
# Dictionary for storing the results of rolls
roll_history = {}
# Function for rolling dice
def roll_dice(dice_type, dice_count, modifier):
result = 0
for _ in range(dice_count):
result += random.randint(1, dice_type)
result += modifier
return result
# Function for saving the results of a roll to history
def save_roll(dice_type, dice_count, modifier, result):
roll_id = len(roll_history) + 1
roll_history[roll_id] = {
“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:
# Variables to store the current values
$ dice_type = 6
$ dice_count = 1
$ modifier = 0
$ result = 0
vbox:
xalign 0.5
yalign 0.5
spacing 10
# Select a die type
text “Select cube 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. ###############EROR!
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”
textbutton “+1” action SetVariable(“dice_count”, dice_count + 1)
# Select modifier ###############EROR!
text “Select modifier:”
hbox:
spacing 10
textbutton “-1” action SetVariable(“modifier”, modifier - 1)
text “[modifier:+]” # Fixed modifier display
textbutton “+1” action SetVariable(“modifier”, modifier + 1)
# Dice roll button
textbutton “roll” action [
SetVariable(“result”, roll_dice(dice_type, dice_count, modifier)),
Function(save_roll, dice_type, dice_count, modifier, result),
Show(“roll_result_screen”, result=result)
] xalign 0.5 yalign 0.9
# Screen to display the result of the roll
screen roll_result_screen(result):
text “[result]” xalign 0.5 yalign 0.5
textbutton “OK” action Return() xalign 0.5 yalign 0.7
label start:
# Attempt to load the throw history from a file
python:
try:
with open(“roll_history.json”, “r”) as f:
roll_history = json.load(f)
except FileNotFoundError:
roll_history = {}
# Display the screen for rolling dice
call screen roll_dice_screen
# Display the result of the last roll
python:
if roll_history:
last_roll = roll_history[str(len(roll_history))]]
last_roll_str = f “Your last roll: {last_roll['dice_count']}d{last_roll['dice_type']}+{last_roll['modifier']} = {last_roll['result']}“”
else:
last_roll_str = “No previous rolls.”
“{last_roll_str}”
label show_old_roll:
# Loading the history of rolls
python:
With open(“roll_history.json”, “r”) as f:
roll_history = json.load(f)
# Select a roll (e.g., roll #2)
$ roll_id = 2
# Check if there is a roll with the given ID
if str(roll_id) in roll_history:
$ old_roll = roll_history[str(roll_id)]
“Roll #[roll_id]: [old_roll['dice_count']]d[old_roll['dice_type']]+[old_roll['modifier']] = [old_roll['result']]”
else:
“Roll #[roll_id] was not found.”
Code: Select all
# Select the number of dice. ###############EROR!
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”
textbutton “+1” action SetVariable(“dice_count”, dice_count + 1)
# Select modifier ###############EROR!
text “Select modifier:”
hbox:
spacing 10
textbutton “-1” action SetVariable(“modifier”, modifier - 1)
text “[modifier:+]” # Fixed modifier display
textbutton “+1” action SetVariable(“modifier”, modifier + 1)