Let's move right along to our Gun
classes. These are created and destroyed with the cycles, so we need to make sure they are getting garbage collected properly.
Open the
GunClasses_00.py
file from theChapter12
folder. Update the imports section to useExplosionClasses_02.py
.Testing comes first, so put the following method at the bottom of the
MachineGun
class:def __del__(self): print("Machine Gun Removed")
Then, put this method at the bottom of the
Cannon
class:def __del__(self): print("Cannon Removed")
This time, resave the file as
GunClasses_01.py
. OpenCycleClass_00.py
and update the import to useGunclasses_01.py
, then resave the file with Ctrl+S and run the game. Click the New Game menu button to destroy some cycles and check our garbage collection on the guns.This time we got it in the first try, as we can see from the printout to the command prompt shown in the following screenshot. Go ahead and remove the
__del__...