Snippets

SinnyStar [Ren'Py] Place selection

Created by J. Tran

File screens.rpy Added

  • Ignore whitespace
  • Hide word diff
+init python:
+
+    # Add a place to the list
+    def add_place(place_list, place_name, place_actions=[], index=0):
+        #place_list.insert(index, (place_name, place_actions))  # adds it ot front of list by default
+        place_list.append((place_name, place_actions))
+        return place_list
+
+    # Removes a place from the list by name
+    # http://stackoverflow.com/a/22825938
+    def remove_place(place_list, place_name):
+        return filter(lambda x: x[0] != place_name, place_list)
+
+label place:
+    show screen places(place_items) 
+    $ renpy.pause() # prevent seeing the next line
+
+screen places(place_items):
+
+    modal True
+
+    ### Note to self:
+    ### items[page_curr] is the current choice to display
+    ### remember it's a tuple of (caption, action, chosen)
+
+    ### Python lists are 0-based
+    default page_curr = 0
+    default page_min = 0
+    default page_max = len(place_items) -1
+                        
+    frame:
+        xalign 0.5 yalign 0.5
+        hbox:
+        
+            ### Simple textbuttons
+            # textbutton "<" action [If(page_curr > page_min, [SetScreenVariable('page_curr', page_curr - 1)])]
+            
+            # if items[page_curr][1]:
+                # textbutton place_items[page_curr][0] action [Hide("places"), place_items[page_curr][1]]
+            # else:
+                # text place_items[page_curr][0]
+                
+            # textbutton ">" action [If(page_curr < page_max, [SetScreenVariable('page_curr', page_curr + 1)])]
+
+
+            ### With imagebuttons
+            ### Example: for caption "park", place thumbnail images are:
+            ### "images/place_park_idle.png"
+            ### "images/place_park_hover.png"
+            ### etc.
+            imagebutton auto "images/arrowl_%s.png" action [If(page_curr > page_min, [SetScreenVariable('page_curr', page_curr - 1)])]
+            
+            if place_items[page_curr][1]:
+                imagebutton auto "images/place_" + place_items[page_curr][0] + "_%s.png" action [Hide("places"),place_items[page_curr][1]]
+            else:
+                imagebutton auto "images/place_" + place_items[page_curr][0] + "_%s.png" action NullAction()
+                
+            imagebutton auto "images/arrowr_%s.png" action [If(page_curr < page_max, [SetScreenVariable('page_curr', page_curr + 1)])]
+
+    ### Current caption
+    frame:
+        xalign 0.5 yalign 0.8
+        text place_items[page_curr][0]
+    
+    ### To indicate page number
+    frame:
+        xalign 0.5 yalign 0.9
+        text "Current Page: "+str(page_curr +1) # +1 because we like pages starting at 1

File script.rpy Added

  • Ignore whitespace
  • Hide word diff
+# Variables
+define energy = 100
+define place_items = [ ("park", [SetVariable("energy", energy - 10), Jump("place1")]), ("home", Jump("place2")) ]
+
+# The game starts here.
+label start:
+    jump place_select
+    
+label place_select:
+    "I have [energy] energy."
+    "Where should I go?"
+    call place
+    return
+            
+label place1:
+    "Going to the park!"
+    "I found a mysterious tunnel."
+
+    # edit the place_items to remove park and add tunnel
+    $ place_items = remove_place(place_items, "park")
+    $ place_items = add_place(place_items, "tunnel", [SetVariable("energy", energy - 20), Jump("place3")])
+
+    jump place_select
+    
+label place2:
+    "I'm tired, let's go home."
+    "I have [energy] energy."
+    return
+    
+label place3:
+    "Entering the tunnel - it's deeper than I thought..."
+    jump place_select
HTTPS SSH

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