call function of class instance with no assigned name?
hi, I'm a Python beginner with a basic question. I'm writing a game where I have keyboard input handling defined in one class, and command execution defined in another class. The keyboard handler class contains a dictionary that maps a key to a command string (like 'h': 'left') and the command handler class contains functions that do the commands (like def do_right(self):), I create instances of these classes in a list attached to a third, 'brain' class. What I'd like to have happen is when the player presses a key, the command string is passed to the command handler, which runs the function. However I can't figure out a good way to make this happen. I've tried a dictionary mapping command strings to functions in the command handler class, but those dictionary values are evaluated just once when the class is instantiated. I can create a dictionary in a separate function in the command handler (like a do_command function) but creating what could be a big dictionary for each input seems kind of silly (unless I'm misunderstanding something there). What would be a good way to make this happen, or is there a different kind of architecture I should be thinking of? -- http://mail.python.org/mailman/listinfo/python-list
Re: call function of class instance with no assigned name?
On May 5, 9:01 am, Chris Rebert wrote: > On Tue, May 5, 2009 at 8:52 AM, George Oliver > wrote: > > I create instances of these classes in a list attached to a third, > > 'brain' class. > > You could exploit Python's dynamism by using the getattr() function: Thanks for the responses -- I should have mentioned that I looked at the getattr() function, but as I instantiate the key handler and command handler classes in a list (like handlers = [command_handler(), keyboard_handler()], I'm unsure how to reference that class instance in the getattr in a straightforward way (short of keeping track of each instance's position in the handlers list). Is there a typical way of doing that? -- http://mail.python.org/mailman/listinfo/python-list
Re: call function of class instance with no assigned name?
On May 5, 11:59 am, Dave Angel wrote: > 1) forget about getattr() unless you have hundreds of methods in your > map. The real question is why you need two maps. What good is the > "command string" doing you? Why not just map the keyvalues directly > into function objects? Thanks for the reply Dave. I understand your example and it's what I originally used. Here is more detail on what I'm doing now, I hope this will explain my question better. In the game I'm writing the player, monsters, items and so on are instances of class Thing, like: class Thing(object): def __init__(self, x, y, name): self.x, self.y = x, y self.name = name self.brain = None Some Things have an instance of class Brain attached. The Brain instance has a list of handlers, like: class Brain(object): def __init__(self): self.handlers = [] A handler class defines some functionality for the Brain. Each Brain has an update method like this: def update(self, arguments): for handler in self.handlers: handler.update(arguments) So on each pass through the main game loop, it calls the update method of all the brains in the game, which run their handler update methods to change the state of the game. A handler would be something like a key input handler. The key input handler is defined separately from the command handler in the case I want to use a different method of input, for example a mouse or joystick. In the dictionary of key inputs I could map each input directly to a function. However there is no instance name I can call the function on, as I create a thing, add a brain, and add handlers to the brain like this: player = Thing(26, 16, 'player') player.brain = Brain() player.brain.add_handlers( commandHandler(player.brain, player), keyboardHandler(player.brain), fovHandler(player.brain)) So what I'm wondering is how to reference the instance in each brain's list of handlers when I want to map something like a key input to a command, or what a better way might be to structure the code. -- http://mail.python.org/mailman/listinfo/python-list
Re: call function of class instance with no assigned name?
Thanks for the suggestions so far. I've taken the advice to keep things simple so currently I'm just creating one instance of the commandHandler and assigning it a name with command = commandHandler (). This makes it easy to call it from any of the other handlers, and I think this will work for what I want the game to do. -- http://mail.python.org/mailman/listinfo/python-list
Re: call function of class instance with no assigned name?
On May 6, 3:07 pm, Carl Banks wrote: > I'm going to guess that you want the keyboardHandler to call method of > commandHandler. There's no reason for commandHandler to be a handler > at all then: keyboardHandler is already handling it. Thanks Carl, you've got it right and your following example is what I ended up using, it's good to know it's a workable solution. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python in a group
On Jun 22, 3:43 am, "Jonathan Roberts" <[EMAIL PROTECTED]> wrote: > Hi all, > > I'm looking to learn Python (as my first programming language) and I'm > pretty sure I'd be more successful doing this with a group of other > people. hi Jon, I'm in the same situation as you and think a co-op method of learning could be effective and fun. I found the Tutor list that David mentioned a little while ago but also have been looking for a group (without success so far). So count me in! best, George -- http://mail.python.org/mailman/listinfo/python-list
2D online multiplayer framework?
I'm looking for a framework to support a 2D online real-time multiplayer game (rugby league football with a lo-fi pixel look). The GameProgramming page at the Python wiki had some suggestions but so far nothing looks that promising, does anyone have some recommendations? It would be ideal to play this through a web browser but I don't know if that's practical. This aspect of programming is pretty new to me so I'm not sure if I should start with something general like Twisted or if there's a different path I should take. thanks, George -- http://mail.python.org/mailman/listinfo/python-list
Re: 2D online multiplayer framework?
On Jun 28, 9:04 am, Gary Herron <[EMAIL PROTECTED]> wrote: > > Pyglet is my favorite: http://www.pyglet.org/ > > Twisted might be fine for the "online multiplayer" parts, but really if > you want a 2D/3D real-time game, start with a game framework. > > Gary Herron Thanks Cédric and Gary for the suggestions, I'm familiar with all of those. So a good workflow for a graphical online game is to make the game first and just plan for the incoming connections without knowing how they will be done exactly? My only experience with networked games is muds, where usually you do the opposite -- sockets first. -- http://mail.python.org/mailman/listinfo/python-list
sending input to an embedded application
hi, I'm a novice programmer trying to better define a hobby project I'm thinking of. What I would like to do is take a program and embed it or put it within a Python-run GUI, using the GUI just to capture and send input to the application, and display the ouput. Specifically I want to use a Python module such as wxPython, pygame or pyglet to build the UI, capture key presses, and then send a string to the program, an interactive fiction interpreter; the reason for this is that the interpreter on its own doesn't have the capability to recognize certain key presses on the keyboard. I thought that writing a middle layer rather than a brand new interpreter would be easier for someone of my skill level. The interpreter would send its output to the Python GUI. The GUI then would be as light/translucent as possible, just accepting input, sending it to the interpreter, and displaying the output as if you were just running the interpreter by itself. As I don't really know the issues involved, I'm hoping someone can point me in the right direction. Do people 'frame' programs with something like wxPython or pygame and use the frame to capture and pass along input, and receive and display the output? thanks, George -- http://mail.python.org/mailman/listinfo/python-list
looking for Python live code reloading IDEs
hi, I'm wondering if there are any Python programming environments that enable live code reloading, for example something like the Scheme- based impromptu (but also meant for any kind of Python program, not just audio/visual generation). Currently I do this directly in my editor (for game development), but I'm curious if there are other options. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newsgroup for beginners
On Nov 16, 8:35 am, Terry Reedy wrote: > mrholtsr wrote: > > Is there a Python newsgroup for those who are strictly beginners at > > programming and python? > > gmane.comp.python.tutor, which I believe mirrors the tutor-list There also is a beginner's forum at python-forum.org: http://www.python-forum.org/pythonforum/viewforum.php?f=3 -- http://mail.python.org/mailman/listinfo/python-list
requirements in writing an email/rss/usenet client?
hi, I'd like to know what to consider when writing an email/rss/usenet client. Apologies for such a broad question, but I've never attempted a project of this scope and I'm currently feeling out the requirements. My target is something like a gui-based mutt (I use a Windows OS btw), with influences from programs like Alpine (http://www.washington.edu/ alpine/) and Sup (http://sup.rubyforge.org/). I currently use Thunderbird + Muttator, which is a nice setup; but, it has some clunky parts, and I thought it might be simpler in the end to start fresh than try to engage with what seems to be the massive-ness of Thunderbird (of course, I may be disabused of this notion at some point ;) ). So far I've looked at the email and related modules in the standard lib, found some related links from the list [1], and read a little about the relevant protocols involved. I'd appreciate hearing about any other examples, references, or experiences of people who've written similar programs or related libraries. thanks, George [1] Including references to: * http://sourceforge.net/projects/usablemail/ * http://proquest.safaribooksonline.com/0596000855/python2-CHP-11-SECT-4 * http://wiki.laptop.org/go/Email * the pyne client * http://chandler.osafoundation.org/ -- http://mail.python.org/mailman/listinfo/python-list
interactive fiction in Python?
hi, I'm just curious who might be working on interactive fiction modules in the style of Inform or TADS for Python. I've seen a few threads on this list [1] (among many that mention IF tangentially), and there are old projects like PUB and PAWS. There are some newer potential projects such as Curveship as well. In any case I'd like to see what's out there. thanks, George [1]: http://bit.ly/Python_Text_Adventure_Authoring_System http://bit.ly/simple_interactive_fiction_engine http://bit.ly/adventure_engines_in_Python -- http://mail.python.org/mailman/listinfo/python-list