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')
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?
python pygame
repecmps
source share