There may be a much better way to manage artillery.

Rhodri James rhodri at wildebst.demon.co.uk
Tue May 12 00:39:51 CEST 2009


On Mon, 11 May 2009 22:59:43 +0100, Tobiah <toby at tobiah.org> wrote:

> On Mon, 11 May 2009 00:48:25 +0100, Rhodri James wrote:
>
>> On Mon, 11 May 2009 00:06:34 +0100, Tobiah <toby at tobiah.org> wrote:
>>
>> [Snippety snip]
>>
>>> I wanted the bullets to be responsible for destroying themselves, but a
>>> little Googling brought me to points about dangling references and how
>>> an object is not allowed (nor does it seem to have the means) to  
>>> destroy
>>> itself. That's why I made this wrapper class for all of the bullets.
>>
>> An object can, however, erase its representation from the screen and  
>> tell
>> whatever control system is keeping track of objects to forget about it.
>> Once the last reference to the object is deleted, the object will be
>> garbage collected.
>>
>> What framework are you writing your game in?  Pygame has facilities for
>> handling this sort of thing fairly straightforwardly.
>
> Actually I am using Pygame, although I'm quite new to it.  Which
> module has the facilities that you describe?

Looking at the code, it's not quite as straightforward as I was making
out!  I'm used to writing games using a veneer library which has a
Screen class that handles all the basic drawing functions and the main
processing loop.  It keeps a list of objects, and is responsible for
calling appropriate methods on those objects when things happen.
The Object class has a destroy() method which erases the object and
asks the Screen to remove it from its internal list.  Needless to
say there is a lot of list copying going on in the Screen class
main loop to make sure that lists aren't corrupted as they are being
iterated through.

This code was written back in the days of Python 2.1, and hasn't had
more than simple bug-fixing done to it since.  We're contemplating a
rewrite when we shift to Python 3, and we're putting that decision
off as long as possible :-)  You're welcome to use or scavenge from
it, as it's on a fairly free license.  It's available here:

   http://www.livewires.co.uk/python/home

(though apparently I must go and bug the webmaster about putting the
latest version of the worksheets up.  What's there at the moment is
years out of date!)

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list