Hi everyone
Hi just found out at this and i am a noob
I am just wondering if anyone can tell me how to put the buttons in a text file.
Can you please elaborate what you mean? Do you want the caption of your DirectButtons saved in a text file? Do you want to store a list of DirectButtons in a text file? Or do you mean something else?
You could try storing the keys in a XML file and use PyXML for update and parse it… that’s what I would do since it’s such a portable format.
or use a .csv file and use the “csv” module that comes with python.
why not just store the button definitions in python? Its also a text file
like
self.accept("w", self.setKey, ["forward", 1])
i want to put “w” in a text file
My guy system has some code:
discourse.panda3d.org/viewtopic.php?t=3242
But you have to dig at it. Its really not that hard, read the file that has key to value mapping dict and put them in appropriate place.
in your script, just create a default setting dictionary
defaultSetting = {
"forward" : ["w","arrow_up"],
"backward" : ["s","arrow_down"],
"left" : ["a","arrow_left"],
"right" : ["d","arrow_right"],
}
If user changes the setting, just pickle the whole dictionary to disk, and upon next startup, check if there is saved setting on disk, unpickle and use it instead of the default in your script.
thanks all for your 2 cents and especially tree form.
now i make it write the 1
self.accept("w", self.setKey, ["forward", 1])
Hi guys, i got a problem
i am trying to change the 1
self.accept("a", self.setKey, ["left",1])
my code is
class Keys:
def __init__(self,game):
""" create event prosessor """
self.game = game
self.focus = None
self.bound = {}
for n,line in enumerate(open('cfg/keys.txt')):
s = line.split()
if s == []:
continue
elif line[0] != "#":
key,message,p = s
print message
print p
self.bound[key] = message
else:
print "error in keys.3gg file on line",n
for event in self.bound.keys():
self.game.accept(event,self.onKey,[event,p])
letters = "abcdefghijklmnopqrstuvwxyz"
symbols = "1234567890-=,./[]\\;'`"
shifted = "!@#$%^&*()_+<>?{}|:\"~"
work = ['enter','backspace','space']
e = ['forward','cam-left','cam-right','left','right']
self.mouseEvents = dict.fromkeys(["mouse1", "mouse2", "mouse3"])
#for mouseEvent in ["mouse1", "mouse2", "mouse3","mouse1-up","mouse2-up","mouse3-up"]:
# self.game.accept(mouseEvent,self.onKey,[message,p])
for letter in symbols+letters:
self.game.accept(key,self.onKey,[message,p])
#self.game.accept('shift-'+letter,self.onKey,[message,p])
for event in work:
self.game.accept(key,self.onKey,[message,p])
#for index,key in enumerate(symbols):
#self.game.accept('shift-'+key,self.onKey, [shifted[message],p] )
self.inputKeys = dict.fromkeys(list(letters+letters.upper()+symbols+shifted)+work)
def onKey(self,key,message,p):
""" if gui is in focus give keys to that else
see if they are bound to action """
#messenger.send("redraw")
#if key in self.mouseEvents:
# if self.game.gui.baseMouseEvent(key):
# return
#if key in ("mouse1-up","mouse2-up","mouse3-up"):
# self.game.gui.drag = None
if self.focus.onKey(key,message,p):
return
if key in self.bound:
print key
messenger.send(self.bound[key,message,p])
and my keys.txt is
o cam-left 1
o-up cam-left 0
p cam-right 1
p-up cam-right 0
w forward 1
w-up forward 0
a left 1
a-up left 0
d right 1
d-up right 0
any help please