alexpinho98
alexpinho98

Reputation: 909

Pygame and Multiprocessing Strategy

I'm developing a game in pygame and i don't know which tasks should go to which process.

I have two processes connected by a pipe, one will have the window another will do calculations.

My question is: Which parts of the main loop should go to the other process?

In my game i will have to do event handling, collision detection, AI, drawing and heavy calculations(2D lighting system).

I'm afraid that if i put to much stuff on the other process the main one will have to wait for input and the FPS will freeze.

PS: For now i'm just starting to code the game so i can't show you much code.

Upvotes: 1

Views: 1126

Answers (1)

User
User

Reputation: 14873

There is the observer pattern

I would suggest the following architecture for creating a PyGame with two processes:

You divide your program into two parts:

  1. model

    all the game logic is kept in the subprocess, computing the whole game. Whenever there is something noteworthy changed, it notifies the other process.

    responsibilities:

    • update the game e.g. in a loop
    • do physics
    • send updates to the gui
  2. gui

    The gui is in the main process because it starts several games. When a game is started it starts to observe important parts of the game.

    responsibilities

    • handle user input e.g. right arrow pressed
    • send modifications to the model e.g. player walks righth
    • render views of model elements when updates are received

Note that I do not really know much of PyGame. But keeping model and view apart is possible.

You can have a look at the MVC pattern, too. But it is really heavy. Just merging View and Controller is enough if the program shall not be distributed across computers.

Then I heard about MVVM pattern. Not sure whether this is too much again since you only need to split your game into two parts and not three.

Upvotes: 1

Related Questions