I programmed this at school today, still not finished, still doesn't work right, still trying

Started by
2 comments, last by Clockworking 9 years, 3 months ago

I wrote some code up today between classes, it's the start of a text RPG game (Or maybe some type of software client? I don't know yet) and right now the code is messy because i'm too busy manually checking other stuff and swapping out code to get language file loading to work right. It's becoming a nightmare to work on using the Chromebook I have to use. Maybe pulling it up in Pycharm at home will allow me to fix it up and make it pretty? tongue.png

Essentially i'm using the info command in sys_control to load different data, and python doesn't like loading values from files to lists for some reason.

(If anyone knows how I can get this working, please let me know, I have been working on that for the past two hours.)


import fcntl
import termios
import struct
import os
import platform
import time

# Things such as error handling.
class system_control:
    @staticmethod
    def error(id = 0):
        if isinstance(id, int) is True:
            try:
                print system_var.errors[id + 1][0]
                if system_var.errors[id + 1][1] is True:
                   exit()
            except IndexError:
                print system_var.errors[0][0]
                exit()
        else:
            print system_var.errors[0][0]
            exit()
        
        
# A console formating class, makes the game look pretty.
class console:
    @staticmethod
    # A function retrieved from http://stackoverflow.com/questions/566746/
    # how-to-get-console-window-width-in-python. I'll try my best to document the 
    # code for the code, i renamed it to getdimensions
    def getdimensions(dimension):
        env = os.environ
        def ioctl_GWINSZ(fd):
            try:
                cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
            '1234'))
            except:
                return
            return cr
        cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
        if not cr:
            try:
                fd = os.open(os.ctermid(), os.O_RDONLY)
                cr = ioctl_GWINSZ(fd)
                os.close(fd)
            except:
                pass
        if not cr:
            cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
    
            ### Use get(key[, default]) instead of a try/catch
            #try:
            #    cr = (env['LINES'], env['COLUMNS'])
            #except:
            #    cr = (25, 80)
        if dimension.lower() == 'width' or 'x':
            return int(cr[1])
        elif dimension.lower() == 'length' or 'y':
            return int(cr[2])
        else:
            return int(cr[1]), int(cr[0])
    
    # Centers text
    @staticmethod
    def centered(text):
        # Get the whole screen width and convert it to half of that.
        difference = console.getdimensions('x') / 2
        # subtract new width by length of string to get the centering value.
        difference = difference - len(text)
        # return the input, centering it using the new width.
        return text.center(difference, ' ')
        
    # Clears the screen
    @staticmethod
    def clear():
        if system_var.osplat == 'Linux':
            os.system('clear')
        elif system_var.osplat == 'Windows':
            os.system('cls')
        else:
            system_control.error(1)


# A data managing class to make things easier.
class filemanager:
    #How we manage our files. Use appropriate extension in selection.
    @staticmethod
    def info(selection, mode, data = ['NONE']):
        # We first enter the right mode.
        if mode == 'read':
            #Open the selected file
            data1 = open(selection, 'r')
            # Check which file is being manipulated.
            if selection == 'userdata.cfg':
                try:
                    system_var.username = data1.readline() # Username.
                    system_var.password = data1.readline() # Password.
                except IOError:
                    system_control.error(4)
            elif selection == 'config.cfg':
                try:
                    system_var.language = data1.readline(1) # Language Selection.
                except IOError:
                    system_control.error(4)
            elif selection == 'eng1.lang' or 'eng2.lang':
                if selection == 'eng1.lang':
                    try:
                        for line in data1.readlines():
                            system_var.texts_one.append(line.strip())
                    except IOError:
                        system_control.error(4)
                else:
                    try:
                        system_var.texts_two = data1.read()
                    except IOError:
                        system_control.error(4)
            else:
                system_control.error(3)
        elif mode == 'write':
            data1 = open(selection, 'r+')
            if selection == 'userdata.cfg':
                data1.write(system_var.usrname + '\n' + system_var.passwrd)
        else:
            system_control.error(0)
        data1.close()


# Variables.
class system_var:
    usrname = 'NAN'
    username = 'NAN'
    passwrd = 'NAN'
    password = 'NAN'
    langugages = ['english']
    language_selection = 'english'
    
    osname = os.name
    osplat = platform.system()
    
    errors = [
        ['ERROR: Invalid ID reported to error handler. (00-00)', True],
        ['ERROR: Invalid mode for the data manager. (00-01)', True],
        ['ERROR: Operating System is not supported. (00-02)', True],
        ['ERROR: Invalid language setting, assuming language to be English. (00-03)', False],
        ['ERROR: Terminating for safety, an invalid file selection (00-04)', False],
        ['ERROR: Unable to continue, a file is missing! (00-05)', True]
    ]
    
    texts_one = []


