Snippets

SinnyStar [Ren'Py] Random selection of items from set

Created by Sinny Star
init python:

    # Start with empty set
    all_objects = set()        

    # Making 10 objects and adding to set of all_objects
    for i in range(0,10):
        all_objects.add('obj'+str(i))

    # Current objects
    curr_objects = set()

    # Random object function
    # Takes in the number of objects wanted in curr_objects
    import random
    def random_objects(num=1):
        global all_objects
        global curr_objects
        curr_objects = random.sample(all_objects, num)
            
# The game starts here.
label start:

    show screen debug
    "Start with 10 objects in total"
    
    $ random_objects(3)
    "3 random objects"
    
    $ random_objects(5)
    "5 random objects"

    # Check if obj1 and obj2 are in curr_objects
    if all(x in curr_objects for x in ["obj1", "obj2"]):
        "You have obj1 and obj2"
    elif "obj1" in curr_objects:
        "You have obj1"
    else:
        "You don't have obj1 at all"
    
    return

screen debug():
    hbox:
        frame:
            has vbox
            text "all_objects:"
            for obj in sorted(all_objects):
                text obj
        frame:
            has vbox
            text "curr_objects:"
            for obj in sorted(curr_objects):
                text obj

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.