Pigment Code Organization

I started learning how to create games using python / pygame, and as it is easy to quickly create a working game in pygame, there is no real guide to organizing the code in a smart way.

On the pygame man page, I found 3 ways to do this.

1- Do not use classes for small projects

2-MVC Ruby-on-rails structure type, but without a rail frame, which leads to something overly complex and obscure (even with OO programming and rail knowledge)

3- C ++ - a similar structure is as follows: (clean and intuitive, but maybe not very similar to python?)

import pygame from pygame.locals import * class MyGame: def __init__(self): self._running = True self._surf_display = None self.size = self.width, self.height = 150, 150 def on_init(self): pygame.init() self._display_surf = pygame.display.set_mode(self.size) pygame.display.set_caption('MyGame') #some more actions pygame.display.flip() self._running = True def on_event(self, event): if event.type == pygame.QUIT: self._running = False elif event.type == KEYDOWN: if event.key == K_ESCAPE: self._running = False elif event.type == MOUSEBUTTONDOWN: if event.button == 1: print event.pos def on_loop(self): pass def on_render(self): pass def on_cleanup(self): pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False while( self._running ): for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() self.on_cleanup() if __name__ == "__main__" : mygame = MyGame() mygame.on_execute() 

I am used to making SDL games in C ++, and I use this exact structure, but I wonder if it can be used for both small and large projects, or if pygame has a cleaner way.

For example, I found a game organized as follows:

 imports def functionx def functiony class MyGameObject: class AnotherObject: class Game: #pygame init, event handler, win, lose...etc while True: #event loop display update 

It also looks very well organized and easy to use.

What structure should I use consistently in all my projects in order to have clean code that can be used in small and large games?

+6
python pygame
source share
2 answers

I would also suggest that perhaps using comments (as boring as it seems) to share your work. As an example:

 import pygame, random, math ## CLASSES ---------------------------------------------- class Ball(): def __init__(self, (x,y), size): """Setting up the new instance""" self.x = x self.y = y self.size = size ## FUNCTIONS -------------------------------------------- def addVectors((angle1, length1), (angle2, length2)): """Take two vectors and find the resultant""" x = math.sin(angle1) * length1 + math.sin(angle2) * length2 y = math.cos(angle1) * length1 + math.cos(angle2) * length2 ## INIT ------------------------------------------------- width = 600 height = 400 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("S-kuru") 

And so on.

As another option, have you thought about using submodules? These are just other Python (.py) files where you host commonly used functions.

 def showText(passedVariable): print passedVariable return 

This new file is imported, just like math or random, and the function used as such:

 import mySubModule mySubModule.showText("Hello!") 

But that’s exactly how I work. Absolutely follow what you can understand, not only now, but next week or year.

+4
source share

Do what you can follow. If you can understand the code you posted, then this is what you should use. If another structure seems more natural, use it instead.

+2
source share

All Articles