# Initial class for game.
class startgame:
    
    @staticmethod
    def login():
        filemanager.info('userdata.cfg', 'read')
        print console.centered(system_var.texts_one[0])
        if len(system_var.username) > 0:
            system_var.usrname = raw_input(system_var.texts_one[2])
            system_var.passwrd = raw_input(system_var.texts_one[4])
            if system_var.usrname == system_var.username:
                if system_var.passwrd == system_var.password:
                    startgame.load()
                else:
                    print "Username and/or password are incorrect"
                    startgame.login()
            else:
                print "Username and/or password are incorrect"
                startgame.login()
        else:
            system_var.usrname = raw_input(system_var.texts_one[1])
            system_var.passwrd = raw_input(system_var.texts_one[3])
            filemanager.info('userdata.cfg', 'write')
            
    @staticmethod
    def load():
            
        if system_var.language_selection == 'english':
            print 'Loading game, please wait...'
            languagecache = str(system_var.language_selection) + '1.lang'
            print 'loading ' + languagecache
            filemanager.info(languagecache, 'read')
        else:
            system_control.error(2)
            system_var.language_selection = 'english'
            startgame.load()
        startgame.login()
    
    @staticmethod
    def __init__():
        print "Operating System: " + str(system_var.osplat) + " (" + str(system_var.osname) + ")"
        if system_var.osplat == 'Linux' or 'Windows':
            print "Operating System verified as supported."
            time.sleep(3)
            console.clear()
            startgame.load()
        else:
            system_control.error(1)
        

startgame.__init__() 
Advertisement

You were just missing a bunch of parentheses, really ohmy.png

I didn't even have to check a man page to fix it ( and it's not 'cause I'm special ;p )

Am I missing something?
Runs fine for me with a touch'd english1.lang.

Sorry about the double newlines, the tag won't work with me.

[source='python']import fcntl
import termios
import struct
import os
import platform
import time

# Things such as error handling.
class system_control:
@staticmethod
def error(id = 0):
if isinstance(id, int) is True:
try:
print (system_var.errors[id + 1][0])
if system_var.errors[id + 1][1] is True:
exit()
except IndexError:
print (system_var.errors[0][0])
exit()
else:
print (system_var.errors[0][0])
exit()
# A console formating class, makes the game look pretty.
class console:
@staticmethod
# A function retrieved from http://stackoverflow.com/questions/566746/
# how-to-get-console-window-width-in-python. I'll try my best to document the
# code for the code, i renamed it to getdimensions
def getdimensions(dimension):
env = os.environ
def ioctl_GWINSZ(fd):
try:
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
### Use get(key[, default]) instead of a try/catch
#try:
# cr = (env['LINES'], env['COLUMNS'])
#except:
# cr = (25, 80)
if dimension.lower() == 'width' or 'x':
return int(cr[1])
elif dimension.lower() == 'length' or 'y':
return int(cr[2])
else:
return int(cr[1]), int(cr[0])
# Centers text
@staticmethod
def centered(text):
# Get the whole screen width and convert it to half of that.
difference = console.getdimensions('x') / 2
# subtract new width by length of string to get the centering value.
difference = difference - len(text)
# return the input, centering it using the new width.
return text.center(difference, ' ')
# Clears the screen
@staticmethod
def clear():
if system_var.osplat == 'Linux':
os.system('clear')
elif system_var.osplat == 'Windows':
os.system('cls')
else:
system_control.error(1)


# A data managing class to make things easier.
class filemanager:
#How we manage our files. Use appropriate extension in selection.
@staticmethod
def info(selection, mode, data = ['NONE']):
# We first enter the right mode.
if mode == 'read':
#Open the selected file
data1 = open(selection, 'r')
# Check which file is being manipulated.
if selection == 'userdata.cfg':
try:
system_var.username = data1.readline() # Username.
system_var.password = data1.readline() # Password.
except IOError:
system_control.error(4)
elif selection == 'config.cfg':
try:
system_var.language = data1.readline(1) # Language Selection.
except IOError:
system_control.error(4)
elif selection == 'eng1.lang' or 'eng2.lang':
if selection == 'eng1.lang':
try:
for line in data1.readlines():
system_var.texts_one.append(line.strip())
except IOError:
system_control.error(4)
else:
try:
system_var.texts_two = data1.read()
except IOError:
system_control.error(4)
else:
system_control.error(3)
elif mode == 'write':
data1 = open(selection, 'r+')
if selection == 'userdata.cfg':
data1.write(system_var.usrname + '\n' + system_var.passwrd)
else:
system_control.error(0)
data1.close()


