John Salerno a écrit :
I know that it is good programming practice to keep GUI and logic code
physically separate, by using XRC for example, but I'm wondering if it's
also good practice (and even possible) to keep them separate from an
implementation standpoint as well. Basically what I mean is, should it
be possible to write, for example, the logic for a strategy game without
even knowing what the graphics will look like or how they will work?
Depends on your definition of "logic". The point is not "keep GUI and
logic code" separate, but to keep "domain" logic (as) separated (as
possible) from user-interface stuff.
To be more specific, let's say I want to create a simple, 2D strategy
game. It will have a board layout like chess or checkers and the player
will move around the board. Let's say this is all I know, and perhaps I
don't even know *this* for sure either. Is it possible to write the
logic for such a game at this point?
Yes : build a model of the board and pieces and functions/methods to
allow (code-level) interaction with it. Then your user interface will
just have to create a visual representation of this model (let's call it
a view) and present the user with ways (connected to things we'll call
'controllers') to call the interaction functions/methods.
Then : the view registers itself with the model, display itself, and
wait for user interactions. These user interactions are sent to the
appropriate controller, which will call on the model's interaction
functions/methods. Then the model updates itself and notify the view
that it's state has changed, so the view can refresh itself.
Ever heard of the "Model/View/Controller" pattern ?
Let's say I want to write a "move" function (or perhaps it would be a
method of a "Character" class) for moving the player around. Could you
really write this function without 1) knowing what the interface will
look like, and 2) integrating GUI code with the logic?
Yes. The move function will modify the state of the board/pieces
("character", whatever) model, which will then notify whoever is
interested that it's state has changed.
Another example could be printing messages to the player. If I need to
say "You killed the monster!", is there a general way to write this, or
do I need to specifically refer to GUI widgets and methods, etc. in
order for it to be displayed properly?
Idem. State change, notification.
Basically, the question is this: can you write the logic behind a
program (whether it be a game, an email client, a text editor, etc.)
without having any idea of how you will implement the GUI?
s/GUI/ui/g
The user interface doesn't need to be graphical. There were games and
emails clients and text editors before GUIs existed, you know ?
--
http://mail.python.org/mailman/listinfo/python-list