Relationship between GUI and logic?

alex23 wuwei23 at gmail.com
Thu May 22 23:56:13 EDT 2008


On May 23, 1:37 pm, John Salerno <johnj... at gmailNOSPAM.com> wrote:
> 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?

Hey John,

Are you familiar with the Model-View-Controller pattern? It
specifically addresses this issue, and is very common in game
development.

In your example of a grid-based game like chess, the board & the
pieces would all be defined in the Model. The board may be a 2D array,
so to move a piece, the piece only has to know how to address
locations within the array, or even better, only has to know how to
tell the board where it's moving to, so the pieces don't have to know
how the board is implemented.

The View interprets & displays the Model. You could represent each
square by a bitmap, draw a 3d object, or use ASCII etc etc.

The Controller maps user input to Model behaviour, so pressing 'up'
while a piece is highlighted may call something like
piece.move_north(), which updates the necessary entities in the Model
to reflect the move. It generally also passes information from the
Model to the View, although it's not uncommon for the View to just
refer to the Model separately.

So you can think of the Model as a simulation of the behaviour you
want, while the View is _a_ representation of that simulation. The
simulation doesn't care if you're displaying it in 2D or 3D, it cares
about pieces and board positions. By separating the Model & the View,
you also allow for multiple representations of the same data, such as
having a 2D overhead map and a 3D visualisation of the same
information.

- alex23



More information about the Python-list mailing list