# # options.py # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Copyright # Author: Nicholas F. Hoover # Contributors: # Created: 2007.07.17 # Last Modified: 2007.07.31 # import pygame class Options(object): ############ # CONTROLS # ############ CONTROLS = {'ENTER': pygame.K_RETURN, 'QUIT': pygame.K_ESCAPE, 'ADD_KID': pygame.K_k, 'FPS_TOGGLE': pygame.K_g, 'PAUSE': pygame.K_p, 'MUSIC_TOGGLE': pygame.K_m, 'UP': pygame.K_UP, 'DOWN': pygame.K_DOWN, 'LEFT': pygame.K_LEFT, 'RIGHT': pygame.K_RIGHT, 'PUNCH': pygame.K_s, 'KICK': pygame.K_d, 'EAT': pygame.K_SPACE, 'FLAME': pygame.K_a, 'GAS': pygame.K_f, 'PICKUP': pygame.K_w, 'SUBMENU_TOGGLE':pygame.K_LCTRL, } ################# # SOUND OPTIONS # ################# SOUND = {'MUSIC_ON': True, 'SFX_ON': True, 'MUSIC_VOL': 1.0, 'SFX_VOL': 0.5, 'USE_OGG': True, 'USE_MIDI': False, } ################# # VIDEO OPTIONS # ################# VIDEO = {'FULLSCREEN': False, 'RESOLUTION': (800,600), } def __init__(self, filename): self.filename = filename def setFullscreen(self, value): if self.VIDEO['FULLSCREEN'] and not value: pygame.display.set_mode(self.VIDEO['RESOLUTION']) elif not self.VIDEO['FULLSCREEN'] and value: pygame.display.set_mode(self.VIDEO['RESOLUTION'], pygame.FULLSCREEN) self.VIDEO['FULLSCREEN'] = value def setMusicOn(self, value): if self.SOUND['MUSIC_ON'] and not value: pygame.mixer.music.pause() elif not self.SOUND['MUSIC_ON'] and value: pygame.mixer.music.unpause() self.SOUND['MUSIC_ON'] = value def readOptionsFile(self): #fixme add exception handling optionsFile = open(self.filename,'r') lines = [] while True: line = optionsFile.readline() if line == '': #EOF break elif line[0] == '#' or line[0] == '\n' or line[0] == '\r': #ignore blank and comment lines pass else: splits = line.split('#') #remove comments cleanLine = splits[0] lines.append(cleanLine) optionsFile.close() for line in lines: (option, value) = line.split('=') option = option.strip() value = value.strip() if Options.CONTROLS.has_key(option): if len(value) == 1: value = value.lower() else: value = value.upper() if Options.KEY_TABLE.has_key(value): Options.CONTROLS[option] = Options.KEY_TABLE[value] else: print 'Could not find key:', value, 'assigned to control option:', option, '\n\tUsing default value.' elif Options.SOUND.has_key(option): if value == 'True': value = True elif value == 'False': value = False else: value = float(value) Options.SOUND[option] = value elif Options.VIDEO.has_key(option): if value == 'True': value = True elif value == 'False': value = False elif option == 'RESOLUTION': value = value.strip('()') (x,y) = value.split(',') value = (int(x), int(y)) Options.VIDEO[option] = value else: print 'Could not find option table for option:', option def writeOptionsFile(self): #FIXME needs exception handling optionsFile = open(self.filename,'w') optionsFile.write('############\n# CONTROLS #\n############\n') for key in Options.CONTROLS.keys(): if len(key) >= 8: connectString = '\t= ' else: connectString = '\t\t= ' optionsFile.write(key + connectString + (pygame.key.name(Options.CONTROLS[key])).upper() + '\n') optionsFile.write('\n#########\n# SOUND #\n#########\n') for key in Options.SOUND.keys(): if len(key) >= 8: connectString = '\t= ' else: connectString = '\t\t= ' optionsFile.write(key + connectString + str(Options.SOUND[key]) + '\n') optionsFile.write('\n#########\n# VIDEO #\n#########\n') for key in Options.VIDEO.keys(): if len(key) >= 8: connectString = '\t= ' else: connectString = '\t\t= ' optionsFile.write(key + connectString + str(Options.VIDEO[key])+ '\n') KEY_TABLE = { 'BACKSPACE': pygame.K_BACKSPACE, 'TAB': pygame.K_TAB, 'CLEAR': pygame.K_CLEAR, 'RETURN': pygame.K_RETURN, 'PAUSE': pygame.K_PAUSE, 'ESCAPE': pygame.K_ESCAPE, 'SPACE': pygame.K_SPACE, 'EXCLAIM': pygame.K_EXCLAIM, 'QUOTEDBL': pygame.K_QUOTEDBL, 'HASH': pygame.K_HASH, 'DOLLAR': pygame.K_DOLLAR, 'AMPERSAND': pygame.K_AMPERSAND, 'QUOTE': pygame.K_QUOTE, 'LEFTPAREN': pygame.K_LEFTPAREN, 'RIGHTPAREN': pygame.K_RIGHTPAREN, 'ASTERISK': pygame.K_ASTERISK, 'PLUS': pygame.K_PLUS, 'COMMA': pygame.K_COMMA, 'MINUS': pygame.K_MINUS, 'PERIOD': pygame.K_PERIOD, 'SLASH': pygame.K_SLASH, '0': pygame.K_0, '1': pygame.K_1, '2': pygame.K_2, '3': pygame.K_3, '4': pygame.K_4, '5': pygame.K_5, '6': pygame.K_6, '7': pygame.K_7, '8': pygame.K_8, '9': pygame.K_9, 'COLON': pygame.K_COLON, 'SEMICOLON': pygame.K_SEMICOLON, 'LESS': pygame.K_LESS, 'EQUALS': pygame.K_EQUALS, 'GREATER': pygame.K_GREATER, 'QUESTION': pygame.K_QUESTION, 'AT': pygame.K_AT, 'LEFTBRACKET': pygame.K_LEFTBRACKET, 'BACKSLASH': pygame.K_BACKSLASH, 'RIGHTBRACKET': pygame.K_RIGHTBRACKET, 'CARET': pygame.K_CARET, 'UNDERSCORE': pygame.K_UNDERSCORE, 'BACKQUOTE': pygame.K_BACKQUOTE, 'a': pygame.K_a, 'b': pygame.K_b, 'c': pygame.K_c, 'd': pygame.K_d, 'e': pygame.K_e, 'f': pygame.K_f, 'g': pygame.K_g, 'h': pygame.K_h, 'i': pygame.K_i, 'j': pygame.K_j, 'k': pygame.K_k, 'l': pygame.K_l, 'm': pygame.K_m, 'n': pygame.K_n, 'o': pygame.K_o, 'p': pygame.K_p, 'q': pygame.K_q, 'r': pygame.K_r, 's': pygame.K_s, 't': pygame.K_t, 'u': pygame.K_u, 'v': pygame.K_v, 'w': pygame.K_w, 'x': pygame.K_x, 'y': pygame.K_y, 'z': pygame.K_z, 'DELETE': pygame.K_DELETE, 'KP0': pygame.K_KP0, 'KP1': pygame.K_KP1, 'KP2': pygame.K_KP2, 'KP3': pygame.K_KP3, 'KP4': pygame.K_KP4, 'KP5': pygame.K_KP5, 'KP6': pygame.K_KP6, 'KP7': pygame.K_KP7, 'KP8': pygame.K_KP8, 'KP9': pygame.K_KP9, 'KP_PERIOD': pygame.K_KP_PERIOD, 'KP_DIVIDE': pygame.K_KP_DIVIDE, 'KP_MULTIPLY': pygame.K_KP_MULTIPLY, 'KP_MINUS': pygame.K_KP_MINUS, 'KP_PLUS': pygame.K_KP_PLUS, 'KP_ENTER': pygame.K_KP_ENTER, 'KP_EQUALS': pygame.K_KP_EQUALS, 'UP': pygame.K_UP, 'DOWN': pygame.K_DOWN, 'RIGHT': pygame.K_RIGHT, 'LEFT': pygame.K_LEFT, 'INSERT': pygame.K_INSERT, 'HOME': pygame.K_HOME, 'END': pygame.K_END, 'PAGEUP': pygame.K_PAGEUP, 'PAGEDOWN': pygame.K_PAGEDOWN, 'F1': pygame.K_F1, 'F2': pygame.K_F2, 'F3': pygame.K_F3, 'F4': pygame.K_F4, 'F5': pygame.K_F5, 'F6': pygame.K_F6, 'F7': pygame.K_F7, 'F8': pygame.K_F8, 'F9': pygame.K_F9, 'F10': pygame.K_F10, 'F11': pygame.K_F11, 'F12': pygame.K_F12, 'F13': pygame.K_F13, 'F14': pygame.K_F14, 'F15': pygame.K_F15, 'NUMLOCK': pygame.K_NUMLOCK, 'CAPSLOCK': pygame.K_CAPSLOCK, 'SCROLLOCK': pygame.K_SCROLLOCK, 'RSHIFT': pygame.K_RSHIFT, 'LSHIFT': pygame.K_LSHIFT, 'RCTRL': pygame.K_RCTRL, 'LCTRL': pygame.K_LCTRL, 'RALT': pygame.K_RALT, 'LALT': pygame.K_LALT, 'RMETA': pygame.K_RMETA, 'LMETA': pygame.K_LMETA, 'LSUPER': pygame.K_LSUPER, 'RSUPER': pygame.K_RSUPER, 'MODE': pygame.K_MODE, 'HELP': pygame.K_HELP, 'PRINT': pygame.K_PRINT, 'SYSREQ': pygame.K_SYSREQ, 'BREAK': pygame.K_BREAK, 'MENU': pygame.K_MENU, 'POWER': pygame.K_POWER, 'EURO': pygame.K_EURO, }