Splitting Pygame functionality between classes or modules?

Posted by sec_goat on Game Development See other posts from Game Development or by sec_goat
Published on 2014-06-11T20:16:26Z Indexed on 2014/06/11 21:42 UTC
Read the original article Hit count: 230

Filed under:
|

I am attempting to make my pygame application more modular so that different functionalities are split up into different classes and modules. I am having some trouble getting pygame to allow me to draw or load images in secondary classes when the display has been set and pygame.init() has been done in my main class.

I have typically used C# and XNA to accomplish this sort of behavior, but this time I need to use python.

How do I init pygame in class1, then create an instance of class2 which loads and converts() images.

I have tried pygame.init() in class 2 but then it tells me no display mode has been set, when it has been set in class1. I am under the impression i do not wnat to create multiple pygame.displays as that gets problematic

I am probably missing something pythonic and simple but I am not sure what. How do I create a Display class, init python and then have other modules do my work like loading images, fonts etc.?

here is the simplest version of what I am doing:

    class1:

    def __init__(self):
        self.screen = pygame.display.set_mode((600,400))
        self.imageLoader = class2()


class2:
    def __init__(self):
        self.images = ['list of images']

    def load_images():
        self.images = os.listdir('./images/') #get all images in the images directory
        for img in self.images:
            #read all images in the directory and load them into pygame
            new_img = pygame.image.load(os.path.join('images', img)).convert()
            scale_img = pygame.transform.scale(new_img, (pygame.display.Info().current_w, pygame.display.Info().current_h))
            self.images.append(scale_img)


if __name__ == "__main__":
    c1 = class1()
    c1.imageLoader.load_images()

Of course when it tries to load an convert the images it tells me pygame has not been initialized, so i throw in a pygame.init() in class2 ( i have heard it is safe to init multiple times) and then the error goes to pygame.error: No video mode has been set

© Game Development or respective owner

Related posts about python

Related posts about pygame