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
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 766
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

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):
        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
Big thanks to discord man @djdrachenfels for fixing the bugs and explaining why you can't do that!
Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Sawa - a game of the Drow Nation

Honest Critique

Poses in visual novels, or how to hold a character properly in the frame

Help save articles to the webarchive. [/color]

Please save your projects on github, you would know how hard it is to find projects after 7 years...

Post Reply

Who is online

Users browsing this forum: No registered users