Hello everyone. I'm making a game and trying to do everything properly, ie putting everything into classes and having no global variables. However, I have run into a slight problem. I have some images for player and enemies, and when I tried to convert them inside their classes it crashed saying that the video mode not initialised. So I've had to do this:

Is this ok? There are only a few images at the moment but by the time I'm done there will be more than double that. Will this cause any harm to my game, like slow down the fps or anything? Is there a better way of doing this?Grateful as alwaysDan

EDIT- I've just put my player images into a list and now I set the colorkey in the player class, the same thing that I do with the enemies. Don't really know why I didn't do that before. I still would like to know the rules on global constants and variables though

def load_all_gfx(directory,colorkey=(255,0,255),accept=(".png",".jpg",".bmp")): """Load all graphics with extensions in the accept argument. If alpha transparency is found in the image the image will be converted using convert_alpha(). If no alpha transparency is detected image will be converted using convert() and colorkey will be set to colorkey.""" graphics = {} for pic in os.listdir(directory): name,ext = os.path.splitext(pic) if ext.lower() in accept: img = pg.image.load(os.path.join(directory,pic)) if img.get_alpha(): img = img.convert_alpha() else: img = img.convert() img.set_colorkey(colorkey) graphics[name]=img return graphics

That takes care of the color coding as well. It is called something like this (depending on where the loading function is and what directory the graphics are in):

That is my dictionary of graphics. It is global and nothing every modifies it.

In smaller programs it is fine to just load them directly after the screen is initialized though, and I do similar in small examples. If you are going to do that however, I would colorkey them immediately if needed.

Cool cheers Mek, you always have an answer. How long have you been programming for? Hopefully one day I'll be able to write code as fluently as you (I've only been learning for just over 2 months, so I'm quite far away from that at the moment). Regarding the load_gfx() function, presumably I need to put that in the Control() class right? I alsso have a couple of other questions if you don't mind. The first is regarding your opinion on the use of super() inside classes. For my game, I'm using Tiled to create my level and initiate player and enemies positions. I'm also using a version of Richard Jones' Tmx library thats been ported for Python 3. So I'm using super() and this allows me to update everything (player,enemies,moving platforms) by just using tilemap.update(). I've never seen it used before though, only in an example that comes with the library, which kind of suggests that it's not a great method to use. I may be wrong, just wondering. The second question is do you mind if I use your smallface.png in my game? I've created sideviews of smallface for left and right, and the plan is, when my character dies your smallface png will replace the player imge, spin around a few times and then I'll respawn as my original player image at the start of the level. It will look so cool. Not sure how to rotate yet, but I'll work it out (probably lol). Thanks Mek!

I'm in my third year of programming somewhat seriously. I had a few classes here and there in college but at the time I wasn't very serious about it. The load_gfx function actually doesn't need to be in control. If you take a look at the current state of our Plant vs Zombies repo, you can see how I handle this in slightly larger programs.

Edit: Forgot to comment on super. I don't go out of my way to use it, though if it is being used in someone else's code I can. I have never had the type of inheritance structure that demands its use (complex diamond inheritance hierarchies). I just manually init inherited classes as do quite a lot of other people. One major thing that annoys me about super is that the simplified syntax of it that you can get away with in python 3 doesn't work in python 2.