Dnd calculator on renpy

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
User avatar
Miko-Class Veteran
Posts: 738
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia

Dnd calculator on renpy

#1 Post by Andredron »

- The video has a buggy version, already fixed.

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):
            "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."

        xalign 0.5
        yalign 0.5
        spacing 10

        # Select a die type
        text "Select dice type:"
            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:"
            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:"
            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
            with open("roll_history.json", "r") as f:
                roll_history = json.load(f)
        except FileNotFoundError:

    call screen roll_dice_screen

Big thanks to discord man @djdrachenfels for fixing the bugs and explaining why you can't do that!

Post Reply

Who is online

Users browsing this forum: No registered users