Relationship between GUI and logic?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri May 23 01:42:16 EDT 2008


On Thu, 22 May 2008 23:37:45 -0400, John Salerno wrote:

> 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?

Yes, that's possible.

> 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?

Maybe even this is possible.  But here you are not only drawing the line
between GUI and logic but also within the logic part itself because the
board layout isn't (just) a GUI issue.  If you have checkers, hexagons, or
other shapes also has influence on the logic part, because you need
slightly different algorithms for finding ways from tile `A` to tile `B`,
or to decide when a piece is blocked by others, and how the rules to move
pieces around look like.  Just imagine how to modify chess rules to a
board with hexagonal patterns.  It would severely change the logic part.

> 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?

The observer pattern can be used here.  The GUI has to register a callback
function with your logic engine and every time you want to inform the
player about something, your game logic calls that function with the
message.  It doesn't have to know if it will be rendered as graphic on
screen, displayed as text in a terminal, or synthesized as sexy female
computer voice.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list