Global constants and variables query

Global constants and variables query

Postby dboxall123 » Sat Aug 17, 2013 9:05 pm

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:
Code: Select all
if __name__ == '__main__':
    os.environ['SDL_VIDEO_CENTERED'] = '1'
    pg.init()
    screen = pg.display.set_mode((640,480))
    pg.display.set_caption(CAPTION)
    PLAYER_RIGHT_IMG = pg.image.load('player_right.png').convert()
    PLAYER_RIGHT_IMG.set_colorkey((255,255,255))
    PLAYER_LEFT_IMG = pg.image.load('player_left.png').convert()
    PLAYER_LEFT_IMG.set_colorkey((255,255,255))
    ENEMY_LIST = [pg.image.load('enemy1.png').convert(),
                  pg.image.load('enemy2.png').convert()]
    run_it=Control(screen)
    run_it.main_loop()
    pg.quit()


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 always
Dan

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
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Global constants and variables query

Postby Mekire » Sat Aug 17, 2013 10:56 pm

In a larger program the images are usually loaded into a constant dictionary on startup. You should never actually modify the original images so they are indeed constant.

I use a function like this for the loading:
Code: Select all
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):
Code: Select all
GFX = tools.load_all_gfx(os.path.join("resources","graphics"))

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.

-Mek
User avatar
Mekire
 
Posts: 982
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Global constants and variables query

Postby dboxall123 » Sun Aug 18, 2013 10:54 am

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!

Dan
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Global constants and variables query

Postby Mekire » Sun Aug 18, 2013 12:35 pm

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.

You will find the resource loading functions here:
https://github.com/metulburr/Plants-VS-Zombies/blob/master/data/tools.py#L146
And the display is initialized and graphics actually loaded here:
https://github.com/metulburr/Plants-VS-Zombies/blob/master/data/setup.py#L23

Feel free to use the face if it suits your need (though it is part of a set of faces =P).

Also if you haven't checked out this example yet, you can see a version where I rotate the face while moving (if contacting the ground). I have to rotate the face seperately though so the outer circle doesn't slightly distort and screw up my mask detection. This may or may not be necessary for you.
https://github.com/Mekire/meks-pygame-samples/blob/master/platforming/fall_rotate.py

-Mek

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.
User avatar
Mekire
 
Posts: 982
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron