[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] placement of graphics code



On Mon, Aug 11, 2003 at 05:34:28PM +0200, Gerrit Holl wrote:
> R. Alan Monroe wrote:
> > Date: Mon, 11 Aug 2003 00:05:53 +0200
> 
> > Dumb newbie question: Is there a generally accepted "right" place to
> > put a blit statement, given a choice of putting it inside a method of
> > your game object, versus in the main game loop that acts on said
> > object? Or is it "six of one, half dozen of another"?
> 
> I put it into a Screen class, which has methods to be called by my
> main game look. The object knows nothing about this. My screen class
> does much more, like regulating the viewport and scrolling the screen,
> as well as drawing a grid and blitting multiple backgrounds and
> more...

Hi,

I use (probably not consistently) the Model-View approach. The world data
are objects which send messages when they are changed. The UI consists
of views (widgets) which redraw themselves when they are signalled a
change in a data object they watch. When clicked they may change the
data in their associated data objects or control another view object.

My main loop then looks like this:

  open the window
  create canvas on it
  create the first view on the canvas

  while (1):
      wait for user interaction event or tick event
      canvas->update() if necessary
      dispatch the event to the view objects
      do scheduled actions


canvas->update () takes care of redrawing parts of the canvas which has changed and blitting them
on the screen and doing display flip, but otherwise is pretty stupid - it does not know
anything about the game. It's because it does not deal with the views directly, only with
canvas items - squares, texts and images (similar to sprites)

The views, on the other hand, know of which canvas items they consist and add/delete/modify
them as necessary


I guess that you could get similar approach with the Sprite/SpriteGroup classes, I use
canvas for historical reasons.

BTW: I hope to offer my canvas for public adoration/criticism soon - holidays are over :-)


Jarda Benkovsky

-- 
GCM/IT d- s:+ a- C++(+++) ULOI++++$ P++ L+++>++++ E+ W++
N w--- PS+ PE++ Y+ PGP R+ tv- b+++ D+ e+++ y+