# Variables.
class system_var:
usrname = 'NAN'
username = 'NAN'
passwrd = 'NAN'
password = 'NAN'
langugages = ['english']
language_selection = 'english'
osname = os.name
osplat = platform.system()
errors = [
['ERROR: Invalid ID reported to error handler. (00-00)', True],
['ERROR: Invalid mode for the data manager. (00-01)', True],
['ERROR: Operating System is not supported. (00-02)', True],
['ERROR: Invalid language setting, assuming language to be English. (00-03)', False],
['ERROR: Terminating for safety, an invalid file selection (00-04)', False],
['ERROR: Unable to continue, a file is missing! (00-05)', True]
]
texts_one = []


# Initial class for game.
class startgame:
@staticmethod
def login():
filemanager.info('userdata.cfg', 'read')
print (console.centered(system_var.texts_one[0]))
if len(system_var.username) > 0:
system_var.usrname = raw_input(system_var.texts_one[2])
system_var.passwrd = raw_input(system_var.texts_one[4])
if system_var.usrname == system_var.username:
if system_var.passwrd == system_var.password:
startgame.load()
else:
print ("Username and/or password are incorrect")
startgame.login()
else:
print ("Username and/or password are incorrect")
startgame.login()
else:
system_var.usrname = raw_input(system_var.texts_one[1])
system_var.passwrd = raw_input(system_var.texts_one[3])
filemanager.info('userdata.cfg', 'write')
@staticmethod
def load():
if system_var.language_selection == 'english':
print ('Loading game, please wait...')
languagecache = str(system_var.language_selection) + '1.lang'
print ('loading ' + languagecache)
filemanager.info(languagecache, 'read')
else:
system_control.error(2)
system_var.language_selection = 'english'
startgame.load()
startgame.login()
@staticmethod
def __init__():
print ("Operating System: " + str(system_var.osplat) + " (" + str(system_var.osname) + ")")
if system_var.osplat == 'Linux' or 'Windows':
print ("Operating System verified as supported.")
time.sleep(3)
console.clear()
startgame.load()
else:
system_control.error(1)

startgame.__init__()

[/source]

This isn't meant to be offensive at all but i'm just laughing so hard when you said "I've been working on this for the past two hours"

Ohhhhh, okay. I was taught not to use the parenthesis on print statements (did I overlook something else?) , i'll take note of that.

EDIT: Parenthesis didn't do anything, it's still refusing to load the data to system_var.texts_one

ANOTHER_EDIT: Actually, I found the issue, it turns out I never set up the language controller to accept the new name of english1.lang instead of the old eng1.lang. tongue.png

I can't believe it took me this long to realize there was a difference in the filenames.


class filemanager:
    #How we manage our files. Use appropriate extension in selection.
    @staticmethod
    def info(selection, mode, data = ['NONE']):
        # We first enter the right mode.
        if mode == 'read':
            #Open the selected file
            data1 = open(selection, 'r')
            # Check which file is being manipulated.
            if selection == 'userdata.cfg':
                try:
                    system_var.username = data1.readline() # Username.
                    system_var.password = data1.readline() # Password.
                except IOError:
                    exit()
            elif selection == 'config.cfg':
                try:
                    system_var.language = data1.readline(1) # Language Selection.
                except IOError:
                    exit()
            elif selection == 'eng1.lang' or 'eng2.lang':
                if selection == 'eng1.lang':
                    try:
                        for line in data1.readlines():
                           system_var.texts_one.append(line.strip(line))
                    except IOError:
                        exit()
                else:
                    try:
                        system_var.texts_two = data1.read()
                    except IOError:
                        exit()
            else:
                exit()
        elif mode == 'write':
            data1 = open(selection, 'r+')
            if selection == 'userdata.cfg':
                data1.write(system_var.usrname + '\n' + system_var.passwrd)
        else:
            exit()
        data1.close()

# Variables.
class system_var:
    usrname = 'NAN'
    username = 'NAN'
    passwrd = 'NAN'
    password = 'NAN'
   
    langugages = ['english']
    language_selection = 'english'
     
    osname = os.name
    osplat = platform.system()
   
    texts_one = []

filemanager.info('english.lang', 'read')
print (system_var.texts_one[0])

This topic is closed to new replies.

Advertisement