Orbital Cookies, Read from a text file tutorial (from beginner dev)


So, like, you've got a text game.

It needs an easier way to maintain large amount of text, storing it in code has it's downsides.

Here's how to do so by importing one character at a time, in python. Made for a visual novel game using pygame. Also works on python2. 


#Imports io component, in order to load in files -

#On python 2x.

import io

#For python3 to not throw errors -

#On python2 unicode() code.

#Stackoverflow sure helped -

#With this one.

if float(sys.version_info[0]) > 2:

    #Should show the major version 3.

    #print("Python version", sys.version_info[0])

    unicode = str


#A function to read a particular string -

#By looking for an identifying substring,

#Adding to the returned string -

#One character at a time.

#Variables are the substring to search from,

#And the location of the source txt file.

def sugarcube_melting(sugar_type, sugar_bag):

    #More suited for python 2x,

    #That with the io include.

    with io.open(sugar_bag, 'r', encoding='utf-8') as sugarcube:

        #Establishes a dictionary, that holds the needed -

        #Variables, for interfacing upwards.

        sugarcube_melted = {}

        #Whether looking for a locate substring,

        #In which case it's 'finger dipping',

        #Or looking for a target substring,

        #Meaning 'scattering sugar all over'.

        #'salivating' is more of a debug type.

        sugarcube_melted["action"] = u"salivating"

        #Text of the targeted substring.

        sugarcube_melted["text"] = u""

        #Text of the locate-type substring.

        sugarcube_melted["sugar type"] = u""

        #A letter iterated over from source text.

        sugarcube_melted["letter"] = u""

        #Loop start.

        while True:

            #Assignings each next character to this cointainer.

            sugarcube_melted["letter"] = unicode(sugarcube.read(1))

            

            #If it's a locate-type centric -

            #And it ain't the curly brackets,

            #Then add to locate-type string.

            if sugarcube_melted["action"] == "finger dipping" and sugarcube_melted["letter"] != '{' and sugarcube_melted["letter"] != '}':

                sugarcube_melted["sugar type"] = unicode(sugarcube_melted["sugar type"]) + unicode(sugarcube_melted["letter"])

                #print("Sugar Bag" + unicode(sugarcube_melted["sugar type"]))

                #To be enabled to debug.

            #If it's a desired-text centric, 

            #And it ain't the square brackets,

            #Then add the character to the -

            #desired-text string object.

            if sugarcube_melted["action"] == "scattering sugar all over" and sugarcube_melted["letter"] != '[' and sugarcube_melted["letter"] != ']':

                sugarcube_melted["text"] = unicode(sugarcube_melted["text"]) + unicode(sugarcube_melted["letter"])

            #If it's looking for a target text,

            #And the character is a closed square bracket,

            #Then end the loop,

            #Since the end of the intended text sentence

            #Was reached.        

            if sugarcube_melted["action"] == "scattering sugar all over" and sugarcube_melted['letter'] == ']':

                break

            #Looks for an opening curly bracket,

            #To set the mode to locate-type.

            if sugarcube_melted["letter"] == '{':

                sugarcube_melted["action"] = "finger dipping"

            #If it's the closing curly bracket,

            #Compares the locate-type string with

            #The function variable, and if equal,

            #Starts the target-type mode.

            if sugarcube_melted["letter"] == "}":

                if sugarcube_melted["sugar type"] == sugar_type:

                    sugarcube_melted["action"] = "scattering sugar all over"

                #If the strings don't match however,

                #The locate-type string is reset,

                #And the mode is set to debug.

                if sugarcube_melted["sugar type"] != sugar_type:

                    sugarcube_melted["sugar type"] = ""

                    sugarcube_melted["action"] = "salivating"

    #Strips any errant spaces from the target-type string.

    #sugarcube_melted["text"] = sugarcube_melted["text"].strip()

    #Prints the desired string out.

    #print(sugarcube_melted["text"])

    

    return(sugarcube_melted["text"]


Otherwise, have at here the art of a character, guild worker Yooaie from the first game of mine,  sunset floors, maybe being just as surprised as Pershea, from Orbital Cookies (the game jam one), at being put next to each other.

Space fantasy pirates and high fantasy medieval-adjacent, after all.



Get Orbital Cookies

Leave a comment

Log in with itch.io to leave a comment.