Below is the Sea Battle game ini construction.
I have included in this how voices are stored and selected.
In Python arrays and dictionairies have [Val] {Key:Val} as constructs
respectively. Both can be empty to place them into the memory table.
Comments always start with the # sign.
Functions and Procedures start with the key word def and that header lline ends
always with a :.
Remember that Python divides statements based in indent level and you can do
your own margin amount as long as you are consistent.
Globals are first as in most languages and like Visual Basic you have to import
definitions and they are called modules in Python and you use the name import
to get them or part of them and the module file name can be removed by just
asking for what inside that module as long as you know the names inside the
module of each function/procedure/variable...
You do this by using the key word From.
Also you can get a complete list of a type by using * to get them all.
#Global Section:
# Load Modules
import pygame
from pygame.locals import *
from pygame.event import Event
import random, sys, os
import time
from math import *
#import pyTTS
from win32com.client import constants
import win32com.client
import BattleSapi5
import ConfigParser
IniConfig = ConfigParser
from win32api import GetProfileSection
GetIniSection = GetProfileSection
#Below Reduces Name Size In Program and Generates Needed 3'rd Applications For
Compiling .exe Files.
from comtypes.client import CreateObject
# Fetch Generated Speech:
try:
from comtypes.gen import WindowEyes
except:
print " Windoweyes Not Generated Yet "
try:
from comtypes.gen import SpeechLib
except:
print " Speech Lib Not Generated Yet "
#Fix for no video device on XP machines placed in as Pygame 1.9.1 bug fix.
os.environ['SDL_VIDEODRIVER']='windib'
#Set up video display:
pygame.init()
screenMode = pygame.display.set_mode((640,480))
#Variables and dictionairies
#Note: Dictionairy pairs are {key:value} or {another dictionary...}}
SapiPlayerVoice = {"v": 0, "rl": 1, "pl": 2, "vl": 75}
SapiComputerVoice = {"v": 1, "rl": 2, "pl": 4, "vl": 75}
SapiPlayer2Voice = {"v": 0, "rl": 2, "pl": 2, "vl": 75}
WePlayerVoice = {"v": "L", "rl": 62, "pl": 4, "vl": 6}
WeComputerVoice = {"v": "K", "rl": 62, "pl": 4, "vl": 6}
WePlayer2Voice = {"v": "S", "rl": 62, "pl": 4, "vl": 6}
WeScreenVoice = {"v": "L", "rl": 62, "pl": 4, "vl": 6}
myIniFile = "SeaBattle.ini" #"SeaBattle2015.ini"
PlayerDict = {"s": "Player", "c": "Computer", "p": "Player2"}
WeTtsDict = {"INI": myIniFile, NAME4GAME_PLAYLIST: {}, NAME4JUKEBOX_PLAYLIST:
{}, "Org": WeScreenVoice,
"Player": {"SAPI": SapiPlayerVoice, "WE": WePlayerVoice},
"Computer": {"SAPI": SapiComputerVoice, "WE": WeComputerVoice},
"Player2": {"SAPI": SapiPlayer2Voice, "WE": WePlayer2Voice},
"IniSection":{}}
def SetIniData( section, key, val, gameDict):
"Set Section, Key, Value In Ini File."
try:
gameDict[ "IniOutput"].set( section, key, val)
except:
gameDict[ "IniOutput"].add_section( section)
gameDict[ "IniOutput"].set( section, key, val)
#Save In Configuration File:
with open( gameDict[ "INI"], 'w') as configfile:
gameDict[ "IniOutput"].write( configfile)
def GetIniData( gameDict):
"Get All Ini Data!"
iniFileName = gameDict[ "INI"]
iniConfig = gameDict[ "IniConfig"]
iniInput = iniConfig.RawConfigParser()
gameDict[ "IniInput"] = iniInput
iniOutput = iniConfig.RawConfigParser()
gameDict[ "IniOutput"] = iniOutput
sArray = []
#Note: Ini File must have exact path.
#theIniFile = os.path.join( myDir, myFileName)
try:
iniInput.read( iniFileName)
sArray = iniInput.sections()
except:
print " No INI Sections! "
COLS4BOARD = DEFAULT4COLS
SetIniData( "Configuration", "oceancolumns", COLS4BOARD, WeTtsDict)
ROWS4BOARD = DEFAULT4ROWS
SetIniData( "Configuration", "oceanrows", ROWS4BOARD, WeTtsDict)
# gameDict[ "IniSection"] = {}
for section in sArray:
#Get All Section Names and Data:
s2Array = GetIniSection( section, iniFileName)
iniOutput.add_section( section)
#Now Init Section Data:
gameDict[ "IniSection"][ section] = {}
for key in s2Array:
#Now Save and Assign All Section Data Value Pairs:
#print key
s3Array = key.split( '=')
iniOutput.set( section, s3Array[ 0], s3Array[ 1])
try: #Save Key As Integer:
gameDict[ "IniSection"][ section][ int( s3Array[ 0])] =
s3Array[ 1]
except: #Save Key As String:
gameDict[ "IniSection"][ section][ s3Array[ 0]] = s3Array[ 1]
#Finally Now Writing Our New Configuration File:
with open( iniFileName, 'w') as configfile:
iniOutput.write( configfile)
def SaveVoice( engine, name, gameDict):
"SAVE THE VOICE SETTINGS IN THE INI FILE!"
ve = "SAPI"; vc = "s"
# We engine must exist to use ini file.
#if gameDict[ "weVoices"]:
if engine == "WE":
ve = "WE"; vc = "w"
SetIniData( name, "vw", gameDict[ name][ ve][ "v"], gameDict)
else:
SetIniData( name, "vs", gameDict[ name][ ve][ "v"], gameDict)
SetIniData( name, "rl"+vc, gameDict[ name][ ve][ "rl"], gameDict)
SetIniData( name, "pl"+vc, gameDict[ name][ ve][ "pl"], gameDict)
SetIniData( name, "vl"+vc, gameDict[ name][ ve][ "vl"], gameDict)
def changePlayerVoice( engineType, player, gameDict):
"CHANGE THE PLAYERS VOICE, PITCH, RATE, AND VOLUME!"
global _PITCH
engine = gameDict[ "TTS4" + engineType]
v = gameDict[ player][ engineType][ "v"]
r = gameDict[ player][ engineType][ "rl"]
p = gameDict[ player][ engineType][ "pl"]
vl = gameDict[ player][ engineType][ "vl"]
try:
if engineType == "WE":
engine.ActiveSettings.Screen.Volume = vl
engine.ActiveSettings.Screen.Tone = v
engine.ActiveSettings.Screen.Rate = r
engine.ActiveSettings.Screen.Pitch = p
else:
if engineType == "SAPI":
_PITCH = p
engine.setVoiceByNum( v)
engine.setRate( r)
engine.setVolume( vl)
except: #pass
print " \n Change Player Voice Error! \n "
def RestoreWeVoice( gameDict):
"Restore Original Windoweyes Screen Voice."
if gameDict[ "weVoices"]:
gameDict[ "TTS4WE"].ActiveSettings.Screen.Tone = gameDict[ "Org"][ "v"]
gameDict[ "TTS4WE"].ActiveSettings.Screen.Rate = gameDict[ "Org"][ "rl"]
gameDict[ "TTS4WE"].ActiveSettings.Screen.Pitch = gameDict[ "Org"][
"pl"]
gameDict[ "TTS4WE"].ActiveSettings.Screen.Volume = gameDict[ "Org"][
"vl"]
gameDict[ "TTS4WE"].Speech.Speak( " Voice back to original voice ")
#Created procedure using windoweyes TickCount
Sleep( 1.0)
# t = gameDict[ "TTS4WE"].TickCount
def VoiceAdjustment( gameDict, playerKey="s"):
"Adjust All Voices."
global WE_VOICE
alphabetCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
voiceType = {1: "v", 2: "vl", 3: "rl", 4: "pl"}
voiceDescription = {1: "Voice", 2: "Volume", 3: "Rate", 4: "Pitch"}
voiceBoundaries = {"WE": {"v": {"h":25, "l":0},
"vl": {"h":9, "l":0}, "rl": {"h":100, "l":0}, "pl": {"h":9, "l":0}},
"SAPI": {"v": {"h":0, "l":0},
"vl": {"h":100, "l":0}, "rl": {"h":9, "l":-9}, "pl": {"h":9, "l":-9}}}
try:
voiceBoundaries[ "SAPI"][ "v"][ "h"] = WeTtsDict[
"TTS4SAPI"].getVoiceCount() -1
except: pass
playerNum = {"s":1, "p":2, "c":3}
playerType = {1: "s", 2: "p", 3: "c"}
playerName = {1:" Player 1", 2:" Player 2", 3:" Computer "}
engineName = {1: "Window Eyes", 2: "Windows SAPI"}
engineType = {1: "WE", 2: "SAPI"}
gamePlayerKey = playerKey
player = playerNum[ playerKey]
gameVoice = WE_VOICE
engine = 2
if WE_VOICE: engine = 1
voiceEngine = engineType[ engine]
voice = 1
voiceKey = voiceType[ voice]
Speak( " Adjust: %s, %s Voice " % (playerName[ player], engineName[
engine]))
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
RestoreWeVoice( WeTtsDict)
sys.exit()
break
elif event.type == pygame.KEYDOWN:
key = event.key
keymod = event.mod
if key == K_ESCAPE:
playerKey = gamePlayerKey
WE_VOICE = gameVoice
SpeakNext( " Now Leaving Voice Settings ")
break
elif key in [K_LEFT, K_RIGHT, K_UP, K_DOWN]:
if key == K_LEFT:
if WeVoices: engine = 1
else: SpeakNext(" No Window Eyes Voices ")
elif key == K_RIGHT:
if SapiVoices: engine = 2
else: SpeakNext(" No Windows SAPI Voices ")
elif key == K_UP:
player += 1
else:
player -= 1
if engine > 1:
engine = 2
WE_VOICE = False
if engine < 2:
engine = 1
WE_VOICE = True
voiceEngine = engineType[ engine]
if player > 3: player = 3
if player < 1: player = 1
playerKey = playerType[ player]
changePlayerVoice( voiceEngine, playerKey, gameDict)
SpeakNext( " Adjust: %s, %s Voice " % (playerName[ player],
engineName[ engine]))
elif key == K_RETURN:
changePlayerVoice( voiceEngine, playerKey, gameDict)
SpeakNext( " Now Adjusting: %s Voice In: %s " % (playerName[
player], engineName[ engine]))
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
RestoreWeVoice( WeTtsDict)
sys.exit()
break
elif event.type == pygame.KEYDOWN:
key = event.key
keymod = event.mod
inc = 1
if voiceEngine == "SAPI" and voiceKey == "vl": inc = 5
if True: #try:
changePlayerVoice( voiceEngine, playerKey, gameDict)
val = gameDict[ playerKey][ voiceEngine][ voiceKey]
if voiceEngine == "WE" and voiceKey == "v": val =
alphabetCaps.find( val)
else: val = int( val)
if key == K_ESCAPE or key == K_RETURN:
SpeakNext( " Adjust: %s Voice In: %s " %
(playerName[ player], engineName[ engine]))
break
elif key in [K_LEFT, K_RIGHT, K_UP, K_DOWN]:
if key == K_LEFT:
voice -= 1
elif key == K_RIGHT:
voice += 1
elif key == K_UP:
val += inc
else:
val -= inc
if voice < 2: voice = 1
if voice > 3: voice = 4
voiceKey = voiceType[ voice]
if key == K_LEFT or key == K_RIGHT:
val = gameDict[ playerKey][ voiceEngine][
voiceKey]
if voiceEngine == "WE" and voiceKey == "v":
val = alphabetCaps.find( val)
else: val = int( val)
else:
if val > voiceBoundaries[ voiceEngine][
voiceKey][ "h"]:
val = voiceBoundaries[ voiceEngine][
voiceKey][ "h"]
if val < voiceBoundaries[ voiceEngine][
voiceKey][ "l"]:
val = voiceBoundaries[ voiceEngine][
voiceKey][ "l"]
if voiceEngine == "WE" and voiceKey == "v":
val = alphabetCaps[ val]
gameDict[ playerKey][ voiceEngine][
voiceKey] = val
# Speak( " Adjust: %s, %s Voice Val: %s " %
(playerName[ player], engineName[ engine], str(val)))
if voiceEngine == "SAPI":
if val == voiceBoundaries[
voiceEngine][ voiceKey][ "l"] and voiceKey == "vl":
WeTtsDict[ "TTS4SAPI"].setVolume(
100)
SpeakNext( ' Volume %d, Minimum!' %
val)
gameDict[ playerKey][ voiceEngine][
voiceKey] = val
changePlayerVoice( voiceEngine, playerKey,
gameDict)
SpeakNext(' %s %s' % (voiceDescription[ voice],
gameDict[ playerKey][ voiceEngine][ voiceKey]))
# except: SpeakNext( " %s Error! " % voiceKey); pass
def SavePlayList( spk, playListName, gameDict, playList):
"Save IniFile Specified Play List."
SpeakNext( " Now Saving: %s Ini File Play List " % spk)
# print " Now Saving: %s Play List " % spk
total = playList[ "t"]
pos = playList[ "p"]
vol = str( playList[ "v"]) #Note: Below IniFile Numbers Are Integer Only.
SetIniData( playListName, "t", total, gameDict)
gameDict[ "IniSection"][ playListName][ "t"] = total
SetIniData( playListName, "p", pos, gameDict)
gameDict[ "IniSection"][ playListName][ "p"] = pos
#Note: Below is not an integer so it has to be saved as text.
SetIniData( playListName, "v", vol, gameDict)
gameDict[ "IniSection"][ playListName][ "v"] = vol
for sKey in range(1, total+1):
print " Tot: %d Key: %d \n List: %s \n Name: %s FilePath: \n%s \n
Repeats: %s " % (total, sKey, playListName, playList[ sKey][ "n"], playList[
sKey][ "f"], str(
playList[ sKey][ "r"]))
sData = playList[ sKey][ "n"] + '~' + playList[ sKey][ "f"] + '~' +
str( playList[ sKey][ "r"])
#Note: Keys in Config must be text:
SetIniData( playListName, str( sKey), sData, gameDict)
gameDict[ "IniSection"][ playListName][ sKey] = sData
def GetPlayList( playListName, gameDict):
"Get IniFile Specified Play List."
playError = False
playList = {}
try:
total = int( gameDict[ "IniSection"][ playListName][ "t"])
# print " Name: %s Total: %d" % (playListName, total)
except: total = 0
try:
if total < 1:
# print " total2: %d " % total
if playListName == NAME4JUKEBOX_PLAYLIST:
total = gameDict[ NAME4JUKEBOXDEFAULTS][ "t"]
else:
total = gameDict[ NAME4BACKGROUNDDEFAULTS][ "t"]
if total > 0:
if playListName == NAME4JUKEBOX_PLAYLIST:
SavePlayList( playListName, playListName, gameDict,
DEFAULTLIST4JUKEBOX)
else:
SavePlayList( playListName, playListName, gameDict,
DEFAULTLIST4BACKGROUND)
# print " total2E: %d " % total
# print " Name2: %s Total: %d" % (playListName, total)
if total < 1: playError = True
except: print " Play List Error! " #playError = True
if playError:
total = 1
playList[ "t"] = total
playList[ 1] = {"n":"", "f":"", "r":"0"}
playList[1][ "n"] = "SummerDay"
playList[1][ "f"] = os.path.join( FOLDER4BACKGROUND_PLAYLIST,
"SummerDay.ogg")
playList[1][ "r"] = "0"
SavePlayList( playListName, playListName, gameDict, playList)
# print " Play Total: %d Error: %s " % (total, playError)
playList[ "t"] = int( gameDict[ "IniSection"][ playListName][ "t"])
playList[ "p"] = int( gameDict[ "IniSection"][ playListName][ "p"])
playList[ "v"] = float( gameDict[ "IniSection"][ playListName][ "v"])
# print " t: %d p: %d v: %1.1f \n" % (playList[ "t"], playList[ "p"],
playList[ "v"])
for sKey in range(1, total+1):
sData = "No Name~No Path~0"
#Note: All Config Ini Keys Are Text: Change To Integer.
sData = gameDict[ "IniSection"][ playListName][ sKey]
sArray = sData.split( '~')
playList[ sKey] = {"m": 0, "n": sArray[ 0], "f": sArray[ 1], "r":
sArray[ 2]}
gameDict[ playListName] = playList
def getIniVoices( name, gameDict):
"GET THE VOICE SETTINGS FROM THE INI FILE."
# We engine must exist to use ini file.
# if talkDict[ "weVoices"]:
#Using Windows .ini Instead:
try:
if name in gameDict[ "IniSection"]:
gameDict[ name][ "WE"][ "v"] = gameDict[ "IniSection"][ name][ "vw"]
gameDict[ name][ "WE"][ "rl"] = int( gameDict[ "IniSection"][
name][ "rlw"])
gameDict[ name][ "WE"][ "pl"] = int( gameDict[ "IniSection"][
name][ "plw"])
gameDict[ name][ "WE"][ "vl"] = int( gameDict[ "IniSection"][
name][ "vlw"])
gameDict[ name][ "SAPI"][ "v"] = int( gameDict[ "IniSection"][
name][ "vs"])
gameDict[ name][ "SAPI"][ "rl"] = int( gameDict[ "IniSection"][
name][ "rls"])
gameDict[ name][ "SAPI"][ "pl"] = int( gameDict[ "IniSection"][
name][ "pls"])
gameDict[ name][ "SAPI"][ "vl"] = int( gameDict[ "IniSection"][
name][ "vls"])
except:
print "\n %s Ini Voices Error! Possibly Not Set Yet. \n Just Use
Control S To Save Your Voice Settings During Game. \n" % name
def SaveAllSettings2IniFile( gameDict):
"Save Voices and Play Lists."
#Save Voice Settings.
SaveVoice( "WE", "Player", gameDict)
SaveVoice( "WE", "Computer", gameDict)
SaveVoice( "WE", "Player2", gameDict)
SaveVoice( "SAPI", "Player", gameDict)
SaveVoice( "SAPI", "Computer", gameDict)
SaveVoice( "SAPI", "Player2", gameDict)
SpeakNext( " Voices Saved ")
SavePlayList( "Game Music Background", NAME4GAME_PLAYLIST, gameDict,
gameDict[ NAME4GAME_PLAYLIST])
SavePlayList( "Juke Box Music", NAME4JUKEBOX_PLAYLIST, gameDict, gameDict[
NAME4JUKEBOX_PLAYLIST])
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
_______________________________________________
Any views or opinions presented in this email are solely those of the author
and do not necessarily represent those of Ai Squared.
For membership options, visit
http://lists.window-eyes.com/options.cgi/scripting-window-eyes.com/archive%40mail-archive.com.
For subscription options, visit
http://lists.window-eyes.com/listinfo.cgi/scripting-window-eyes.com
List archives can be found at
http://lists.window-eyes.com/private.cgi/scripting-window-eyes.com